一、下载openssl库源码,下载地址:https://ftp.openssl.org/source/
我下载的版本:
二、编译源码
1、解压源码:
tar zxvf openssl-1.1.1u.tar.gz
2、进入到解压后的文件夹中:
cd openssl-1.1.1u/
3、执行配置,生成makefile文件:
./config shared --prefix=/usr/local/openssl
4、编译:
make
5、安装
sudo make install
至此,编译完成!
三、测试rsa加密解密:
1、找到编译后的头文件和库文件,起目录为:/usr/local/openssl
将头文件和库文件拷贝到程序目录中:
2、将头文件和库文件添加项目中:
3、测试:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#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>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
//加密函数
QString rsa_pri_encrypt_base64(const QString &strClearData);
//解密函数
QString rsa_pub_decrypt_base64(const QString &strDecryptData);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString encrypt_str = rsa_pri_encrypt_base64("123abc");
QString decrypt_str = rsa_pub_decrypt_base64(encrypt_str);
qDebug() << "解析后:" << decrypt_str << endl;
}
MainWindow::~MainWindow()
{
delete ui;
}
//私钥加密
QString MainWindow::rsa_pri_encrypt_base64 (const QString& strClearData)
{
//私钥 长度为512 (使用自己生成的公秘钥)
char private_key[] = "-----BEGIN PRIVATE KEY-----\n"
"MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAypZS0ybGDs7WWDIs\n"
"bN9qNnojaHpQn6wiQf23U3Tm0pe1vCR803+gY3We7hBYfT7+uXK3pykQ8bIKnWdb\n"
"XmoddQIDAQABAkAFCE7uZ9wrqf+xGS35VmNxDv+qLL+r976yHPdc316MhKTi9zfS\n"
"9qAnilqB7LE6YedPpMMrKCK7AobBlTrzqF/RAiEA/KdNO+w7WDwYjC98ocOZqHLb\n"
"49AP85nQL1Hu0eUf1HsCIQDNRUG+W9/IIK3kJ76eVwfSkq+LhM85yEY3vVIVmXmK\n"
"zwIgT6KoLHgUaIZgsmcRYKeXmknB13ewX3ce8JthrFUNCFsCIHySNx+q6det3VnE\n"
"tO2qq1ijJF1rL8dFPze8cQAir5wJAiEAmMP+WBohdEzXQMFYFkaVCIyfB8Zs6Muv\n"
"cUsvIxOqW2o=\n"
"-----END PRIVATE KEY-----";
//将字符串键加载到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 MainWindow::rsa_pub_decrypt_base64(const QString& strDecryptData)
{
//公钥解密
char public_key[] = "-----BEGIN PUBLIC KEY-----\n"\
"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMqWUtMmxg7O1lgyLGzfajZ6I2h6UJ+s\n"\
"IkH9t1N05tKXtbwkfNN/oGN1nu4QWH0+/rlyt6cpEPGyCp1nW15qHXUCAwEAAQ==\n"\
"-----END PUBLIC KEY-----";
//将字符串键加载到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;
}