《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

相关文章

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

QT Creator配置Kit的实现示例

《QTCreator配置Kit的实现示例》本文主要介绍了使用Qt5.12.12与VS2022时,因MSVC编译器版本不匹配及WindowsSDK缺失导致配置错误的问题解决,感兴趣的可以了解一下... 目录0、背景:qt5.12.12+vs2022一、症状:二、原因:(可以跳过,直奔后面的解决方法)三、解决方

Python实战之SEO优化自动化工具开发指南

《Python实战之SEO优化自动化工具开发指南》在数字化营销时代,搜索引擎优化(SEO)已成为网站获取流量的重要手段,本文将带您使用Python开发一套完整的SEO自动化工具,需要的可以了解下... 目录前言项目概述技术栈选择核心模块实现1. 关键词研究模块2. 网站技术seo检测模块3. 内容优化分析模

从基础到进阶详解Python条件判断的实用指南

《从基础到进阶详解Python条件判断的实用指南》本文将通过15个实战案例,带你大家掌握条件判断的核心技巧,并从基础语法到高级应用一网打尽,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录​引言:条件判断为何如此重要一、基础语法:三行代码构建决策系统二、多条件分支:elif的魔法三、

基于Java开发一个极简版敏感词检测工具

《基于Java开发一个极简版敏感词检测工具》这篇文章主要为大家详细介绍了如何基于Java开发一个极简版敏感词检测工具,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录你是否还在为敏感词检测头疼一、极简版Java敏感词检测工具的3大核心优势1.1 优势1:DFA算法驱动,效率提升10

Python 字符串裁切与提取全面且实用的解决方案

《Python字符串裁切与提取全面且实用的解决方案》本文梳理了Python字符串处理方法,涵盖基础切片、split/partition分割、正则匹配及结构化数据解析(如BeautifulSoup、j... 目录python 字符串裁切与提取的完整指南 基础切片方法1. 使用切片操作符[start:end]2

Python开发简易网络服务器的示例详解(新手入门)

《Python开发简易网络服务器的示例详解(新手入门)》网络服务器是互联网基础设施的核心组件,它本质上是一个持续运行的程序,负责监听特定端口,本文将使用Python开发一个简单的网络服务器,感兴趣的小... 目录网络服务器基础概念python内置服务器模块1. HTTP服务器模块2. Socket服务器模块

MySQL慢查询工具的使用小结

《MySQL慢查询工具的使用小结》使用MySQL的慢查询工具可以帮助开发者识别和优化性能不佳的SQL查询,本文就来介绍一下MySQL的慢查询工具,具有一定的参考价值,感兴趣的可以了解一下... 目录一、启用慢查询日志1.1 编辑mysql配置文件1.2 重启MySQL服务二、配置动态参数(可选)三、分析慢查