📄 encrypt.cc
字号:
// head files#include <QFile>#include <QByteArray>#include <QTime>#include "encrypt.h"#include "aes.h"// function implementationsZT_Encrypt::ZT_Encrypt(QWidget* parent): QWidget(parent){ // initialize initUI(); initBinding();}ZT_Encrypt::~ZT_Encrypt(){ delete chkDelete; delete dlgSource; delete dlgDestination; delete lblSource; delete lblDestination; delete lblPassword; delete txtSource; delete txtDestination; delete txtPassword; delete msgMessage; delete btnEncrypt; delete btnAbout; delete btnDecrypt; delete btnBrowseSource; delete btnBrowseDestination;}void ZT_Encrypt::initUI(){ setWindowTitle(QObject::tr("ZT-Encrypt")); setFixedSize(480, 210); lblSource = new QLabel(QObject::tr("Source"), this); lblSource->setGeometry(QRect(20, 20, 100, 25)); lblDestination = new QLabel(QObject::tr("Destination"), this); lblDestination->setGeometry(QRect(20, 55, 100, 25)); lblPassword = new QLabel(QObject::tr("Password"), this); lblPassword->setGeometry(QRect(20, 90, 100, 25)); chkDelete = new QCheckBox(QObject::tr("Delete"), this); chkDelete->setGeometry(QRect(20, 125, 200, 25)); btnEncrypt = new QPushButton(QObject::tr("Encrypt"), this); btnEncrypt->setGeometry(QRect(40, 160, 80, 30)); btnAbout = new QPushButton(QObject::tr("About"), this); btnAbout->setGeometry(QRect(280, 160, 80, 30)); btnDecrypt = new QPushButton(QObject::tr("Decrypt"), this); btnDecrypt->setGeometry(QRect(160, 160, 80, 30)); btnBrowseSource = new QPushButton(QObject::tr("Browse"), this); btnBrowseSource->setGeometry(QRect(380, 20, 80, 25)); btnBrowseDestination = new QPushButton(QObject::tr("Browse"), this); btnBrowseDestination->setGeometry(QRect(380, 55, 80, 25)); txtSource = new QLineEdit(this); txtSource->setGeometry(QRect(120, 20, 240, 25)); txtDestination = new QLineEdit(this); txtDestination->setGeometry(QRect(120, 55, 240, 25)); txtPassword = new QLineEdit(this); txtPassword->setGeometry(QRect(120, 90, 240, 25)); msgMessage = new QMessageBox(); msgMessage->setWindowTitle(QObject::tr("Info")); msgMessage->setWindowModality(Qt::ApplicationModal); dlgSource = new QFileDialog(); dlgSource->setWindowTitle(QObject::tr("Open")); dlgSource->setWindowModality(Qt::ApplicationModal); dlgDestination = new QFileDialog(); dlgDestination->setWindowModality(Qt::ApplicationModal);}void ZT_Encrypt::initBinding(){ QObject::connect(btnBrowseSource, SIGNAL(clicked()), this, SLOT(showBrowseSource())); QObject::connect(btnBrowseDestination, SIGNAL(clicked()), this, SLOT(showBrowseDestination())); QObject::connect(dlgSource, SIGNAL(filesSelected(const QStringList&)), this, SLOT(setSource(const QStringList&))); QObject::connect(dlgDestination, SIGNAL(filesSelected(const QStringList&)), this, SLOT(setDestination(const QStringList&))); QObject::connect(btnEncrypt, SIGNAL(clicked()), this, SLOT(encrypt())); QObject::connect(btnDecrypt, SIGNAL(clicked()), this, SLOT(decrypt()));}void ZT_Encrypt::showBrowseSource(){ dlgSource->show();}void ZT_Encrypt::showBrowseDestination(){ dlgDestination->show();}void ZT_Encrypt::setSource(const QStringList& path){ txtSource->setText(path.at(0));}void ZT_Encrypt::setDestination(const QStringList& path){ txtDestination->setText(path.at(0));}void ZT_Encrypt::encrypt(){ doEncryption(0);}void ZT_Encrypt::decrypt(){ doEncryption(1);}void ZT_Encrypt::doEncryption(int op){ // check if op is valid if (op != 0 && op != 1) { return ; } // check if we have the password if (txtPassword->text().length() == 0) { return ; } // open source file QFile src(txtSource->text()); if (!src.exists()) { // the specified source file doesn't exist return ; } src.open(QIODevice::ReadWrite); // open destination file QFile dst(txtDestination->text()); if (dst.exists()) { // warn the user that the specified destination file being overwritten } dst.open(QIODevice::WriteOnly); // prepare the key and iv QByteArray tmp_pass = txtPassword->text().toLatin1(); char* data = tmp_pass.data(); int len = tmp_pass.length(); int temp = 0; AES_KEY aes_key; unsigned char key[16] = {0}; for (int i = 0; i < len; i++) { temp += (data[i] << i); } qsrand(temp); for (int i = 0; i < 16; i++) { key[i] = qrand(); } char iv[16] = {0}; temp = 0; for (int i = len - 1; i >= 0; i--) { temp += (data[i] << i); } qsrand(temp); for (int i = 0; i < 16; i++) { iv[i] = qrand(); } // do the encryption char in[512]; char out[512]; qint64 size = src.size(); qint64 read = 0; qint64 tmp = 0; while (read < size) { tmp = src.read(in, 512); if (op == 0) { // encrypt AES_set_encrypt_key(key, 128, &aes_key); AES_cbc_encrypt((unsigned char*)in, (unsigned char*)out, tmp, &aes_key, (unsigned char*)iv, AES_ENCRYPT); } else { // decrypt AES_set_decrypt_key(key, 128, &aes_key); AES_cbc_encrypt((unsigned char*)in, (unsigned char*)out, tmp, &aes_key, (unsigned char*)iv, AES_DECRYPT); } dst.write(out, tmp); read += tmp; } // delete source if checked if (chkDelete->isChecked()) { qint64 size = src.size(); char buffer[4096]; for (int i = 0; i < 4; i++) { qsrand(QTime::currentTime().msec()); for (int k = 0; k < 4096; k++) { buffer[k] = qrand(); } int j = 0; while (j < size) { src.write(buffer, 1024); j += 1024; } } src.resize(qrand()); src.remove(); } // show the message of job done if (op == 0) { msgMessage->setText(QObject::tr("EncryptComplete")); } else { msgMessage->setText(QObject::tr("DecryptComplete")); } msgMessage->show();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -