Qt RSA OpenSSL C++ Qt加密解密签字通信系统窗体源码

news2024/9/24 13:22:38

 程序示例精选

Qt RSA OpenSSL C++ Qt加密解密签字通信系统窗体

如需安装运行环境或远程调试,见文章底部微信名片,由专业技术人员远程协助!

前言

这篇博客针对<<Qt RSA OpenSSL C++ Qt加密解密签字通信系统窗体>>编写代码,主要功能包括了客户端,服务端,信息加密解密。代码整洁,规则,易读。 应用推荐首选。


文章目录

        一、所需工具软件

        二、使用步骤

                1. 引入库

                2. 客户端代码

                3. 服务端代码

                4. 运行结果

         三、在线协助


一、所需工具软件

          1. Visual Studio

          2. Qt, C++

二、使用步骤

1.引入库

代码如下(示例):

#include "MainWindow.h"
#include "stdafx.h"
#include "RSAEncode.h"
#include "RSASign.h"

#include <iostream>
#include <string>
#include <bitset>
#include <fstream>
#include <stdio.h>

2.客户端代码

代码如下(示例):

//加密

MainWindow::MainWindow(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);

	QObject::connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(TestRSAEncode_sendout()));
    QObject::connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(receiver()));

}


void MainWindow::TestRSASign()
{
	GenerateKeyEx();
	string strData = "orderId=01010500201502000004reqTime=20150205012727ext=20151120ext2=1";
	string strSigned;
	if (RSASignAction(strData, strSigned))
	{
		printf("加签成功\n");
		if (RSAVerifyAction(strData, strSigned))
		{
			printf("验签成功\n");
		}

	}
}


//发送
string strEncode;
string strDecode;
void MainWindow::TestRSAEncode_sendout()
{

	TestRSASign();

	//
	
	QString sendoutWord = QString("%1").arg(ui.textEdit->toPlainText());
	string strEncodeData = sendoutWord.toStdString();
	//string strEncodeData = "你好吗";


	GenerateKey();
	strEncode = EncodeByBioPublicKey(strEncodeData);
	strDecode = DecodeByBioPrivateKey(strEncode);

	printf("Old data:%s\n", strEncodeData.c_str());
	printf("Encode data:%s\n", strEncode.c_str());
	printf("Decode data:%s\n", strDecode.c_str());


	std::string encoded64 = base64_encode((const char*)(strEncode.c_str()), strEncode.length());
	std::cout << "base64encodedtxt:" << encoded64 << std::endl;
	
	ui.textEdit_3->append(QString::fromStdString("原文字: ") + strEncodeData.c_str());
	ui.textEdit_3->append(QString::fromStdString("已加密符号: ") + strEncode.c_str());

	QFile::remove("saveSendOut.txt");
	QFile filetxt(QString::fromStdString("saveSendOut.txt"));
	if (filetxt.open(QIODevice::WriteOnly))
	{
		QTextStream out(&filetxt);
		out << QString::fromStdString(encoded64);
		out.setCodec("UTF-8");
		out.flush();

	}
	filetxt.close();

	std::string dir555 = "saveSendOut.txt";
	std::string filePathCopy = "../server-exe/saveSendOut.txt";
	std::string filePathCopy2 = "../server-exe/private.pem";

	//std::string filePathCopy = "D:/saveSendOut.txt";
	QFile::remove(QString::fromStdString(filePathCopy));
	QFile::remove(QString::fromStdString(filePathCopy2));



}


inline string readFile(const string& filename) 
{
	ifstream in(filename);
	istreambuf_iterator<char> begin(in), end;
	string content(begin, end);
	in.close();
	return content;
}
void MainWindow::receiver()
{
	//自接收
	string strDecode = DecodeByBioPrivateKey(strEncode);
    std::cout << "strDecode: " << strDecode << std::endl;
    QString title2 = QString::fromStdString(strDecode).remove("?");
    ui.textEdit_2->setPlainText(title2);
	ui.textEdit_4->append(QString::fromStdString("自接收解密: ") + strDecode.c_str());

	//远程接收
	//QFile file(QString::fromStdString("saveSendOut.txt"));
	//if (file.open(QFile::ReadOnly | QFile::Text))
	//{
	//	QTextStream fs(&file);
	//	QString fileContent(fs.readAll());
	//	std::cout << "fileContent: " << fileContent.toStdString() << std::endl;

	//	string strDecodeRemotion = DecodeByBioPrivateKey(fileContent.toStdString());
	//	ui.textEdit_4->append(QString::fromStdString("远程接收解密: ") + strDecodeRemotion.c_str());
	//}
	//file.close();


	string content = readFile("saveSendOut_server.txt");
	string strDecodeRemotion = DecodeByBioPrivateKey(decoded64);
	ui.textEdit_4->append(QString::fromStdString("远程接收解密: ") + strDecodeRemotion.c_str());

}

