Qt使用AES128加密一段文字实例
阅读原文时间:2021年04月23日阅读:1

本实例使用了现成的Qt库:https://github.com/bricke/Qt-AES

主要代码:

#include "mainwidget.h"
#include "ui_mainwidget.h"
#include "qaesencryption.h"
#include <QCryptographicHash>

MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{
    ui->setupUi(this);
}

MainWidget::~MainWidget()
{
    delete ui;
}

void MainWidget::on_buttonEncode_clicked()
{
    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::ZERO);
    QByteArray hashKey = QCryptographicHash::hash(ui->textKey->toPlainText().toUtf8(), QCryptographicHash::Md5);
    QByteArray encodedText = encryption.encode(ui->textOrigin->toPlainText().toUtf8(), hashKey);
    ui->textEncode->setText(QString::fromLatin1(encodedText.toBase64()));
}

void MainWidget::on_buttonDecode_clicked()
{
    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::ZERO);
    QByteArray hashKey = QCryptographicHash::hash(ui->textKey->toPlainText().toUtf8(), QCryptographicHash::Md5);
    QByteArray decodedText = encryption.decode(QByteArray::fromBase64(ui->textEncode->toPlainText().toLatin1()), hashKey);
    ui->textDecode->setText(QString::fromUtf8(decodedText));
}

运行效果:

(---------完--------)

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章