搜索论坛
显示结果为标签'openssl'。
找到3个结果
-
效果图: 核心代码: #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
-
1.安装perl 2.下载openssl 3.管理员模式打开Native Tools Command Prompt for VS 2017 4.解压openssl到D:\openssl-1.1.1l 5.Native Tools Command Prompt for VS 2017下进入D:\openssl-1.1.1l 6.执行 perl Configure VC-WIN64A no-asm --prefix=d:/openssl 7.nmake 8.nmake install
-
http://slproweb.com/products/Win32OpenSSL.html 下载对应的exe安装文件,安装后将libcrypto-1_1.dll libssl-1_1.dll这两个dll复制到对用的位置 比如: D:\QT5\5.15.2\msvc2019_64\bin