Qt實現(xiàn)數(shù)據(jù)進行加密、解密的步驟
一、說明
在Qt項目中簡單的對數(shù)據(jù)進行加密解密,有如下兩種方式
1、QCryptographicHash
Qt提供了用于加密的類QCryptographicHash,但是QCryptographicHash類只有加密功能,沒有解密功能
2、Qt-AES
使用第三方AES庫,對數(shù)據(jù)進行加密解密
二、使用QCryptographicHash
新建一個Qt項目,基類選擇QMainWindow,

在界面上拖拽如下兩個控件,并進行布局

更改.h代碼
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
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 <QDebug>
MainWindow::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); //添加數(shù)據(jù)
QByteArray retArray = hash.result(); //加密后的數(shù)據(jù)
qDebug() << retArray.toHex(); //轉(zhuǎn)化成16進制
}運行,隨意輸入一下數(shù)據(jù),如:123abc,點擊pushButton

三、使用Qt-AES
訪問下面的鏈接,下載Qt-AES相關(guān)文件
https://github.com/bricke/Qt-AES

創(chuàng)建一個Qt項目,基類選擇“QMainWindow”,把qaesencryption.h和qaesencryption.cpp兩個文件添加到項目中

更改.h代碼
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QCryptographicHash>
#include <QDebug>
#include "qaesencryption.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
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.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString data = "qwer123456"; //要加密的數(shù)據(jù)
QString key = "9876543"; //密鑰
QString encoded = encodedText(data, key); //加密
QString decoded = decodedText(encoded, key); //解密·
qDebug() << "源數(shù)據(jù):" << data;
qDebug() << "加密:" << encoded;
qDebug() << "解密:" << decoded;
}
MainWindow::~MainWindow()
{
delete ui;
}
//使用AES對數(shù)據(jù)進行加 密
QString MainWindow::encodedText(QString data, QString key)
{
//密鑰長度AES_128,加密方式ECB,填充方式ZERO
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::ZERO);
//使用QCryptographicHash對密鑰進行加密
QByteArray hashKey = QCryptographicHash::hash(key.toUtf8(), QCryptographicHash::Sha1);
//對源數(shù)據(jù)加密
QByteArray encodedText = encryption.encode(data.toUtf8(), hashKey);
//QByteArray轉(zhuǎn)QString (toBase64()不能去掉)
QString encodeTextStr = QString::fromLatin1(encodedText.toBase64());
//qDebug()<< "encodedText:"<< encodeTextStr;
return encodeTextStr;
}
//使用AES對數(shù)據(jù)進行解密
QString MainWindow::decodedText(QString data, QString key)
{
//密鑰長度AES_128,加密方式ECB,填充方式ZERO
QAESEncryption 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轉(zhuǎn)QString
QString decodedTextStr = QString::fromLatin1(decodedText);
//qDebug()<<"decodedText:"<< decodedTextStr;
return decodedTextStr;
}運行測試

不加fromBase64、toBase64時

到此這篇關(guān)于Qt實現(xiàn)數(shù)據(jù)進行加密、解密的步驟的文章就介紹到這了,更多相關(guān)Qt 加密解密內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言編程數(shù)據(jù)結(jié)構(gòu)線性表之順序表和鏈表原理分析
本篇文章是C語言編程篇主要為大家介紹了C語言編程中的數(shù)據(jù)結(jié)構(gòu)線性表,文中附含豐富的圖文示例代碼為大家詳解了線性表中的順序表和鏈表,有需要的朋友可以借鑒參考下2021-09-09
Java C++ 題解leetcode857雇傭K名工人最低成本vector pair
這篇文章主要為大家介紹了Java C++ 題解leetcode857雇傭K名工人最低成本vector pair示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09

