C++服务器、QT客户端开发,烧录工具

C++服务器、QT客户端开发,烧录工具,第1张

C++服务器、QT客户端开发,烧录工具

项目参考基于C++实现的聊天室_csdn_wen的博客-CSDN博客_c++聊天室实现。

在此基础上修改了UI界面,增加了加密验证功能,客户端登陆时使用服务器公钥(RSA2048)加密用户名、密码、对称加密密钥(AES256),服务器接收到用户名和密码,用私钥解密,再和数据库对比,校验通过后,后续通信数据使用客户端发送的AES密钥进行对称加密。

注册功能修改为,客户端密码经过SHA256加密后发送给服务器,MySql中存储Sha256加密后的密码,防止通过数据库泄露密码。

烧录功能采用多线程,主线程负责显示UI、显示下载进度,子线程负责烧录,同时向主线程发送当前的烧录进度、结果。

项目地址:https://github.com/canghenzhilei123/downloadtool

一、环境: 客户端:

*** 作系统:windows10

IDE:Qt 5.14.2

编译器:MinGW 32-bit

服务器:

*** 作系统:华为云CentOS 7.6 64bit

数据库:MySql 5.7.36

二、使用方法 客户端使用方法:

1.安装CH341驱动,设备管理器中出现USB-EPP/I2C... CH341A代表安装成功。


2.安装windows版openssl,下载链接:文件分享,直接点击安装

安装完成后目录:include里面是头文件,lib是库文件,本系统需要用到libcrypto.lib

3.修改Qt项目配置,修改.pro文件,增加network,LIBS中配置CH341DLL.LIB路径,配置为实际路径。LIBS增加openssl libcrypto路径,INCLUDEPATH增加openssl头文件路径,修改为实际路径。

QT += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

ConFIG += c++14

LIBS += E:/code/downloadtool/downloadtool/CH341DLL.LIB

LIBS += -L$$quote(D:/OpenSSL-Win32/lib) -llibcrypto
INCLUDEPATH += $$quote(D:/OpenSSL-Win32/include)

 4.编译、将config、image、libcrypto-1_1.dll添加到如下目录。

 5.运行,运行成功效果如下图,d出登录窗口。

服务器使用方法:

 1.安装linux版openssl,参考Centos7安装openssl-1.1.1_rdisme-CSDN博客_centos7 安装openssl,我用的版本是1.1.1d

[root@hecs-33591 ~]# openssl version
OpenSSL 1.1.1d  10 Sep 2019
[root@hecs-33591 ~]#

2.安装jsoncpp,安装方法参考:Jsoncpp的安装配置及示例使用_jenie的专栏-CSDN博客_jsoncpp安装

3.安装MySql,安装方法参考Centos7安装MySQL详细步骤_Horizon-CSDN博客_centos7安装mysql的步骤和方法

安装成功后登录如下:

[root@hecs-33591 jsoncpp-master]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 588
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| chat               |
| chatroom           |
| mysql              |
| performance_schema |
| register           |
| sys                |
| yourdb             |
+--------------------+
8 rows in set (0.00 sec)

mysql>

建议安装Navicat Premium或其他客户端管理数据库。建立如下4个表,建表方法自行学习。

mysql> use chatroom;
Database changed
mysql> show tables;
+--------------------+
| Tables_in_chatroom |
+--------------------+
| firmware           |
| mac                |
| taskorder          |
| user               |
+--------------------+
4 rows in set (0.06 sec)

mysql> 
mysql> desc firmware;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| name       | varchar(20) | NO   |     | NULL    |       |
| version    | varchar(20) | NO   |     | NULL    |       |
| createtime | datetime    | YES  |     | NULL    |       |
| creater    | varchar(20) | NO   |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
4 rows in set (0.10 sec)

mysql> desc mac;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| address    | varchar(20) | NO   | PRI | NULL    |       |
| createtime | datetime    | YES  |     | NULL    |       |
| creater    | varchar(20) | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.09 sec)

