Qt RSA OpenSSL C++ Qt加密解密签字通信系统窗体源码
程序示例精选 Qt RSA OpenSSL C++ Qt加密解密签字通信系统窗体
前言
这篇博客针对<<Qt RSA OpenSSL C++ Qt加密解密签字通信系统窗体>>编写代码,主要功能包括了客户端,服务端,信息加密解密。代码整洁,规则,易读。 应用推荐首选。
文章目录
客户端代码
3. 服务端代码
4. 运行结果
三在线协助
一、所需工具软件
Visual Studio
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.客户端代码
代码如下(示例):
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("加签成功
");
if (RSAVerifyAction(strData, strSigned))
{
printf("验签成功
");
}
}
}
//发送
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
", strEncodeData.c_str());
printf("Encode data:%s
", strEncode.c_str());
printf("Decode data:%s
", 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.运行结果如下:
三、在线协助:
下一篇:
C语言中数组与指针的通俗理解
