设计需求
在QT端实现对字符串的加密与解密
OpenSSL下载教程
本人采用 Win64OpenSSL-1_1_1t.msi,百度网盘下载链接
链接:https://pan.baidu.com/s/1vg4s_1JmCpa68TMc1F2gMw
提取码:u4js
OpenSSL安装参考链接
OpenSSL使用的参考链接
OpenSS使用参考链接
OpenSSL使用的参考链接
INCLUDEPATH += $$PWD/openssl/
win32 {
# LIBS += -lwsock32
LIBS += -L$$PWD/lib/ -llibeay32 -lssleay32
}
动态链接库下载
链接:https://pan.baidu.com/s/1mtmfps7Ob6oMfR0EwmwPSQ
提取码:ux57
主函数实现
公钥实现对字符串“123456”的加密,然后对加密的内容用 私钥进行解密
#include <QApplication>
#include "rsa.h"
#include "QDebug"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// MainWindow w;
// w.show();
QString strPlainData_before = "123456";
QString strPubKey = "-----BEGIN PUBLIC KEY-----\n"\
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCjBAChJDE8yhBSwsNmRzfDHoVo\n"\
"FIFjWICaXfwUU5HGyYnW3AgKnX+itPfWNNXnD4fjOoQv+5VlRUvHgeuFB07yngEG\n"\
"AH7WhCllPq1TvQWynQOqdx1Jx7H9w4YyW3IAASSHqQHzAMqSYy1zghcxjyI7NpXP\n"\
"A7iMmZ8U0GhgUjcd9wIDAQAB\n"\
"-----END PUBLIC KEY-----";
QString strPriKey = "-----BEGIN RSA PRIVATE KEY-----\n"\
"MIICXAIBAAKBgQCjBAChJDE8yhBSwsNmRzfDHoVoFIFjWICaXfwUU5HGyYnW3AgK\n"\
"nX+itPfWNNXnD4fjOoQv+5VlRUvHgeuFB07yngEGAH7WhCllPq1TvQWynQOqdx1J\n"\
"x7H9w4YyW3IAASSHqQHzAMqSYy1zghcxjyI7NpXPA7iMmZ8U0GhgUjcd9wIDAQAB\n"\
"AoGAf4kdGPUg1Gdd9/HKP9WOCchyJOiOBfRywNKw6hIrpbN9dKy2Wd+4wMoGb/7s\n"\
"LpbXAG6Chqu0yQM68z5wzaXXG/fPvUOvNcAxKpnSeK/guzNETwxoSRe5duc2o+iY\n"\
"m1qG7l6/tYsGZ/qzDHeNateMM4OpC4CobjOlKycpw7N6z6kCQQDQGOATh8B5UcG7\n"\
"ViWZ37Yc0K64BK1MwaoS+R33BUWY+UAL5x/im5NmEHVwBSqThyKBReaZxAfGtA0i\n"\
"1+AmITorAkEAyIp7N4gP7dbPmanR4dB/ca/y2b8YrFDKjolwceIJQPp/8zEPv28V\n"\
"q7032u37UIytQI0CiwnPJw0gnFwBliMBZQJAKeG3x3Z88G3Z9eBCtRNnTalaYMLg\n"\
"NiCMIEFvHHY9KaqQj1S3AB6breEGIcOIbCS4dxggbzYa8ozuT+Luw/3P/QJBAK5c\n"\
"6aFw5hivizVaGCi09fpNN2Pn6XX5kQS5r1D1bYJXy2QRplWWKkt2a9gozzhL+F+F\n"\
"u05Uqw6TNWV+AcseiJkCQE/LuxFAbIdmXLRDLZ9F8q6YB0sJJ/Xq0JqhHwusrEy9\n"\
"sYzz9JVuLDZHDsmd8zlm593IVZYmz2Mj4Qqw+KZ0PF4=\n"\
"-----END RSA PRIVATE KEY-----";
QString strEncryptData ;
QString strPlainData ;
rsa* m_rsa=new rsa();
//加密
strEncryptData = m_rsa->rsaPubEncrypt(strPlainData_before,strPubKey);
qDebug()<<strPlainData_before;
qDebug()<<strEncryptData;
//解密
strPlainData = m_rsa->rsaPriDecrypt(strEncryptData, strPriKey);
qDebug()<<strPlainData;
return a.exec();
}
加密解密的函数
rsa.h
#ifndef RSA_H
#define RSA_H
#include <QString>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#define BEGIN_RSA_PUBLIC_KEY "BEGIN RSA PUBLIC KEY"
#define BEGIN_RSA_PRIVATE_KEY "BEGIN RSA PRIVATE KEY"
#define BEGIN_PUBLIC_KEY "BEGIN PUBLIC KEY"
#define BEGIN_PRIVATE_KEY "BEGIN PRIVATE KEY"
#define KEY_LENGTH 1024
class rsa
{
public:
rsa();
~rsa();
bool createRSAKey(QString &strPubKey, QString &strPriKey);
QString rsaPubEncrypt(const QString &strPlainData, const QString &strPubKey);
QString rsaPriDecrypt(const QString &strDecryptData, const QString &strPriKey);
};
#endif // RSA_H
rsa.cpp
#include <QDebug>
#include "rsa.h"
#define myDebugMsg(msg) qDebug()<<QString("[Debug] File:%1 Line:%2 Function:%3 Msg:%4").arg(__FILE__).arg(__LINE__).arg(__FUNCTION__).arg(msg)
#define myDebug qDebug()<<QString("[Debug] File:%1 Line:%2 Function:%3").arg(__FILE__).arg(__LINE__).arg(__FUNCTION__)
rsa::rsa()
{
}
rsa::~rsa()
{
}
bool rsa::createRSAKey(QString &strPubKey, QString &strPriKey)
{
RSA *pRsa = RSA_generate_key(KEY_LENGTH, RSA_3, NULL, NULL);
if (!pRsa){
return false;
}
BIO *pPriBio = BIO_new(BIO_s_mem());
PEM_write_bio_RSAPrivateKey(pPriBio, pRsa, NULL, NULL, 0, NULL, NULL);
BIO *pPubBio = BIO_new(BIO_s_mem());
PEM_write_bio_RSAPublicKey(pPubBio, pRsa);
//获取长度
size_t nPriKeyLen = BIO_pending(pPriBio);
size_t nPubKeyLen = BIO_pending(pPubBio);
//密钥对读取到字符串
char* pPriKey = new char[nPriKeyLen];
char* pPubKey = new char[nPubKeyLen];
BIO_read(pPriBio, pPriKey, nPriKeyLen);
BIO_read(pPubBio, pPubKey, nPubKeyLen);
//存储密钥对
strPubKey = QByteArray(pPubKey, nPubKeyLen);
strPriKey = QByteArray(pPriKey, nPriKeyLen);
//内存释放
RSA_free(pRsa);
BIO_free_all(pPriBio);
BIO_free_all(pPubBio);
delete pPriKey;
delete pPubKey;
return true;
}
//公钥加密
QString rsa::rsaPubEncrypt(const QString &strPlainData, const QString &strPubKey)
{
QByteArray pubKeyArry = strPubKey.toUtf8();
uchar* pPubKey = (uchar*)pubKeyArry.data();
BIO* pKeyBio = BIO_new_mem_buf(pPubKey, pubKeyArry.length());
if (pKeyBio == NULL) {
return "";
}
RSA* pRsa = RSA_new();
if (strPubKey.contains(BEGIN_RSA_PUBLIC_KEY)) {
pRsa = PEM_read_bio_RSAPublicKey(pKeyBio, &pRsa, NULL, NULL);
}
else {
pRsa = PEM_read_bio_RSA_PUBKEY(pKeyBio, &pRsa, NULL, NULL);
}
if (pRsa == NULL) {
BIO_free_all(pKeyBio);
return "";
}
int nLen = RSA_size(pRsa);
char* pEncryptBuf = new char[nLen];
memset(pEncryptBuf, 0, nLen);
//加密
QByteArray plainDataArry = strPlainData.toUtf8();
int nPlainDataLen = plainDataArry.length();
uchar* pPlainData = (uchar*)plainDataArry.data();
int nSize = RSA_public_encrypt(nPlainDataLen,
pPlainData,
(uchar*)pEncryptBuf,
pRsa,
RSA_PKCS1_PADDING);
QString strEncryptData = "";
if (nSize >= 0) {
QByteArray arry(pEncryptBuf, nSize);
strEncryptData = arry.toBase64();
}
//释放内存
delete pEncryptBuf;
BIO_free_all(pKeyBio);
RSA_free(pRsa);
return strEncryptData;
}
//私钥解密
QString rsa::rsaPriDecrypt(const QString &strDecryptData, const QString &strPriKey)
{
QByteArray priKeyArry = strPriKey.toUtf8();
uchar* pPriKey = (uchar*)priKeyArry.data();
BIO* pKeyBio = BIO_new_mem_buf(pPriKey, priKeyArry.length());
if (pKeyBio == NULL) {
return "";
}
RSA* pRsa = RSA_new();
pRsa = PEM_read_bio_RSAPrivateKey(pKeyBio, &pRsa, NULL, NULL);
if (pRsa == NULL) {
BIO_free_all(pKeyBio);
return "";
}
int nLen = RSA_size(pRsa);
char* pPlainBuf = new char[nLen];
memset(pPlainBuf, 0, nLen);
//解密
QByteArray decryptDataArry = strDecryptData.toUtf8();
decryptDataArry = QByteArray::fromBase64(decryptDataArry);
int nDecryptDataLen = decryptDataArry.length();
uchar* pDecryptData = (uchar*)decryptDataArry.data();
int nSize = RSA_private_decrypt(nDecryptDataLen,
pDecryptData,
(uchar*)pPlainBuf,
pRsa,
RSA_PKCS1_PADDING);
QString strPlainData = "";
if (nSize >= 0) {
strPlainData = QByteArray(pPlainBuf, nSize);
}
//释放内存
delete pPlainBuf;
BIO_free_all(pKeyBio);
RSA_free(pRsa);
return strPlainData;
}