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 
#include 
#include 
#include 
#include 

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 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 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.运行结果如下: 

Qt RSA OpenSSL C++ Qt加密解密签字通信系统窗体源码_第1张图片

三、在线协助: 

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

你可能感兴趣的:(C++,c++,开发语言,qt)