Qt + AES加密解密

环境

Qt 5.4.2
Qt-AES

功能

Qt-ASE 利用本机Mac信息,加密解密文本内容

Qt-ASE

下载Qt-ASE,并拷贝相关文件
在这里插入图片描述

源码

  1. Pro文件
    在这里插入图片描述
  2. mainwindow.h
#include "qaesencryption.h"

struct Tag_CERTINFO
{
    char key[256];
    char iv[256];
    char doc[256];

    Tag_CERTINFO()
    {
        memset(key,'\0',256);
        memset(iv,'\0',256);
        memset(doc,'\0',256);
    }
};
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
private:
    void InitCert();
    QStringList GetNetworkNames();
    QByteArray AES_decryption(const QByteArray &data, const QString &key, const QByteArray &iv);
    QByteArray AES_encryption(const QByteArray &data, const QString &key, const QByteArray &iv);
private:
    Tag_CERTINFO tCertInf;
private slots:
    void on_pushButton_file_clicked();
    void on_pushButton_DMsg_clicked();
};


  1. mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    InitCert();

}
void MainWindow::InitCert()
{
    QStringList names = GetNetworkNames();

    if(names.count() >= 2)
    {
       QString key = names[0].replace("-","");
       key = key.mid(1,16);

       memcpy(tCertInf.key,key.toLocal8Bit().data(),key.toLocal8Bit().length());

       QString iv = names[1].replace("-","");
       iv = iv.mid(1,16);

       memcpy(tCertInf.iv,iv.toLocal8Bit().data(),iv.toLocal8Bit().length());
    }

    ui->lineEdit_EKey->setText(QString::fromLocal8Bit(tCertInf.key));
    ui->lineEdit_EIV->setText(QString::fromLocal8Bit(tCertInf.iv));
}

QStringList MainWindow::GetNetworkNames()
{
    QStringList names;
    names.clear();
    QList<QNetworkInterface> interfaces =  QNetworkInterface::allInterfaces();

    foreach (QNetworkInterface interface, interfaces)
    {
        if (!(interface.flags() & QNetworkInterface::IsLoopBack) && interface.isValid())
        {
            names.append(interface.name());
        }
    }
    return names;
}

QByteArray MainWindow::AES_encryption(const QByteArray& data,const QString& key,const QByteArray& iv)
{
    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::CBC, QAESEncryption::PKCS7);
    QByteArray enBA = encryption.encode(data, key.toUtf8(), iv);
    return enBA.toBase64();
}

QByteArray MainWindow::AES_decryption(const QByteArray& data,const QString& key,const QByteArray& iv)
{
    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::CBC, QAESEncryption::PKCS7);
    QByteArray  enBA = QByteArray::fromBase64(data);
    QByteArray deBA = encryption.decode(enBA, key.toUtf8(), iv);
    return QAESEncryption::RemovePadding(deBA, QAESEncryption::PKCS7);
}

void MainWindow::on_pushButton_file_clicked()
{
    QByteArray data = ui->lineEdit_EDoc->text().toUtf8();
    QString key = QString::fromLocal8Bit(tCertInf.key).toUtf8();
    QByteArray iv  = QString::fromLocal8Bit(tCertInf.iv).toUtf8();
    ui->lineEdit_EMsg->setText(QString(AES_encryption(data,key,iv)));
}

void MainWindow::on_pushButton_DMsg_clicked()
{
    QByteArray data = ui->lineEdit_Msg->text().toUtf8();
    QString key = QString::fromLocal8Bit(tCertInf.key).toUtf8();
    QByteArray iv  = QString::fromLocal8Bit(tCertInf.iv).toUtf8();
    ui->lineEdit_DDoc->setText(QString(AES_decryption(data,key,iv)));
}

运行

在这里插入图片描述
在这里插入图片描述

Qt是一种流行的跨平台应用程序开发框架,提供了各种功能丰富的类库和工具,包括AES加密和解密算法的支持。 AES(Advanced Encryption Standard)是一种对称加密算法,被广泛应用于保护数据的机密性和安全性。在Qt中,我们可以使用QCryptographic类库来实现AES加密和解密操作。 首先,我们需要在项目中包含QCryptographic库的头文件: #include <QCryptographicHash> 然后,使用该库中的相关方法来进行加密和解密操作。例如,使用AES-128加密算法: QString plaintext = "Hello, World!"; QString password = "SecretPassword"; // 将明文转换为字节数组 QByteArray plaintextBytes = plaintext.toUtf8(); // 创建AES加密对象 QAESEncryption aesEncrypt(QAESEncryption::AES_128, QAESEncryption::CBC); // 设置加密密码 aesEncrypt.setKey(password.toUtf8()); // 加密明文数据 QByteArray encryptedData = aesEncrypt.encode(plaintextBytes); // 将加密数据转换为十六进制字符串 QString encryptedText = encryptedData.toHex(); // 输出加密后的结果 qDebug() << "Encrypted text: " << encryptedText; 上述代码将明文字符串"Hello, World!"使用AES-128算法加密,加密密码为"SecretPassword",然后将加密后的数据转换为十六进制字符串并输出。 解密操作可以使用相同的加密密码和加密算法: // 将加密后的字符串转换为字节数组 QByteArray encryptedData = QByteArray::fromHex(encryptedText.toUtf8()); // 创建AES解密对象 QAESEncryption aesDecrypt(QAESEncryption::AES_128, QAESEncryption::CBC); // 设置解密密码 aesDecrypt.setKey(password.toUtf8()); // 解密数据 QByteArray decryptedData = aesDecrypt.decode(encryptedData); // 将解密后的字节数组转换为明文字符串 QString decryptedText = QString::fromUtf8(decryptedData); // 输出解密结果 qDebug() << "Decrypted text: " << decryptedText; 上述代码将加密后的十六进制字符串转换为字节数组,然后使用AES-128算法和密码"SecretPassword"进行解密操作,最后将解密后的字节数组转换为明文字符串并输出。 通过以上代码片段,我们可以在Qt中使用AES算法进行加密和解密操作,保护我们的数据的机密性和安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值