跳转到帖子
登录关注  
墨香年少

Qt使用OpenSSL加密解密数据

已推荐帖子

效果图:

openssl_demo.png

 

核心代码:

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/bn.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/ssl.h>
#include <openssl/err.h>

Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //读取公钥
    QFile public_file("C:\\Program Files (x86)\\OpenSSL\\bin\\rsa_public_key.pem");
    public_file.open(QIODevice::ReadOnly);
    __public_key = public_file.readAll();
    __public_key = __public_key.replace("\r\n","\n");
    public_file.close();

    //读取私钥
    QFile private_file("C:\\Program Files (x86)\\OpenSSL\\bin\\pkcs8_rsa_private_key.pem");
    private_file.open(QIODevice::ReadOnly);
    __private_key = private_file.readAll();
    __private_key = __private_key.replace("\r\n","\n");
    private_file.close();
}

Widget::~Widget()
{
    delete ui;
}

//加密
void Widget::on_btn_authcode_clicked()
{
    QString native_string = ui->edit_native_string->text();
    QString encrypt_str =  rsa_pri_encrypt_base64(native_string);
    ui->area_encrypt_str->setPlainText(encrypt_str);
}

//解密
void Widget::on_btn_decode_clicked()
{
    QString encrypt_str = ui->area_encrypt_str->toPlainText();
    QString decode_str = rsa_pub_decrypt_base64(encrypt_str);
    ui->edit_decode_str->setText(decode_str);
}

//私钥加密
QString Widget::rsa_pri_encrypt_base64 (const QString& strClearData)
{
    //私钥  长度为512  (使用自己生成的公秘钥)
    char*  private_key;
    QByteArray ba = __private_key.toLatin1();
    private_key=ba.data();

    //将字符串键加载到bio对象
    BIO* pKeyBio = BIO_new_mem_buf(private_key, strlen(private_key));
    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* pEncryptBuf = new char[nLen];
    memset(pEncryptBuf, 0, nLen);
    QByteArray clearDataArry = strClearData.toUtf8();
    int nClearDataLen = clearDataArry.length();
    uchar* pClearData = (uchar*)clearDataArry.data();
    int nSize = RSA_private_encrypt(nClearDataLen,pClearData,(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 Widget::rsa_pub_decrypt_base64(const QString& strDecryptData)
{

    //公钥解密
    char*  public_key;
    QByteArray ba = __public_key.toLatin1();
    public_key=ba.data();

    //将字符串键加载到bio对象
    BIO* pKeyBio = BIO_new_mem_buf(public_key, strlen(public_key));
    if (pKeyBio == NULL){
        return "";
    }

    RSA* pRsa = RSA_new();

    pRsa = PEM_read_bio_RSA_PUBKEY(pKeyBio, &pRsa, NULL, NULL);


    if ( pRsa == NULL ){
        BIO_free_all(pKeyBio);
        return "";
    }
    int nLen = RSA_size(pRsa);
    char* pClearBuf = new char[nLen];
    memset(pClearBuf, 0, nLen);
    //解密
    QByteArray decryptDataArry = strDecryptData.toUtf8();
    decryptDataArry = QByteArray::fromBase64(decryptDataArry);
    int nDecryptDataLen = decryptDataArry.length();
    uchar* pDecryptData = (uchar*)decryptDataArry.data();
    int nSize = RSA_public_decrypt(nDecryptDataLen,pDecryptData,(uchar*)pClearBuf,pRsa,RSA_PKCS1_PADDING);
    QString strClearData = "";
    if ( nSize >= 0 )
    {
        strClearData = QByteArray(pClearBuf, nSize);
    }

    // 释放内存
    delete[] pClearBuf;
    BIO_free_all(pKeyBio);
    RSA_free(pRsa);
    return strClearData;
}

 

Qt下的代码下载:

testOpenSSL.zip


目之所及,皆是回忆,心之所想,皆是过往

分享这篇帖子


链接帖子
分享到其他站点

生成共钥、私钥命令

.\openssl.exe genrsa -out rsa_private_key.pem 512
.\openssl.exe rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
.\openssl.exe pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt -out pkcs8_rsa_private_key.pem

 


目之所及,皆是回忆,心之所想,皆是过往

分享这篇帖子


链接帖子
分享到其他站点

创建帐户或登录来提出意见

你需要成为会员才能提出意见

创建帐户

注册成为会员。只要几个简单步骤!

注册帐户

登录

已有帐户? 请登录。

现在登录
登录关注  

×
×
  • 创建新的...

重要信息

注册必须使用2-8个中文汉字作为账号