mysql> desc taskorder;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id         | varchar(20) | NO   | PRI | NULL    |       |
| fw         | varchar(20) | YES  |     | NULL    |       |
| fwversion  | varchar(20) | YES  |     | NULL    |       |
| cnt        | int(11)     | YES  |     | NULL    |       |
| createtime | datetime    | YES  |     | NULL    |       |
| creater    | varchar(20) | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
6 rows in set (0.11 sec)

mysql> desc user;
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| account    | int(20)      | NO   | PRI | NULL    |       |
| username   | varchar(100) | NO   |     | NULL    |       |
| password   | varchar(100) | NO   |     | NULL    |       |
| createTime | datetime     | NO   |     | NULL    |       |
+------------+--------------+------+-----+---------+-------+
4 rows in set (0.10 sec)

mysql> 

4.修改Makefile,加入openssl头文件和库文件路径,加入库文件引用:-I/opt/ssl/include/ -L/opt/ssl/lib/ -lcrypto,加入jsoncpp头文件和库文件路径,加入库文件引用

-I/usr/local/include/jsoncpp -L/usr/local/lib64

OBJS = ./out/main.o ./out/MyTime.o ./out/MySQLConnector.o ./out/UserService.o ./out/Online.o ./out/DataEncoder.o ./out/HeadData.o ./out/DataProcesser.o ./out/base64.o ./out/sha256.o ./out/rsacrypto.o ./out/aescrypto.o
main: $(OBJS)
	g++ -std=c++11 $(OBJS) -o main `mysql_config --cflags --libs` -ljsoncpp -I/usr/local/include/jsoncpp -L/usr/local/lib64 -I/opt/ssl/include/ -L/opt/ssl/lib/ -lcrypto
./out/main.o: main.cpp ./ProtocolHead/HeadData.h ./Service/DataProcesser.h ./Service/UserService.h ./Service/Online.h ./config/server_config.h ./crypto/base64.h ./crypto/sha256.h ./crypto/rsacrypto.h ./crypto/aescrypto.h
	g++ -std=c++11 -c main.cpp -o ./out/main.o -I/usr/local/include/jsoncpp -L/usr/local/lib64 -I/opt/ssl/include/ -L/opt/ssl/lib/ -lcrypto
./out/DataProcesser.o: ./Service/DataProcesser.cpp ./ProtocolHead/protocolmsg.h ./ProtocolHead/HeadData.h ./ProtocolHead/DataEncoder.h ./Util/MyTime.h
	g++ -std=c++11 -c ./Service/DataProcesser.cpp -o ./out/DataProcesser.o -I/usr/local/include/jsoncpp -L/usr/local/lib64
./out/HeadData.o: ./ProtocolHead/HeadData.cpp ./ProtocolHead/protocolmsg.h
	g++ -std=c++11 -c ./ProtocolHead/HeadData.cpp -o ./out/HeadData.o -I/usr/local/include/jsoncpp -L/usr/local/lib64
./out/DataEncoder.o: ./ProtocolHead/DataEncoder.cpp ./ProtocolHead/protocolmsg.h
	g++ -std=c++11 -c ./ProtocolHead/DataEncoder.cpp -o ./out/DataEncoder.o -I/usr/local/include/jsoncpp -L/usr/local/lib64
./out/Online.o: ./Service/Online.cpp ./Util/MyTime.h
	g++ -std=c++11 -c ./Service/Online.cpp -o ./out/Online.o -I/usr/local/include/jsoncpp -L/usr/local/lib64
./out/UserService.o: ./Service/UserService.cpp ./Dao/MySQLConnector.h
	g++ -std=c++11 -c ./Service/UserService.cpp -o ./out/UserService.o -I/usr/local/include/jsoncpp -L/usr/local/lib64
./out/MySQLConnector.o: ./Dao/MySQLConnector.cpp ./Util/MyTime.h ./config/mysql_config.h
	g++ -std=c++11 -c ./Dao/MySQLConnector.cpp -o ./out/MySQLConnector.o -I/usr/local/include/jsoncpp -L/usr/local/lib64
./out/MyTime.o: ./Util/MyTime.cpp
	g++ -std=c++11 -c ./Util/MyTime.cpp -o ./out/MyTime.o -I/usr/local/include/jsoncpp -L/usr/local/lib64
