diff --git a/common/commands.h b/common/commands.h index af32e40..ef9b0a9 100644 --- a/common/commands.h +++ b/common/commands.h @@ -58,8 +58,7 @@ typedef void* LPVOID, * HANDLE; #define CancelIo(p) close(reinterpret_cast(p)) #endif -#include -#include +#include "ip_enc.h" #include #include @@ -352,10 +351,16 @@ public: int FlagLen() const { return strlen(szFlag); } - const char* ServerIP()const { + const char* ServerIP(){ + if (bEncrypt) { + Decrypt(); + } return szServerIP; } - int ServerPort()const { + int ServerPort() { + if (bEncrypt) { + Decrypt(); + } return atoi(szPort); } int ClientType()const { @@ -372,8 +377,24 @@ public: return modified; } - bool IsValid()const { - return strlen(szServerIP) != 0 && atoi(szPort) > 0; + void Encrypt() { + if (!bEncrypt){ + bEncrypt = true; + StreamCipher cipher(0x12345678); + cipher.process((uint8_t*)szServerIP, sizeof(szServerIP)); + cipher.process((uint8_t*)szPort, sizeof(szPort)); + } + } + void Decrypt() { + if (bEncrypt) { + bEncrypt = false; + StreamCipher cipher(0x12345678); + cipher.process((uint8_t*)szServerIP, sizeof(szServerIP)); + cipher.process((uint8_t*)szPort, sizeof(szPort)); + } + } + bool IsValid() { + return strlen(ServerIP()) != 0 && ServerPort() > 0; } int Size() const { return sizeof(CONNECT_ADDRESS); diff --git a/common/ip_enc.h b/common/ip_enc.h new file mode 100644 index 0000000..caf05f4 --- /dev/null +++ b/common/ip_enc.h @@ -0,0 +1,33 @@ +#pragma once + +#include +#include +#include + +// Encode for IP and Port. +// provided by ChatGPT. +class StreamCipher { +private: + uint32_t state; + + // 简单非线性伪随机数生成器 + uint8_t prngNext() { + // 例子:xorshift32,改造一点非线性 + state ^= (state << 13); + state ^= (state >> 17); + state ^= (state << 5); + // 再混合一个简单的非线性变换 + uint8_t out = (state & 0xFF) ^ ((state >> 8) & 0xFF); + return out; + } + +public: + StreamCipher(uint32_t key) : state(key) {} + + // 加密解密(对称,长度不变) + void process(uint8_t* data, size_t len) { + for (size_t i = 0; i < len; ++i) { + data[i] ^= prngNext(); + } + } +}; diff --git a/server/2015Remote/BuildDlg.cpp b/server/2015Remote/BuildDlg.cpp index 6220b5d..ad0ad29 100644 --- a/server/2015Remote/BuildDlg.cpp +++ b/server/2015Remote/BuildDlg.cpp @@ -151,6 +151,7 @@ void CBuildDlg::OnBnClickedOk() SAFE_DELETE_ARRAY(szBuffer); return; } + g_ConnectAddress.Encrypt(); try { // 更新标识