环境
Qt 5.4.2
Qt-AES
功能
Qt-ASE 利用本机Mac信息,加密解密文本内容
Qt-ASE
下载Qt-ASE,并拷贝相关文件
源码
- Pro文件
- 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();
};
- 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)));
}
运行