3.服务端代码: 

//加密
MainWindow::MainWindow(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);

	QObject::connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(TestRSAEncode_sendout()));
    QObject::connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(receiver()));

}


void MainWindow::TestRSASign()
{
	GenerateKeyEx();
	string strData = "orderId=01010500201502000004reqTime=20150205012727ext=20151120ext2=1";
	string strSigned;
	if (RSASignAction(strData, strSigned))
	{
		printf("加签成功\n");
		if (RSAVerifyAction(strData, strSigned))
		{
			printf("验签成功\n");
		}

	}
}


//发送
string strEncode;
string strDecode;
void MainWindow::TestRSAEncode_sendout()
{

	TestRSASign();

	//
	
	QString sendoutWord = QString("%1").arg(ui.textEdit->toPlainText());
	string strEncodeData = sendoutWord.toStdString();
	//string strEncodeData = "你好吗";


	GenerateKey();
	strEncode = EncodeByBioPublicKey(strEncodeData);
	strDecode = DecodeByBioPrivateKey(strEncode);

	printf("Old data:%s\n", strEncodeData.c_str());
	printf("Encode data:%s\n", strEncode.c_str());
	printf("Decode data:%s\n", strDecode.c_str());


	std::string encoded64 = base64_encode((const char*)(strEncode.c_str()), strEncode.length());
	std::cout << "base64encodedtxt:" << encoded64 << std::endl;


	QFile::remove("saveSendOut.txt");
	QFile filetxt(QString::fromStdString("saveSendOut_server.txt"));
	if (filetxt.open(QIODevice::WriteOnly))
	{
		QTextStream out(&filetxt);
		out.setCodec("UTF-8");
		out.flush();

	}
	filetxt.close();

	std::string dir555 = "saveSendOut_server.txt";
	std::string filePathCopy = "../client-exe/saveSendOut_server.txt";
	std::string filePathCopy2 = "../client-exe/private.pem";

	//std::string filePathCopy = "D:/saveSendOut.txt";
	QFile::remove(QString::fromStdString(filePathCopy));
	QFile::remove(QString::fromStdString(filePathCopy2));
	QFile::copy(QString::fromStdString(dir555), QString::fromStdString(filePathCopy));
	QFile::copy(QString::fromStdString("private.pem"), QString::fromStdString(filePathCopy2));
}


inline string readFile(const string& filename) 
{
	ifstream in(filename);
	istreambuf_iterator<char> begin(in), end;
	string content(begin, end);
	in.close();
	return content;
}
void MainWindow::receiver()
{
	//自接收
	string strDecode = DecodeByBioPrivateKey(strEncode);
    std::cout << "strDecode: " << strDecode << std::endl;
    ui.textEdit_2->setPlainText(title2);
	ui.textEdit_4->append(QString::fromStdString("自接收解密: ") + strDecode.c_str());

	//远程接收
	//QFile file(QString::fromStdString("saveSendOut.txt"));
	//if (file.open(QFile::ReadOnly | QFile::Text))
	//{
	//	QTextStream fs(&file);
	//	QString fileContent(fs.readAll());
	//	std::cout << "fileContent: " << fileContent.toStdString() << std::endl;

	//	string strDecodeRemotion = DecodeByBioPrivateKey(fileContent.toStdString());
	//	ui.textEdit_4->append(QString::fromStdString("远程接收解密: ") + strDecodeRemotion.c_str());
	//}
	//file.close();

	//远程接收

	string content = readFile("saveSendOut.txt");
	std::string decoded64 = base64_decode(content);
	string strDecodeRemotion = DecodeByBioPrivateKey(decoded64);
	ui.textEdit_4->append(QString::fromStdString("远程接收解密: ") + strDecodeRemotion.c_str());

}

