From 4e4f1a40b33648ca2192afec59201819c65415b4 Mon Sep 17 00:00:00 2001 From: KnCRJNET Date: Sun, 25 May 2025 00:15:37 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=AF=B9=E5=AF=86=E7=A0=81=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E4=BA=86=E5=8A=A0=E5=AF=86=E5=AD=98=E5=82=A8=EF=BC=8C?= =?UTF-8?q?Windows=E5=B9=B3=E5=8F=B0=E4=B8=8A=E9=87=87=E7=94=A8=20Windows?= =?UTF-8?q?=20=E6=8F=90=E4=BE=9B=E7=9A=84=20=E6=95=B0=E6=8D=AE=E4=BF=9D?= =?UTF-8?q?=E6=8A=A4=20API=EF=BC=88DPAPI=EF=BC=89=EF=BC=8C=E4=BF=9D?= =?UTF-8?q?=E6=8A=A4=E4=BB=85=E5=BD=93=E5=89=8D=E8=B4=A6=E6=88=B7=E8=83=BD?= =?UTF-8?q?=E5=A4=9F=E8=A7=A3=E5=AF=86=E6=95=B0=E6=8D=AE=EF=BC=8C=E5=85=B6?= =?UTF-8?q?=E4=BB=96=E5=B9=B3=E5=8F=B0=E6=9A=82=E6=97=B6=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E7=AE=80=E5=8D=95=E7=9A=84XOR=E5=8A=A0=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrCOM_JLU_Qt.pro | 3 ++ encrypt/EncryptData.cpp | 79 +++++++++++++++++++++++++++++++++++++++++ encrypt/EncryptData.h | 23 ++++++++++++ mainwindow.cpp | 14 ++++++-- 4 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 encrypt/EncryptData.cpp create mode 100644 encrypt/EncryptData.h 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/encrypt/EncryptData.cpp b/encrypt/EncryptData.cpp new file mode 100644 index 0000000..4d57cd7 --- /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(password.toUtf8()); + #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..57e6b77 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), @@ -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) From 5f58d49b6fa778c7fb5d19d6ae86ab2d0aafcf67 Mon Sep 17 00:00:00 2001 From: KnCRJNET Date: Sat, 24 May 2025 22:41:56 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=B0=86QSettings=E7=9A=84=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E5=9D=87=E6=94=B9=E4=B8=BA=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E6=9E=84=E9=80=A0=EF=BC=8C=E4=BF=AE=E5=A4=8D=E4=BA=86=E9=83=A8?= =?UTF-8?q?=E5=88=86=E6=95=B0=E6=8D=AE=E4=BF=9D=E5=AD=98=E8=87=B3=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E8=A1=A8=E8=80=8C=E9=83=A8=E5=88=86=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E5=9C=A8ini=E6=96=87=E4=BB=B6=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- constants.h | 2 +- mainwindow.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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/mainwindow.cpp b/mainwindow.cpp index 57e6b77..f70e7d3 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -161,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"; From 7b6a9960cb6b6292e7dc2dea0828bf995a326708 Mon Sep 17 00:00:00 2001 From: KnCRJNET Date: Mon, 26 May 2025 18:24:43 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E7=82=B9=E4=BB=A3=E7=A0=81=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- encrypt/EncryptData.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/encrypt/EncryptData.cpp b/encrypt/EncryptData.cpp index 4d57cd7..2ef5f27 100644 --- a/encrypt/EncryptData.cpp +++ b/encrypt/EncryptData.cpp @@ -73,7 +73,7 @@ QString DecryptString(const QString& base64EncodedPassword) #ifdef _WIN32 QByteArray decrypted = DecryptWithWindowsDPAPI(decoded); #else - QByteArray decrypted = SimpleDecrypt(password.toUtf8()); + QByteArray decrypted = SimpleDecrypt(decoded); #endif return QString::fromUtf8(decrypted); }