QT使用OpenSSL的DES/CBC/PKCS7加密字符串

首先安装Openssl,然后QT项目的pro文件中添加引用

 

加密方法大部分来自:C++调用openssl实现DES加密解密cbc模式 zeropadding填充方式 pkcs5padding填充方式 pkcs7padding填充方式 - osc_pyxgl9fl的个人空间 - OSCHINA - 中文开源技术交流社区

但是加密出来的内容未转码,显示的是乱码,后台需要的是HEX,增加加密后字符串转换,根据自己的需求修改部分代码。main.cpp 代码如下

#include <battery.h>
#include <QGuiApplication>
#include <QQmlApplicationEngine>

#include <QLibrary>
#include <QLocale>
#include <QTranslator>

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

#include <openssl/des.h>


//加密 cbc pkcs5padding 本身实现  //pkcs7padding 跟 pkcs5padding是同样的
std::string des_cbc_pkcs5_encrypt(const std::string &clearText)
{
    static unsigned char cbc_iv[8] = {'p', 'a', 's', 's', 'w', 'o', 'r', 'd'};
    //初始化IV向量【我这边密码与IV相同】
    std::string strCipherText;
    DES_cblock keyEncrypt, ivec;

    memcpy(keyEncrypt, cbc_iv, sizeof(cbc_iv));

    DES_key_schedule keySchedule;  //密钥表
    DES_set_key_unchecked(&keyEncrypt, &keySchedule);   //设置密钥,且不检测密钥奇偶性

    memcpy(ivec, cbc_iv, sizeof(cbc_iv));

    // 循环加密,每8字节一次
    const_DES_cblock inputText;
    DES_cblock outputText;
    std::vector<unsigned char> vecCiphertext;
    unsigned char tmp[8];

    for (int i = 0; i < clearText.length() / 8; i++)
    {
        memcpy(inputText, clearText.c_str() + i * 8, 8);
        DES_ncbc_encrypt(inputText, outputText, 8, &keySchedule, &ivec, DES_ENCRYPT);  //加密
        memcpy(tmp, outputText, 8);

        for (int j = 0; j < 8; j++)
            vecCiphertext.push_back(tmp[j]);

        //重置ivec
        memcpy(ivec, outputText, 8);
    }

    if (clearText.length() % 8 != 0)
    {
        int tmp1 = clearText.length() / 8 * 8;
        int tmp2 = clearText.length() - tmp1;
        memset(inputText,(8-tmp2), 8);
        memcpy(inputText, clearText.c_str() + tmp1, tmp2);
    }
    else
    {
        memset(inputText,8, 8);
    }
    // 加密函数
    DES_ncbc_encrypt(inputText, outputText, 8, &keySchedule, &ivec, DES_ENCRYPT);  //加密

    memcpy(tmp, outputText, 8);

    for (int j = 0; j < 8; j++)
        vecCiphertext.push_back(tmp[j]);

    strCipherText.clear();
    strCipherText.assign(vecCiphertext.begin(), vecCiphertext.end());
    return strCipherText;
}

//解密 cbc pkcs5padding 本身实现  //zeropadding / pkcs7padding 跟 pkcs5padding是同样的
std::string des_cbc_pkcs5_decrypt(const std::string &cipherText)
{
    static unsigned char cbc_iv[8] = {'p', 'a', 's', 's', 'w', 'o', 'r', 'd'};
    //初始化IV向量
    std::string clearText;
    DES_cblock keyEncrypt, ivec;

    memcpy(keyEncrypt, cbc_iv, sizeof(cbc_iv));

    DES_key_schedule keySchedule;  //密钥表
    DES_set_key_unchecked(&keyEncrypt, &keySchedule);   //设置密钥,且不检测密钥奇偶性

    memcpy(ivec, cbc_iv, sizeof(cbc_iv));

    // 循环解密,每8字节一次
    const_DES_cblock inputText;
    DES_cblock outputText;
    std::vector<unsigned char> vecCleartext;
    unsigned char tmp[8];

    for (int i = 0; i < cipherText.length() / 8; i++)
    {
        memcpy(inputText, cipherText.c_str() + i * 8, 8);
        DES_ncbc_encrypt(inputText, outputText, 8, &keySchedule, &ivec, DES_DECRYPT);  //解密
        memcpy(tmp, outputText, 8);

        for (int j = 0; j < 8; j++)
            vecCleartext.push_back(tmp[j]);

        //重置ivec
        //memcpy(ivec, outputText, 8);  //解密过程不须要用前一块的结果做为下一块的IV
    }

    if (clearText.length() % 8 != 0)
    {
        int tmp1 = clearText.length() / 8 * 8;
        int tmp2 = clearText.length() - tmp1;
        memset(inputText,0, tmp2);
        memcpy(inputText, cipherText.c_str() + tmp1, tmp2);
        DES_ncbc_encrypt(inputText, outputText, tmp2, &keySchedule, &ivec, DES_DECRYPT);  //解密
        memcpy(tmp, outputText, tmp2);
        for (int j = 0; j < 8; j++)
            vecCleartext.push_back(tmp[j]);
    }
    clearText.clear();
    clearText.assign(vecCleartext.begin(), vecCleartext.end());
    return clearText;
}

