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();
}

猜你喜欢

转载自blog.csdn.net/CHEN_ZL0125/article/details/124988785
今日推荐