4.运行结果如下: 

三、在线协助: 

如需安装运行环境或远程调试,见文章底部微信名片,由专业技术人员远程协助!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/130393.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

年终回顾 | 小米技术最受欢迎的技术文章TOP20

转眼间&#xff0c;小米技术已经陪伴大家度过了一整个年头。在一年里&#xff0c;我们始终坚持为大家提供有趣好玩的技术科普、硬核前沿的技术干货&#xff0c;带给大家一切有关小米的新鲜技术创新内容。2023年就要到了&#xff0c;欢迎你点击文末左下角的“阅读原文”填写一份…

Vue3:搜索框输入防抖实现整理笔记

目录 场景需求 前言 防抖 & 节流 防抖 节流 输入防抖存在的问题 指令实现 总结 在Vue开发中&#xff0c;遇到了搜索框输入防抖处理&#xff0c;算是防抖的使用场景之一吧&#xff0c;抽象其逻辑记录下来以备后用 场景需求 作为开发人员&#xff0c;一定要先搞清楚…

Android---Material Design

目录 一、什么是Material Design Z轴 Material Design 的一些 theme 一、什么是Material Design Material Design 中文名&#xff1a;材料设计语言&#xff0c;是由 Google 推出的全新的设计语言。Google 表示&#xff0c;这种设计语言旨在为手机、平板、台式机和“其它平台”…

标签平滑(Label Smoothing)详解

一、什么是label smoothing&#xff1f; 标签平滑&#xff08;Label smoothing&#xff09;&#xff0c;像L1、L2和dropout一样&#xff0c;是机器学习领域的一种正则化方法&#xff0c;通常用于分类问题&#xff0c;目的是防止模型在训练时过于自信地预测标签&#xff0c;改善…

spring6笔记2( ioc、bean的作用域、工厂模式、bean的四种实例化方式,生命周期)

第四章、Spring对ioc的实现 4.4 p命名空间注入 目的&#xff1a;简化配置。 使用p命名空间注入的前提条件包括两个&#xff1a; 第一&#xff1a;在XML头部信息中添加p命名空间的配置信息&#xff1a;xmlns:p"http://www.springframework.org/schema/p"第二&…

wanglinrong 程序 环境配置

1、总体要求 我的matlab版本是 r2020b、matconvnet-1.0-beta25、visual studio 2022。笔记本安装&#xff0c;没考虑GPU。建议vs的版本尽量比matlab版本低。 1.1 完美解决方案&#xff1a; 低版本Visual Studio与高版本Matlab&#xff0c;先装vs 后装matlab&#xff01; 比如…

windows下PyTorch安装教程(1.10)

文章目录一.pytorch 1.10版本安装教程一.pytorch 1.10版本安装教程 PyTorch官网 pytorch与cuda版本关系 官网 从官网选择自己对应的conda,python,cuda版本&#xff0c;复制conda命令 在windows搜索框中搜索CMD&#xff0c;选择以管理员身份运行 使用conda新建虚拟环境pyt…

arthas离线包使用说明

arthas离线包使用说明 基于私有化全内网场景&#xff0c;打包了一套arthas离线包&#xff0c;方便后续对服务进行调试和问题的定位。 首先将arthas-bin.zip导入到服务器中 下载连接&#xff1a;https://download.csdn.net/download/Decembetion/87347459 将zip包解压 #解压 unz…

c4d导入大模型以及给建筑上贴图笔记

快捷键普及 h定位 o 鼠标中键 切换视图 鼠标左键移动视图 坐标轴反了&#xff0c;按w切换 alt左键 旋转 alt中键移动 alt右键 缩放 导入超大模型 导入后什么都看不到需要在工程属性里面修改为极大 image.pngshiftf2弹出材质编辑器 点四条杠可以移动选项卡 image.png点新建材质之…

基于GUI界面的yolov5人脸口罩检测项目

文章目录 前言 一、运行环境 二、环境配置 三、yolov5网络结构图介绍 四、 损失函数 五、数据集 六、实验内容 1.实验框架 2.实验环境 3.实验结果 前言 佩戴口罩可以有效降低在和感染者有接触时可能被感染者感染的风险。目前&#xff0c;在一些公共场所&#xff0c…

