qt MD5 和AES 加密

一 、MD5 加密

#include <QCryptographicHash>


// MD5 加密
QString Dialog_login::MD5_encryption(const QString &data)
{
    QCryptographicHash md5(QCryptographicHash::Md5);    //使用MD5加密
    md5.addData(data.toUtf8(),data.size());   // 添加数据
    QByteArray bArry = md5.result();    //获取MD5加密后的密码
    QString md5_date;
    md5_data.append(bArry.toHex());

    return md5_data;
}

二、AES 加密和解密

1、下载库:https://github.com/bricke/Qt-AES

     下载后需要下面5个文件,把这些文件复制出来放到一个文件夹下:

 2、 qt 工程添加这些文件

 3. 编写加密解密代码

#include "aesni/qaesencryption.h"


// 加密
QString Dialog_login::AES_encryption(const QString &data, const QString &key)
{
    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::PKCS7);
    QByteArray enBA = encryption.encode(data.toUtf8(), key.toUtf8());
    return QString::fromLatin1(enBA.toBase64());
}

// 解密
QString Dialog_login::AES_decryption(const QString &data, const QString &key)
{
    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::PKCS7);
    QByteArray  enBA = QByteArray::fromBase64(data.toUtf8());
    QByteArray deBA = encryption.decode(enBA, key.toUtf8());
    return QString::fromLatin1(QAESEncryption::RemovePadding(deBA, QAESEncryption::PKCS7));
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值