diff --git a/DrCOM_JLU_Qt.pro b/DrCOM_JLU_Qt.pro index ee54714..7d98695 100644 --- a/DrCOM_JLU_Qt.pro +++ b/DrCOM_JLU_Qt.pro @@ -29,6 +29,7 @@ DEFINES += QT_DEPRECATED_WARNINGS CONFIG += c++11 SOURCES += \ + encrypt/EncryptData.cpp \ main.cpp \ mainwindow.cpp \ dogcomcontroller.cpp \ @@ -40,6 +41,7 @@ SOURCES += \ DogcomSocket.cpp HEADERS += \ + encrypt/EncryptData.h \ mainwindow.h \ dogcomcontroller.h \ constants.h \ @@ -68,6 +70,7 @@ DEFINES += QAPPLICATION_CLASS=QApplication VERSION = 1.0.0.6 win32:LIBS += -lwsock32 +win32:LIBS += -lcrypt32 # 更新日志: # v 0.0.0.0 实现基本功能 # v 1.0.0.1 修复适配高DPI时只窗口大小适配但字号不适配的bug diff --git a/constants.h b/constants.h index 66746b2..98df86e 100644 --- a/constants.h +++ b/constants.h @@ -56,7 +56,7 @@ enum { const int PORT_BIND = 61440; const int PORT_DEST = 61440; const QString SERVER_IP = "10.100.61.3"; -const QString SETTINGS_FILE_NAME = "DrCOM_JLU_Qt.ini"; +const QString SETTINGS_FILE_NAME = "DrCOM_JLU_Qt"; const QString ID_ACCOUNT = "account", ID_PASSWORD = "password", diff --git a/encrypt/EncryptData.cpp b/encrypt/EncryptData.cpp new file mode 100644 index 0000000..2ef5f27 --- /dev/null +++ b/encrypt/EncryptData.cpp @@ -0,0 +1,79 @@ +#include "EncryptData.h" + +#ifdef _WIN32 + +// 加密 +QByteArray EncryptWithWindowsDPAPI(const QByteArray& data) +{ + DATA_BLOB inBlob; + inBlob.pbData = (BYTE*)data.data(); + inBlob.cbData = data.size(); + + DATA_BLOB outBlob; + if (CryptProtectData(&inBlob, L"Password", nullptr, nullptr, nullptr, 0, &outBlob)) { + QByteArray result((char*)outBlob.pbData, outBlob.cbData); + LocalFree(outBlob.pbData); + return result; + } else { + return {}; + } +} + +// 解密 +QByteArray DecryptWithWindowsDPAPI(const QByteArray& encryptedData) +{ + DATA_BLOB inBlob; + inBlob.pbData = (BYTE*)encryptedData.data(); + inBlob.cbData = encryptedData.size(); + + DATA_BLOB outBlob; + if (CryptUnprotectData(&inBlob, nullptr, nullptr, nullptr, nullptr, 0, &outBlob)) { + QByteArray result((char*)outBlob.pbData, outBlob.cbData); + LocalFree(outBlob.pbData); + return result; + } else { + return {}; + } +} + +#else +QByteArray SimpleEncrypt(const QByteArray& data, const QByteArray& key = "SimpleKey") +{ + QByteArray ba = data; + for (int i = 0; i < ba.size(); ++i){ + ba[i] = ba[i] ^ key[i % key.size()]; + } + return ba; +} + +QByteArray SimpleDecrypt(const QByteArray& data, const QByteArray& key = "SimpleKey") +{ + QByteArray ba = data; + for (int i = 0; i < ba.size(); ++i){ + ba[i] = ba[i] ^ key[i % key.size()]; + } + return ba; +} +#endif + +// 包装为字符串操作 +QString EncryptString(const QString& password) +{ + #ifdef _WIN32 + QByteArray encrypted = EncryptWithWindowsDPAPI(password.toUtf8()); + #else + QByteArray encrypted = SimpleEncrypt(password.toUtf8()); + #endif + return encrypted.toBase64(); +} + +QString DecryptString(const QString& base64EncodedPassword) +{ + QByteArray decoded = QByteArray::fromBase64(base64EncodedPassword.toUtf8()); + #ifdef _WIN32 + QByteArray decrypted = DecryptWithWindowsDPAPI(decoded); + #else + QByteArray decrypted = SimpleDecrypt(decoded); + #endif + return QString::fromUtf8(decrypted); +} diff --git a/encrypt/EncryptData.h b/encrypt/EncryptData.h new file mode 100644 index 0000000..bf423e7 --- /dev/null +++ b/encrypt/EncryptData.h @@ -0,0 +1,23 @@ +#ifndef ENCRYPTDATA_H +#define ENCRYPTDATA_H + +#include +#include + +#ifdef _WIN32 +#include +#include +#endif + +#ifdef _WIN32 +QByteArray EncryptWithWindowsDPAPI(const QByteArray& data); +QByteArray DecryptWithWindowsDPAPI(const QByteArray& encryptedData); +#else +QByteArray SimpleEncrypt(const QByteArray& data, const QByteArray& key = "SimpleKey"); +QByteArray SimpleDecrypt(const QByteArray& data, const QByteArray& key = "SimpleKey"); +#endif + +QString EncryptString(const QString& password); +QString DecryptString(const QString& base64EncodedPassword); + +#endif // ENCRYPTDATA_H diff --git a/mainwindow.cpp b/mainwindow.cpp index fa8b839..f70e7d3 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -17,6 +17,8 @@ #include #include +#include "encrypt/EncryptData.h" + MainWindow::MainWindow(SingleApplication *parentApp, QWidget *parent) : QDialog(parent), ui(new Ui::MainWindow), @@ -159,7 +161,7 @@ void MainWindow::RestartDrcom() void MainWindow::QuitDrcom() { // 退出之前恢复重试计数 - QSettings s(SETTINGS_FILE_NAME, QSettings::IniFormat); + QSettings s(SETTINGS_FILE_NAME); s.setValue(ID_RESTART_TIMES, 0); qDebug() << "reset restartTimes"; qDebug() << "QuitDrcom"; @@ -201,14 +203,12 @@ void MainWindow::IconActivated(QSystemTrayIcon::ActivationReason reason) void MainWindow::LoadSettings() { QSettings s(SETTINGS_FILE_NAME); account = s.value(ID_ACCOUNT, "").toString(); - password = s.value(ID_PASSWORD, "").toString(); mac_addr = s.value(ID_MAC, "").toString(); bRemember = s.value(ID_REMEMBER, false).toBool(); bAutoLogin = s.value(ID_AUTO_LOGIN, false).toBool(); bHideWindow = s.value(ID_HIDE_WINDOW, false).toBool(); bNotShowWelcome = s.value(ID_NOT_SHOW_WELCOME, false).toBool(); ui->lineEditAccount->setText(account); - ui->lineEditPass->setText(password); SetMAC(mac_addr); if (bRemember) ui->checkBoxRemember->setCheckState(Qt::CheckState::Checked); @@ -226,15 +226,23 @@ void MainWindow::LoadSettings() { ui->checkBoxNotShowWelcome->setCheckState(Qt::CheckState::Checked); else ui->checkBoxNotShowWelcome->setCheckState(Qt::CheckState::Unchecked); + + // 密码加密处理 + QString encryptedPasswd = s.value(ID_PASSWORD, "").toString(); + password = DecryptString(encryptedPasswd); + ui->lineEditPass->setText(password); } void MainWindow::SaveSettings() { QSettings s(SETTINGS_FILE_NAME); s.setValue(ID_ACCOUNT, account); - s.setValue(ID_PASSWORD, password); s.setValue(ID_MAC, mac_addr); s.setValue(ID_REMEMBER, bRemember); s.setValue(ID_AUTO_LOGIN, bAutoLogin); + + // 密码加密处理 + QString enctyptedPasswd = EncryptString(password); + s.setValue(ID_PASSWORD, enctyptedPasswd); } void MainWindow::SetMAC(const QString &m)