./out/base64.o: ./crypto/base64.cpp
	g++ -std=c++11 -c ./crypto/base64.cpp -o ./out/base64.o -I/opt/ssl/include/ -L/opt/ssl/lib/ -lcrypto
./out/sha256.o: ./crypto/sha256.cpp
	g++ -std=c++11 -c ./crypto/sha256.cpp -o ./out/sha256.o -I/opt/ssl/include/ -L/opt/ssl/lib/ -lcrypto
./out/rsacrypto.o: ./crypto/rsacrypto.cpp ./crypto/base64.h
	g++ -std=c++11 -c ./crypto/rsacrypto.cpp -o ./out/rsacrypto.o -I/opt/ssl/include/ -L/opt/ssl/lib/ -lcrypto
./out/aescrypto.o: ./crypto/aescrypto.cpp ./crypto/base64.h
	g++ -std=c++11 -c ./crypto/aescrypto.cpp -o ./out/aescrypto.o -I/opt/ssl/include/ -L/opt/ssl/lib/ -lcrypto
	
clean:
	rm -rf ./out/*.o main


5.修改MySql配置,IP,用户名,密码,连接哪个数据库。

[root@hecs-33591 config]# ls
mysql_config.h  server_config.h
[root@hecs-33591 config]# vim mysql_config.h
#pragma once
#define HOST "127.0.0.1"
#define USERNAME "root"
#define PASSWORD ""
#define PORT 3306
#define DATAbase "chatroom"

修改服务器IP,端口,最大连接数。

#pragma once
#define HOST "127.0.0.1"
#define PORT 9999
#define MAX_ConNECTIONS 1024

编译环境如下:

[root@hecs-33591 server]# cmake --version
cmake version 3.21.0-rc2

CMake suite maintained and supported by Kitware (kitware.com/cmake).
[root@hecs-33591 server]# g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-9/root/usr/libexec/gcc/x86_64-redhat-linux/9/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-9/root/usr --mandir=/opt/rh/devtoolset-9/root/usr/share/man --infodir=/opt/rh/devtoolset-9/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-9.3.1-20200408/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC)

6.编译,在main.cpp路径中执行make,编译成功后生成main

[root@hecs-33591 server]# ls
config  crypto  Dao  fw  image  main.cpp  Makefile  out  ProtocolHead  Service  Util
[root@hecs-33591 server]# make
g++ -std=c++11 -c main.cpp -o ./out/main.o -I/usr/local/include/jsoncpp -L/usr/local/lib64 -I/opt/ssl/include/ -L/opt/ssl/lib/ -lcrypto
In file included from main.cpp:16:
config/server_config.h:6: warning: "PORT" redefined
    6 | #define PORT 9999
      |
In file included from Service/../Dao/MySQLConnector.h:12,
                 from Service/UserService.h:6,
                 from main.cpp:14:
Service/../Dao/../config/mysql_config.h:8: note: this is the location of the previous definition
    8 | #define PORT 3306
      |
g++ -std=c++11 -c ./Util/MyTime.cpp -o ./out/MyTime.o -I/usr/local/include/jsoncpp -L/usr/local/lib64
g++ -std=c++11 -c ./Dao/MySQLConnector.cpp -o ./out/MySQLConnector.o -I/usr/local/include/jsoncpp -L/usr/local/lib64
g++ -std=c++11 -c ./Service/UserService.cpp -o ./out/UserService.o -I/usr/local/include/jsoncpp -L/usr/local/lib64
g++ -std=c++11 -c ./Service/Online.cpp -o ./out/Online.o -I/usr/local/include/jsoncpp -L/usr/local/lib64
g++ -std=c++11 -c ./ProtocolHead/DataEncoder.cpp -o ./out/DataEncoder.o -I/usr/local/include/jsoncpp -L/usr/local/lib64
g++ -std=c++11 -c ./ProtocolHead/HeadData.cpp -o ./out/HeadData.o -I/usr/local/include/jsoncpp -L/usr/local/lib64
g++ -std=c++11 -c ./Service/DataProcesser.cpp -o ./out/DataProcesser.o -I/usr/local/include/jsoncpp -L/usr/local/lib64
g++ -std=c++11 -c ./crypto/base64.cpp -o ./out/base64.o -I/opt/ssl/include/ -L/opt/ssl/lib/ -lcrypto
g++ -std=c++11 -c ./crypto/sha256.cpp -o ./out/sha256.o -I/opt/ssl/include/ -L/opt/ssl/lib/ -lcrypto
g++ -std=c++11 -c ./crypto/rsacrypto.cpp -o ./out/rsacrypto.o -I/opt/ssl/include/ -L/opt/ssl/lib/ -lcrypto
./crypto/rsacrypto.cpp: In member function ‘int RsaCrypto::GenerateRSAKey(std::string*)’:
./crypto/rsacrypto.cpp:53:66: warning: ‘RSA* RSA_generate_key(int, long unsigned int, void (*)(int, int, void*), void*)’ is deprecated [-Wdeprecated-declarations]
   53 |     RSA* keypair = RSA_generate_key(KEY_LENGTH, RSA_3, NULL, NULL);
      |                                                                  ^
In file included from /usr/include/openssl/e_os2.h:13,
                 from /usr/include/openssl/err.h:13,
                 from ./crypto/rsacrypto.h:4,
                 from ./crypto/rsacrypto.cpp:1:
/usr/include/openssl/rsa.h:234:1: note: declared here
  234 | DEPRECATEDIN_0_9_8(RSA *RSA_generate_key(int bits, unsigned long e, void
      | ^~~~~~~~~~~~~~~~~~
./crypto/rsacrypto.cpp:53:66: warning: ‘RSA* RSA_generate_key(int, long unsigned int, void (*)(int, int, void*), void*)’ is deprecated [-Wdeprecated-declarations]
   53 |     RSA* keypair = RSA_generate_key(KEY_LENGTH, RSA_3, NULL, NULL);
      |                                                                  ^
In file included from /usr/include/openssl/e_os2.h:13,
                 from /usr/include/openssl/err.h:13,
                 from ./crypto/rsacrypto.h:4,
                 from ./crypto/rsacrypto.cpp:1:
/usr/include/openssl/rsa.h:234:1: note: declared here
  234 | DEPRECATEDIN_0_9_8(RSA *RSA_generate_key(int bits, unsigned long e, void
      | ^~~~~~~~~~~~~~~~~~
g++ -std=c++11 -c ./crypto/aescrypto.cpp -o ./out/aescrypto.o -I/opt/ssl/include/ -L/opt/ssl/lib/ -lcrypto
g++ -std=c++11 ./out/main.o ./out/MyTime.o ./out/MySQLConnector.o ./out/UserService.o ./out/Online.o ./out/DataEncoder.o ./out/HeadData.o ./out/DataProcesser.o ./out/base64.o ./out/sha256.o ./out/rsacrypto.o ./out/aescrypto.o -o main `mysql_config --cflags --libs` -ljsoncpp -I/usr/local/include/jsoncpp -L/usr/local/lib64 -I/opt/ssl/include/ -L/opt/ssl/lib/ -lcrypto
[root@hecs-33591 server]# ls
config  crypto  Dao  fw  image  main  main.cpp  Makefile  out  ProtocolHead  Service  Util

7.执行./main,执行成功后提示listening...

[root@hecs-33591 server]# ls
config  crypto  Dao  fw  image  main  main.cpp  Makefile  out  ProtocolHead  Service  Util
[root@hecs-33591 server]# ./main
listening...

此时用客户端连接服务器,连接成功,提示如下:

[root@hecs-33591 server]# ./main
listening...
收到1个请求
收到1个请求
protocolId:1 account:123 dataType: 1 dataLength:1108
login..
account:123
password:8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92
aeskey:08747770838048636999762507057513
login succeed
收到1个请求
收到1个请求
protocolId:3 account:123 dataType: 1 dataLength:0
read..
account:123
username:用户A
loginMsg:用户A(123)走进了聊天室!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5693900.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存