【QT】windows下OpenSSL的使用,RSA加解密

设计需求

在QT端实现对字符串的加密与解密

前期准备

OpenSSL工具下载

本人采用Win64OpenSSL-1_1_1t.msi,百度网盘下载链接如下:

链接:https://pan.baidu.com/s/1vg4s_1JmCpa68TMc1F2gMw
提取码:u4js

安装可以参考大神的博文 OpenSSL安装参考链接,很详细,这边就不进行重复介绍。

OpenSSL工具使用

OpenSSL工具使用学习,可以参考大神的博客OpenSS使用参考链接

完成了前期准备,接下来我们开始QT端的OpenSSL使用

QT端需求实现

pro文件加库

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;
}


输出图片展示

输出图片

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

傻傻虎虎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值