呼和浩特免费制作网站,网站上的网站地图怎么做,网站的做用,网络营销是什么专业一、说明 在Qt项目中简单的对数据进行加密解密#xff0c;有如下两种方式 1、QCryptographicHash Qt提供了用于加密的类QCryptographicHash#xff0c;但是QCryptographicHash类只有加密功能#xff0c;没有解密功能 2、Qt-AES 使用第三方AES库#xff0c;对数据进行加密解…一、说明
在Qt项目中简单的对数据进行加密解密有如下两种方式 1、QCryptographicHash Qt提供了用于加密的类QCryptographicHash但是QCryptographicHash类只有加密功能没有解密功能 2、Qt-AES 使用第三方AES库对数据进行加密解密 二、使用QCryptographicHash
新建一个Qt项目基类选择QMainWindow 在界面上拖拽如下两个控件并进行布局 更改.h代码 #ifndef MAINWINDOW_H
#define MAINWINDOW_H#include QMainWindownamespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent nullptr);~MainWindow();private slots:void on_pushButton_clicked();private:Ui::MainWindow *ui;
};#endif // MAINWINDOW_H 更改.cpp代码 #include mainwindow.h
#include ui_mainwindow.h
#include QCryptographicHash
#include QDebugMainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{ui-setupUi(this);
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::on_pushButton_clicked()
{QByteArray array;array.append(ui-lineEdit-text());QCryptographicHash hash(QCryptographicHash::Md5); //Md5加密hash.addData(array); //添加数据QByteArray retArray hash.result(); //加密后的数据qDebug() retArray.toHex(); //转化成16进制
} 运行随意输入一下数据如123abc点击pushButton 三、使用Qt-AES
访问下面的链接下载Qt-AES相关文件 https://github.com/bricke/Qt-AES 创建一个Qt项目基类选择“QMainWindow”把qaesencryption.h和qaesencryption.cpp两个文件添加到项目中 更改.h代码 #ifndef MAINWINDOW_H
#define MAINWINDOW_H#include QMainWindow
#include QCryptographicHash
#include QDebug
#include qaesencryption.hnamespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent nullptr);~MainWindow();private slots:QString encodedText(QString data, QString key); //加密QString decodedText(QString data, QString key); //解密private:Ui::MainWindow *ui;
};#endif // MAINWINDOW_H 更改.cpp代码 #include MainWindow.h
#include ui_MainWindow.hMainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{ui-setupUi(this);QString data qwer123456; //要加密的数据QString key 9876543; //密钥QString encoded encodedText(data, key); //加密QString decoded decodedText(encoded, key); //解密·qDebug() 源数据 data;qDebug() 加密 encoded;qDebug() 解密 decoded;
}MainWindow::~MainWindow()
{delete ui;
}//使用AES对数据进行加 密
QString MainWindow::encodedText(QString data, QString key)
{//密钥长度AES_128,加密方式ECB,填充方式ZEROQAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::ZERO);//使用QCryptographicHash对密钥进行加密QByteArray hashKey QCryptographicHash::hash(key.toUtf8(), QCryptographicHash::Sha1);//对源数据加密QByteArray encodedText encryption.encode(data.toUtf8(), hashKey);//QByteArray转QString (toBase64()不能去掉)QString encodeTextStr QString::fromLatin1(encodedText.toBase64());//qDebug() encodedText: encodeTextStr;return encodeTextStr;
}//使用AES对数据进行解密
QString MainWindow::decodedText(QString data, QString key)
{//密钥长度AES_128,加密方式ECB,填充方式ZEROQAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::ZERO);//使用QCryptographicHash对密钥进行加密QByteArray hashKey QCryptographicHash::hash(key.toUtf8(), QCryptographicHash::Sha1);//解密QByteArray decodedText encryption.decode(QByteArray::fromBase64(data.toLatin1()), hashKey);//QByteArray转QStringQString decodedTextStr QString::fromLatin1(decodedText);//qDebug()decodedText: decodedTextStr;return decodedTextStr;
} 运行测试 不加fromBase64、toBase64时