Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions DrCOM_JLU_Qt.pro
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
CONFIG += c++11

SOURCES += \
encrypt/EncryptData.cpp \
main.cpp \
mainwindow.cpp \
dogcomcontroller.cpp \
Expand All @@ -40,6 +41,7 @@ SOURCES += \
DogcomSocket.cpp

HEADERS += \
encrypt/EncryptData.h \
mainwindow.h \
dogcomcontroller.h \
constants.h \
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
79 changes: 79 additions & 0 deletions encrypt/EncryptData.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
23 changes: 23 additions & 0 deletions encrypt/EncryptData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef ENCRYPTDATA_H
#define ENCRYPTDATA_H

#include <QString>
#include <QByteArray>

#ifdef _WIN32
#include <windows.h>
#include <wincrypt.h>
#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
16 changes: 12 additions & 4 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <QProcess>
#include <QTimer>

#include "encrypt/EncryptData.h"

MainWindow::MainWindow(SingleApplication *parentApp, QWidget *parent) :
QDialog(parent),
ui(new Ui::MainWindow),
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down