《QT实用小工具·七十》openssl+qt开发的P2P文件加密传输工具

2024-06-10 11:36

本文主要是介绍《QT实用小工具·七十》openssl+qt开发的P2P文件加密传输工具,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、概述
源码放在文章末尾

该项目实现了P2P的文件加密传输功能,具体包含如下功能:
1、 多文件多线程传输
2、rsa+aes文件传输加密
3、秘钥随机生成
4、断点续传
5、跨域传输引导服务器

项目界面如下所示:
接收界面
在这里插入图片描述

发送界面
在这里插入图片描述

RSA秘钥生成,AES秘钥生成
在这里插入图片描述

项目部分代码如下所示:


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <openssl/ssl.h>
#include <QFile>
#include <QDebug>#include <QString>
#include <openssl/ssl.h>
#include <openssl/sha.h>
#include <openssl/aes.h>#include <iostream>
#include <fstream>
#include<string>
#include<QFileDialog>
#include<QDateTime>
#include<QThread>using namespace std;
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);//启动监听但是暂停接收连接serverStatus=false;tcpServer=new QTcpServer(this);//初始化文件接收服务器if(!tcpServer->listen(QHostAddress::Any,6666)){qDebug()<<tcpServer->errorString();close();}else {qDebug()<<"listening.....................";}connect(tcpServer,SIGNAL(newConnection()),this,SLOT(acceptConnection()));tcpServer->pauseAccepting();rsatool.rsaKeyInit();//初始化非对称密钥aestool.keyInit();//初始化对称密钥ui->aesview->setText(aestool.key);ui->rsapriview->setText(rsatool.priKey);ui->rsapubview->setText(rsatool.pubKey);ui->recvTable->setColumnCount(6);ui->recvTable->setRowCount(0);ui->recvTable->setHorizontalHeaderLabels(QStringList()<<"接收方"<<"发送方"<<"文件名"<<"文件大小"<<"时间"<<"进度");ui->recvTable->setSelectionBehavior(QAbstractItemView::SelectRows);  //整行选中的方式ui->recvTable->setEditTriggers(QAbstractItemView::NoEditTriggers);   //禁止修改ui->recvTable->setSelectionMode(QAbstractItemView::SingleSelection);  //设置为可以选中单个ui->recvTable->verticalHeader()->setVisible(false);   //隐藏列表头ui->recvTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);ui->recvTable->selectRow(0);ui->sendTable->setColumnCount(2);ui->sendTable->setRowCount(0);ui->sendTable->setHorizontalHeaderLabels(QStringList()<<"文件"<<"进度");ui->sendTable->setSelectionBehavior(QAbstractItemView::SelectRows);  //整行选中的方式ui->sendTable->setEditTriggers(QAbstractItemView::NoEditTriggers);   //禁止修改ui->sendTable->setSelectionMode(QAbstractItemView::SingleSelection);  //设置为可以选中单个ui->sendTable->verticalHeader()->setVisible(false);   //隐藏列表头ui->sendTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);}void MainWindow::displayError(QAbstractSocket::SocketError)
{QTcpSocket *tcpSocket = qobject_cast<QTcpSocket *>(sender());qDebug()<<tcpSocket->errorString();
}void MainWindow::acceptConnection(){QTcpSocket *tcpSocket=new QTcpSocket(this);tcpSocket=tcpServer->nextPendingConnection();connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(revData()));connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),SLOT(displayError(QAbstractSocket::SocketError)));}
//接收字符串
void MainWindow::revData(){QTcpSocket *tcpSocket = qobject_cast<QTcpSocket *>(sender());QString  tmp;QString re;char buf[16]={0},out[16]={0};char tou[sizeof(Head)];if(tableid.count(tcpSocket)==0){//判断是不是第一次触发。如果是就解析head信息int cols=ui->recvTable->columnCount();int rows=ui->recvTable->rowCount();tranStatus temp;Head head;QString path="D:/recv/";tcpSocket->read(tou,sizeof(Head));memcpy(&head, tou, sizeof(head));QString mid="";QFileInfo info((path+mid+QString(head.name)));while(info.exists()){//判断是否出现重名文件mid=QString::number(rand()%100000);info.setFile(path+mid+QString(head.name));}tableid[tcpSocket].name=(path+mid+QString(head.name));tableid[tcpSocket].size=head.size;tableid[tcpSocket].row=rows;tableid[tcpSocket].step=0;ui->recvTable->insertRow(rows);//更新界面表格数据ui->recvTable->setItem(rows,0,new QTableWidgetItem(tcpSocket->localAddress().toString()));//接收ui->recvTable->setItem(rows,1,new QTableWidgetItem(tcpSocket->peerAddress().toString()));//发送ui->recvTable->setItem(rows,2,new QTableWidgetItem(tableid[tcpSocket].name));//文件名ui->recvTable->setItem(rows,3,new QTableWidgetItem(QString::number(tableid[tcpSocket].size)));//文件大小ui->recvTable->setItem(rows,4,new QTableWidgetItem(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd")));//时间ui->recvTable->setItem(rows,5,new QTableWidgetItem(QString::number(tableid[tcpSocket].step/tableid[tcpSocket].size)+"%"));strcpy(head.key,rsatool.rsaPubEncrypt(aestool.key,head.key).toUtf8());//将自己的对称密钥用公钥加密发给对方tcpSocket->write((char*)&head,sizeof(head));
}ofstream outFile(tableid[tcpSocket].name.toLocal8Bit(),ios::binary |ios::app);  //以二进制写模式打开文件while(tcpSocket->read(out,16)){//每次读取16个字节assert(aestool.aes256_decrypt(out, buf)==0);  //解密for(int j=0;j<buf[15];j++){tmp+=buf[j];outFile.put(buf[j]);tableid[tcpSocket].step++;//文件传输进度}}outFile.close();//更新进度ui->recvTable->setItem(tableid[tcpSocket].row,5,new QTableWidgetItem(QString::number(tableid[tcpSocket].step)+"/"+QString::number(tableid[tcpSocket].size)));}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::on_addFiles_clicked()
{int cols=ui->sendTable->columnCount();int rows=ui->sendTable->rowCount();QFileDialog *fileDialog = new QFileDialog(this);//定义文件对话框标题fileDialog->setWindowTitle(QStringLiteral("选中文件"));//设置默认文件路径fileDialog->setDirectory(".");//设置文件过滤器fileDialog->setNameFilter(tr("File(*.*)"));//设置可以选择多个文件,默认为只能选择一个文件QFileDialog::ExistingFilesfileDialog->setFileMode(QFileDialog::ExistingFiles);//设置视图模式fileDialog->setViewMode(QFileDialog::Detail);//打印所有选择的文件的路径QStringList fileNames;if (fileDialog->exec()) {fileNames = fileDialog->selectedFiles();for(int i=0;i<fileNames.size();i++){sendlist.append({fileNames[i],0});ui->sendTable->insertRow(rows);qDebug()<<fileNames[i]<<endl;ui->sendTable->setItem(rows,i,new QTableWidgetItem("new"+QString::number(rows)));ui->sendTable->selectRow(rows);//ui->sendTable->setItem(rows,0,new QTableWidgetItem(tcpSocket->localAddress().toString()));//接收ui->sendTable->setItem(rows,0,new QTableWidgetItem(fileNames[i]));//文件名ui->sendTable->setItem(rows,1,new QTableWidgetItem("null"));rows++;//sendfile(fileNames[i]);}}
}
void MainWindow::on_sendFiles_clicked()
{for(int i=0;i<sendlist.size();i++){if(sendlist[i].second==0){ui->sendTable->setItem(i,1,new QTableWidgetItem("正在发送"));m_copiers.push_back(new CopyFileThread);m_copiers[m_copiers.size()-1]->set(i,sendlist[i].first,ui->Ipinput->text(),6666,rsatool,my.uid);//设置发送文件所需参数//m_copiers[m_copiers.size()-1]->initConnect();//建立连接,交换对称密钥connect( m_copiers[m_copiers.size()-1], SIGNAL(sendresult(int,int,QString)), this, SLOT(upsendresult(int,int,QString)));//建立发送结果回调m_copiers[m_copiers.size()-1]->start();}}
}void MainWindow::upsendresult(int row,int re,QString log){if(re==1){ui->sendTable->setItem(row,1,new QTableWidgetItem("发送完成"));}else{ui->sendTable->setItem(row,1,new QTableWidgetItem("发送失败"));}ui->sendlog->append(log);sendlist[row].second=1;
}
void MainWindow::on_updateaes_clicked()
{aestool.keyInit();ui->aesview->setText(aestool.key);}void MainWindow::on_updatetrsa_clicked()
{rsatool.rsaKeyInit();ui->rsapriview->setText(rsatool.priKey);ui->rsapubview->setText(rsatool.pubKey);
}void MainWindow::on_onServer_clicked()
{if(serverStatus==false){tcpServer->resumeAccepting();ui->onServer->setText("正在监听....");serverStatus=true;}else{tcpServer->pauseAccepting();ui->onServer->setText("启动监听");serverStatus=false;}}

源码下载

这篇关于《QT实用小工具·七十》openssl+qt开发的P2P文件加密传输工具的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1048054

相关文章

SQLite3命令行工具最佳实践指南

《SQLite3命令行工具最佳实践指南》SQLite3是轻量级嵌入式数据库,无需服务器支持,具备ACID事务与跨平台特性,适用于小型项目和学习,sqlite3.exe作为命令行工具,支持SQL执行、数... 目录1. SQLite3简介和特点2. sqlite3.exe使用概述2.1 sqlite3.exe

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

基于Python实现一个Windows Tree命令工具

《基于Python实现一个WindowsTree命令工具》今天想要在Windows平台的CMD命令终端窗口中使用像Linux下的tree命令,打印一下目录结构层级树,然而还真有tree命令,但是发现... 目录引言实现代码使用说明可用选项示例用法功能特点添加到环境变量方法一:创建批处理文件并添加到PATH1

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

CnPlugin是PL/SQL Developer工具插件使用教程

《CnPlugin是PL/SQLDeveloper工具插件使用教程》:本文主要介绍CnPlugin是PL/SQLDeveloper工具插件使用教程,具有很好的参考价值,希望对大家有所帮助,如有错... 目录PL/SQL Developer工具插件使用安装拷贝文件配置总结PL/SQL Developer工具插

基于Python开发一个有趣的工作时长计算器

《基于Python开发一个有趣的工作时长计算器》随着远程办公和弹性工作制的兴起,个人及团队对于工作时长的准确统计需求日益增长,本文将使用Python和PyQt5打造一个工作时长计算器,感兴趣的小伙伴可... 目录概述功能介绍界面展示php软件使用步骤说明代码详解1.窗口初始化与布局2.工作时长计算核心逻辑3

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

Python使用FFmpeg实现高效音频格式转换工具

《Python使用FFmpeg实现高效音频格式转换工具》在数字音频处理领域,音频格式转换是一项基础但至关重要的功能,本文主要为大家介绍了Python如何使用FFmpeg实现强大功能的图形化音频转换工具... 目录概述功能详解软件效果展示主界面布局转换过程截图完成提示开发步骤详解1. 环境准备2. 项目功能结