QT 使用 openssl,导入外部库

环境

win10 + QT 6.2.4 + Qt Creator 10.0.2 (Community) + Win64OpenSSL-3_1_2.exe

openssl 安装

Win32/Win64 OpenSSL Installer for Windows - Shining Light Productions (slproweb.com)

在这里插入图片描述

PS:注意安装目录最好不要有空格,否则待会qmake有可能会出问题

导入openssl库

  • 在项目上右键,选择添加库

在这里插入图片描述

  • 选择外部库

在这里插入图片描述

  • 选择openssl 安装目录下lib目录下的libcrypto.lib

在这里插入图片描述

  • 选择openssl 安装目录下lib目录下的libssl.lib

    在这里插入图片描述

  • 在构建中执行qmake

在这里插入图片描述

  • 在构建中执行重新构建

在这里插入图片描述

  • PS:在添加库的时候一定要去掉 为debug版本添加 'd的’后缀>,没有去掉会提示找不到库

AES (16 +CBC)加解密

aes.h

#ifndef AES_H
#define AES_H

#include <QByteArray>

class AES {
public:
    // 加密函数
    static QByteArray encrypt(const QByteArray &key, const QByteArray &iv, const QByteArray &data);
    // 解密函数
    static QByteArray decrypt(const QByteArray &key, const QByteArray &iv, const QByteArray &data);
private:
    // 对数据进行padding的函数
    static QByteArray pad(const QByteArray& data);
};

#endif // AES_H

aes.cpp

#include "AES.h"
#include "qdebug.h"
#include <openssl/aes.h>
#include <openssl/rand.h>
#include <string.h>
#include <QDebug>
// 对数据进行padding的函数实现
QByteArray AES::pad(const QByteArray& data) {
    int paddingLength =0;
    if((data.length() % 16)!=0)
    {
        paddingLength = 16 - (data.length() % 16);
    }
    QByteArray paddedData = data;
    for (int i = 0; i < paddingLength; i++) {
        paddedData.append((unsigned char)paddingLength);
    }
    return paddedData;
}
// 使用 OpenSSL 进行 AES 加密
QByteArray AES::encrypt(const QByteArray &key, const QByteArray &iv, const QByteArray &data) {
    AES_KEY encryptKey;
    // 设置密钥,这里假设密钥长度为128位(16字节)
    AES_set_encrypt_key(reinterpret_cast<const unsigned char*>(key.data()), 128, &encryptKey);
    QByteArray paddedData = pad(data);
    // 复制初始向量,因为加密后的数据会覆盖掉它
    unsigned char iv_copy[AES_BLOCK_SIZE];
    memcpy(iv_copy, iv.constData(), AES_BLOCK_SIZE);

    // 创建缓冲区存储加密后的数据
    unsigned char outbuf[1024];
    unsigned char ftr[AES_BLOCK_SIZE];
    unsigned len = paddedData.length();
    qDebug()<<len;
    unsigned char *in = (unsigned char*)malloc(len + AES_BLOCK_SIZE);

    // 将数据复制到输入缓冲区
    memcpy(in, paddedData.constData(), len);

    // 使用 OpenSSL 进行 AES 加密
    AES_cbc_encrypt(in, outbuf, len, &encryptKey, iv_copy,AES_ENCRYPT);

    // 将加密后的数据转换为 QByteArray 返回
    QByteArray output(reinterpret_cast<const char*>(outbuf), len);
    free(in);
    return output;
}