// 加密后密文转hex
string StringToHex(const std::string& data)
{
    const std::string hex = "0123456789ABCDEF";
    std::stringstream ss;

    for (std::string::size_type i = 0; i < data.size(); ++i)
        ss << hex[(unsigned char)data[i] >> 4] << hex[(unsigned char)data[i] & 0xf];
    std::cout << ss.str() << std::endl;
    return ss.str();
}

//---------------------以上代码可以写在一个单独的头文件中---------------------------------

int main(int argc, char *argv[])
{
    #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
    QGuiApplication app(argc, argv);
    {
        string result = des_cbc_pkcs5_encrypt("123456");
        string hexResult = StringToHex(result);
        qDebug() << "---------hex---------- " << QString::fromStdString(hexResult);

    }

    QTranslator translator;
    const QStringList uiLanguages = QLocale::system().uiLanguages();
    for (const QString &locale : uiLanguages) {
        const QString baseName = "DemoApp_" + QLocale(locale).name();
        if (translator.load(":/i18n/" + baseName)) {
            app.installTranslator(&translator);
            break;
        }
    }

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);


    return app.exec();
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AES/CBC/PKCS7Padding是一种常用的加密算法,可用于对数据进行保护。在C语言中,可以通过使用OpenSSL库来实现该算法。 下面是一个使用AES/CBC/PKCS7Padding加密算法的示例代码: ```c #include <openssl/aes.h> #include <openssl/rand.h> #define AES_BLOCK_SIZE 16 void encrypt(const unsigned char *plaintext, int plaintext_len, const unsigned char *key, const unsigned char *iv, unsigned char *ciphertext) { AES_KEY aesKey; AES_set_encrypt_key(key, 128, &aesKey); AES_cbc_encrypt(plaintext, ciphertext, plaintext_len, &aesKey, iv, AES_ENCRYPT); } void decrypt(const unsigned char *ciphertext, int ciphertext_len, const unsigned char *key, const unsigned char *iv, unsigned char *plaintext) { AES_KEY aesKey; AES_set_decrypt_key(key, 128, &aesKey); AES_cbc_encrypt(ciphertext, plaintext, ciphertext_len, &aesKey, iv, AES_DECRYPT); } int main() { unsigned char key[AES_BLOCK_SIZE] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}; unsigned char iv[AES_BLOCK_SIZE]; unsigned char plaintext[32] = "Hello, AES/CBC/PKCS7Padding"; unsigned char ciphertext[sizeof(plaintext)]; unsigned char decryptedtext[sizeof(plaintext)]; RAND_bytes(iv, sizeof(iv)); encrypt(plaintext, sizeof(plaintext), key, iv, ciphertext); decrypt(ciphertext, sizeof(ciphertext), key, iv, decryptedtext); printf("Original Text: %s\n", plaintext); printf("Encrypted Text: %s\n", ciphertext); printf("Decrypted Text: %s\n", decryptedtext); return 0; } ``` 以上代码使用OpenSSL库中的AES函数,分别实现了加密和解密的功能。主函数中生成了一个16字节的密钥和随机生成的16字节初始化向量。然后将明文进行加密,并将密文进行解密,最后打印出原始文本、加密文本和解密文本。 需要注意的是,加密后生成的密文长度要比原始明文长度稍长,因此ciphertext的长度使用了sizeof(plaintext)。为了避免截断问题,推荐使用静态大小的缓冲区来存储密文。 这只是一个简单的示例,实际应用中还需考虑安全性、密钥管理等因素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值