Skip to content

Commit 5698034

Browse files
authored
Merge pull request #5 from KnCRJVirX/feature-encrypt-storage-password
加密存储密码
2 parents cdac8dc + 7b6a996 commit 5698034

File tree

5 files changed

+118
-5
lines changed

5 files changed

+118
-5
lines changed

DrCOM_JLU_Qt.pro

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
2929
CONFIG += c++11
3030

3131
SOURCES += \
32+
encrypt/EncryptData.cpp \
3233
main.cpp \
3334
mainwindow.cpp \
3435
dogcomcontroller.cpp \
@@ -40,6 +41,7 @@ SOURCES += \
4041
DogcomSocket.cpp
4142

4243
HEADERS += \
44+
encrypt/EncryptData.h \
4345
mainwindow.h \
4446
dogcomcontroller.h \
4547
constants.h \
@@ -68,6 +70,7 @@ DEFINES += QAPPLICATION_CLASS=QApplication
6870
VERSION = 1.0.0.6
6971

7072
win32:LIBS += -lwsock32
73+
win32:LIBS += -lcrypt32
7174
# 更新日志:
7275
# v 0.0.0.0 实现基本功能
7376
# v 1.0.0.1 修复适配高DPI时只窗口大小适配但字号不适配的bug

constants.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ enum {
5656
const int PORT_BIND = 61440;
5757
const int PORT_DEST = 61440;
5858
const QString SERVER_IP = "10.100.61.3";
59-
const QString SETTINGS_FILE_NAME = "DrCOM_JLU_Qt.ini";
59+
const QString SETTINGS_FILE_NAME = "DrCOM_JLU_Qt";
6060
const QString
6161
ID_ACCOUNT = "account",
6262
ID_PASSWORD = "password",

encrypt/EncryptData.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include "EncryptData.h"
2+
3+
#ifdef _WIN32
4+
5+
// 加密
6+
QByteArray EncryptWithWindowsDPAPI(const QByteArray& data)
7+
{
8+
DATA_BLOB inBlob;
9+
inBlob.pbData = (BYTE*)data.data();
10+
inBlob.cbData = data.size();
11+
12+
DATA_BLOB outBlob;
13+
if (CryptProtectData(&inBlob, L"Password", nullptr, nullptr, nullptr, 0, &outBlob)) {
14+
QByteArray result((char*)outBlob.pbData, outBlob.cbData);
15+
LocalFree(outBlob.pbData);
16+
return result;
17+
} else {
18+
return {};
19+
}
20+
}
21+
22+
// 解密
23+
QByteArray DecryptWithWindowsDPAPI(const QByteArray& encryptedData)
24+
{
25+
DATA_BLOB inBlob;
26+
inBlob.pbData = (BYTE*)encryptedData.data();
27+
inBlob.cbData = encryptedData.size();
28+
29+
DATA_BLOB outBlob;
30+
if (CryptUnprotectData(&inBlob, nullptr, nullptr, nullptr, nullptr, 0, &outBlob)) {
31+
QByteArray result((char*)outBlob.pbData, outBlob.cbData);
32+
LocalFree(outBlob.pbData);
33+
return result;
34+
} else {
35+
return {};
36+
}
37+
}
38+
39+
#else
40+
QByteArray SimpleEncrypt(const QByteArray& data, const QByteArray& key = "SimpleKey")
41+
{
42+
QByteArray ba = data;
43+
for (int i = 0; i < ba.size(); ++i){
44+
ba[i] = ba[i] ^ key[i % key.size()];
45+
}
46+
return ba;
47+
}
48+
49+
QByteArray SimpleDecrypt(const QByteArray& data, const QByteArray& key = "SimpleKey")
50+
{
51+
QByteArray ba = data;
52+
for (int i = 0; i < ba.size(); ++i){
53+
ba[i] = ba[i] ^ key[i % key.size()];
54+
}
55+
return ba;
56+
}
57+
#endif
58+
59+
// 包装为字符串操作
60+
QString EncryptString(const QString& password)
61+
{
62+
#ifdef _WIN32
63+
QByteArray encrypted = EncryptWithWindowsDPAPI(password.toUtf8());
64+
#else
65+
QByteArray encrypted = SimpleEncrypt(password.toUtf8());
66+
#endif
67+
return encrypted.toBase64();
68+
}
69+
70+
QString DecryptString(const QString& base64EncodedPassword)
71+
{
72+
QByteArray decoded = QByteArray::fromBase64(base64EncodedPassword.toUtf8());
73+
#ifdef _WIN32
74+
QByteArray decrypted = DecryptWithWindowsDPAPI(decoded);
75+
#else
76+
QByteArray decrypted = SimpleDecrypt(decoded);
77+
#endif
78+
return QString::fromUtf8(decrypted);
79+
}

encrypt/EncryptData.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef ENCRYPTDATA_H
2+
#define ENCRYPTDATA_H
3+
4+
#include <QString>
5+
#include <QByteArray>
6+
7+
#ifdef _WIN32
8+
#include <windows.h>
9+
#include <wincrypt.h>
10+
#endif
11+
12+
#ifdef _WIN32
13+
QByteArray EncryptWithWindowsDPAPI(const QByteArray& data);
14+
QByteArray DecryptWithWindowsDPAPI(const QByteArray& encryptedData);
15+
#else
16+
QByteArray SimpleEncrypt(const QByteArray& data, const QByteArray& key = "SimpleKey");
17+
QByteArray SimpleDecrypt(const QByteArray& data, const QByteArray& key = "SimpleKey");
18+
#endif
19+
20+
QString EncryptString(const QString& password);
21+
QString DecryptString(const QString& base64EncodedPassword);
22+
23+
#endif // ENCRYPTDATA_H

mainwindow.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <QProcess>
1818
#include <QTimer>
1919

20+
#include "encrypt/EncryptData.h"
21+
2022
MainWindow::MainWindow(SingleApplication *parentApp, QWidget *parent) :
2123
QDialog(parent),
2224
ui(new Ui::MainWindow),
@@ -159,7 +161,7 @@ void MainWindow::RestartDrcom()
159161
void MainWindow::QuitDrcom()
160162
{
161163
// 退出之前恢复重试计数
162-
QSettings s(SETTINGS_FILE_NAME, QSettings::IniFormat);
164+
QSettings s(SETTINGS_FILE_NAME);
163165
s.setValue(ID_RESTART_TIMES, 0);
164166
qDebug() << "reset restartTimes";
165167
qDebug() << "QuitDrcom";
@@ -201,14 +203,12 @@ void MainWindow::IconActivated(QSystemTrayIcon::ActivationReason reason)
201203
void MainWindow::LoadSettings() {
202204
QSettings s(SETTINGS_FILE_NAME);
203205
account = s.value(ID_ACCOUNT, "").toString();
204-
password = s.value(ID_PASSWORD, "").toString();
205206
mac_addr = s.value(ID_MAC, "").toString();
206207
bRemember = s.value(ID_REMEMBER, false).toBool();
207208
bAutoLogin = s.value(ID_AUTO_LOGIN, false).toBool();
208209
bHideWindow = s.value(ID_HIDE_WINDOW, false).toBool();
209210
bNotShowWelcome = s.value(ID_NOT_SHOW_WELCOME, false).toBool();
210211
ui->lineEditAccount->setText(account);
211-
ui->lineEditPass->setText(password);
212212
SetMAC(mac_addr);
213213
if (bRemember)
214214
ui->checkBoxRemember->setCheckState(Qt::CheckState::Checked);
@@ -226,15 +226,23 @@ void MainWindow::LoadSettings() {
226226
ui->checkBoxNotShowWelcome->setCheckState(Qt::CheckState::Checked);
227227
else
228228
ui->checkBoxNotShowWelcome->setCheckState(Qt::CheckState::Unchecked);
229+
230+
// 密码加密处理
231+
QString encryptedPasswd = s.value(ID_PASSWORD, "").toString();
232+
password = DecryptString(encryptedPasswd);
233+
ui->lineEditPass->setText(password);
229234
}
230235

231236
void MainWindow::SaveSettings() {
232237
QSettings s(SETTINGS_FILE_NAME);
233238
s.setValue(ID_ACCOUNT, account);
234-
s.setValue(ID_PASSWORD, password);
235239
s.setValue(ID_MAC, mac_addr);
236240
s.setValue(ID_REMEMBER, bRemember);
237241
s.setValue(ID_AUTO_LOGIN, bAutoLogin);
242+
243+
// 密码加密处理
244+
QString enctyptedPasswd = EncryptString(password);
245+
s.setValue(ID_PASSWORD, enctyptedPasswd);
238246
}
239247

240248
void MainWindow::SetMAC(const QString &m)

0 commit comments

Comments
 (0)