c++结构体数组sort排序出错?(关于sort排序comp比较器的严格弱排序性质)

文章目录sort的严格弱排序的性质无效的比较器&#xff08;Invalid comparator&#xff09;正确的比较器sort的严格弱排序的性质 我在给结构体数组排序的时候&#xff0c;自定义了sort函数的排序法则&#xff0c;我的结构体如下定义&#xff1a; struct score {int a, b, c;//…

English Learning - L1-8 时态(上) 2022.12.29 周四

English Learning - L1-8 时态&#xff08;上&#xff09; 2022.12.29 周四8 时态为什么时态难学&#xff1f;什么是时态&#xff1f;如何套用时态表8.1 一般时态核心思维&#xff08;一&#xff09; 一般现在时核心思维用法1. 普遍的事实和真理2. 重复活动&#xff08;习惯&am…

166页7万字智慧工厂可视化解决方案

【版权声明】本资料来源网络&#xff0c;仅用于行业知识分享&#xff0c;供个人学习参考&#xff0c;请勿商用。【侵删致歉】如有侵权请联系小编&#xff0c;将在收到信息后第一时间进行删除&#xff01;完整资料领取见文末&#xff0c;部分资料内容&#xff1a; 第 一 章 应用…

QT VS移植过程中出现的问题以及解决记录

目录 一、无法定位程序输入点于动态链接库 二、E1696 无法打开 源 文件 “QString“ 三、编译Qt项目提示 error MSB6006: “cmd.exe”已退出 四、禁止显示状态 错误 MSB8036 找不到 Windows SDK 五、E2512 功能测试宏的参数必须是简单标识符 六、Qt VS中双击ui文件无法打…

【计算机模型机设计】单周期MIPS CPU设计报告

2022年结束了&#xff0c;过去一年确实发生了很多事情&#xff0c;开心的、伤心的、激动的、平凡的…这些都已经成为过去了&#xff0c;只希望在新的一年里&#xff0c;能够多一些开心&#xff0c;少一些emo&#xff0c;做一些自己喜欢的事情。 其实说实话&#xff0c;感觉我的…

Linux驱动之平台总线

文章目录前言一、什么是平台总线&#xff1f;二、平台总线相关的结构体三、平台总线的注册和注销四、总线平台的框架总结前言 一个完整的驱动程序总是由三部分组成&#xff1a;设备、总线、驱动 设备就好比我们的硬件&#xff1a;比如oled &#xff0c;那么总线就相当于iic总线…

Faster RCNN网络源码解读(Ⅲ) --- 如何搭建自己的数据集

目录 一、如何生成类似pascal voc一样结构的文件&#xff08;split_data.py&#xff09; 二、如何创建属于自己的数据集&#xff08;my_dataset.py&#xff09; 2.1 代码 2.2 代码解释 2.2.1 初始化函数__init__ 2.2.2 parse_xml_to_dict函数&#xff08;解析xml文件&…

35岁程序员还能找到工作吗?60%选择转岗项目经理!

我因为工作关系接触过大把的程序员朋友&#xff0c;知道他们都有35岁年龄焦虑&#xff0c;曾经他们在群里调侃程序员的尽头是开出租车和开饭店。毕竟一个公司的CTO只有一个&#xff0c;随着年龄的增长他们的体力和学新的技术都会明显吃力&#xff0c;慢慢就承受不起失业&#x…

[极客大挑战 2019]Havefun1、EasySQL(BUUCTF)

前言: 这篇文章还是是为了帮助一些 像我这样的菜鸟 找到简单的题解 今天是2022年的最后一天&#xff0c; 这一年我居然写了72篇文章 获得5枚勋章 还多了14个粉丝 好了不说了&#xff0c;上水题的题解 EasySQL题目描述 解题工具: 不需什么工具&#xff0c;如题名easy …

元宇宙产业委员会发布《第一届第六次主任委员会议公报》

2022年12月29日中国移动通信联合会元宇宙产业工作委员会第一届第六次主任委员会议书面召开&#xff0c;共同主席3人、学术指导3人、产业指导3人&#xff0c;联席主任委员3人、执行主任委员4人、常务副主任委员9家/人、副主任委员25家/人均以通信方式出席会议并表决相关事项。 …