// 使用 OpenSSL 进行 AES 解密
QByteArray AES::decrypt(const QByteArray &key, const QByteArray &iv, const QByteArray &data) {
    AES_KEY decryptKey;
    // 设置密钥,这里假设密钥长度为128位(16字节)
    AES_set_decrypt_key(reinterpret_cast<const unsigned char*>(key.data()), 128, &decryptKey);

    // 复制初始向量,因为解密后的数据会覆盖掉它
    unsigned char iv_copy[AES_BLOCK_SIZE];
    memcpy(iv_copy, iv.constData(), AES_BLOCK_SIZE);

    // 创建缓冲区存储解密后的数据
    unsigned char outbuf[AES_BLOCK_SIZE];
    unsigned len = data.length();
    unsigned char *in = (unsigned char*)malloc(len + AES_BLOCK_SIZE);

    // 将数据复制到输入缓冲区
    memcpy(in, data.constData(), len);

    AES_cbc_encrypt(in, outbuf, len , &decryptKey, iv_copy,AES_DECRYPT);

    // 解密后的数据已经存储在 outbuf 中,我们只需要将其转换为 QByteArray 并返回即可
    QByteArray output(reinterpret_cast<const char*>(outbuf), len);
    free(in);
    return output;
}



main.cpp

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
    QString    key_str("11111111111111111111111111111111111");
	//将字符串转换成QByteArray
    QByteArray  key_array=QByteArray::fromHex(key_str.toLatin1());
    QString    iv_str("11111111111111111111111111111111111");
    QByteArray  iv_array=QByteArray::fromHex(iv_str.toLatin1());

    QString    pdata_str("11111111111111111111111111111111111");
    QByteArray  pdata_array=QByteArray::fromHex(pdata_str.toLatin1());
    QByteArray en_data = AES::encrypt(key_array,iv_array,pdata_array);
	//按照十六进制的字符串输出,tohex
    qDebug()<<en_data.toHex();

    QString    data_str("11111111111111111111111111111111111");
    QByteArray  data_array=QByteArray::fromHex(data_str.toLatin1());
    QByteArray de_data = AES::decrypt(key_array,iv_array,data_array);
	//按照十六进制的字符串输出,tohex
    qDebug()<<de_data.toHex();
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt使用OpenSSL,可以通过以下步骤实现: 1. 下载OpenSSL 可以从OpenSSL官网下载最新版本的OpenSSL,也可以使用系统自带的OpenSSL。如果是在Windows平台上,可以下载预编译的OpenSSL。 2. 配置Qt项目 在Qt项目中,需要在.pro文件中添加以下配置: ``` QT += network LIBS += -L/path/to/openssl/lib -lssl -lcrypto INCLUDEPATH += /path/to/openssl/include ``` 其中,/path/to/opensslOpenSSL的安装路径。通过上面的配置,可以将OpenSSL链接到Qt项目中。 3. 使用OpenSSLQt使用OpenSSL,可以参考以下示例代码: ```c++ #include <QSslSocket> #include <QSslCertificate> #include <QSslKey> int main() { // 初始化SSL QSslSocket::addDefaultCaCertificate(QSslCertificate::fromPath("ca.crt")); // 创建SSL套接字 QSslSocket socket; socket.setProtocol(QSsl::TlsV1_2); // 加载客户端证书和私钥 QSslCertificate clientCert("client.crt"); QSslKey clientKey("client.key", QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey, "password"); socket.setLocalCertificate(clientCert); socket.setPrivateKey(clientKey); // 连接服务器 socket.connectToHostEncrypted("server.com", 443); if (!socket.waitForConnected()) { qDebug() << "Error: " << socket.errorString(); return -1; } // 发送请求数据 const char *request = "Hello, world!"; socket.write(request, strlen(request)); socket.waitForBytesWritten(); // 读取响应数据 QByteArray response; while (socket.waitForReadyRead()) { response += socket.readAll(); } qDebug() << "Received: " << response; // 关闭套接字 socket.disconnectFromHost(); socket.waitForDisconnected(); return 0; } ``` 在上面的示例中,首先使用addDefaultCaCertificate函数添加了CA证书。然后创建了QSslSocket对象,并设置TLS 1.2协议。接着加载了客户端证书和私钥,并连接到服务器。在连接成功后,通过write函数发送请求数据,并通过waitForBytesWritten函数等待数据发送完成。然后通过waitForReadyRead函数等待服务器响应数据,并通过readAll函数读取响应数据。最后关闭套接字。 注意,在实际使用中,需要根据实际情况修改证书和私钥的文件名、地址和端口等参数。同时,还需要处理连接错误和读写错误等异常情况。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值