#include "Authorization.h" #include "Logger.h" #include "Utils.h" #include #include #include #include #include #include #include #include #include #include "cryptopp/cryptlib.h" #include "cryptopp/sha.h" #include "cryptopp/hex.h" #include "cryptopp/base64.h" #include "cryptopp/files.h" #include "cryptopp/aes.h" #include "cryptopp/modes.h" #ifdef _WIN64// 是 64 位 Windows #ifdef _DEBUG #pragma comment(lib, "cryptopp/x64/Debug/cryptlib.lib") #else #pragma comment(lib, "cryptopp/x64/Release/cryptlib.lib") #endif #else #ifdef _DEBUG// 是 32 位 Windows #pragma comment(lib, "cryptopp/Win32/Debug/cryptlib.lib") #else #pragma comment(lib, "cryptopp/Win32/Release/cryptlib.lib") #endif #endif #pragma comment(lib, "Iphlpapi.lib") // 简单的加密密钥(实际应用中应该更安全) const std::string ENCRYPTION_KEY = "CommModifyServiceKey12345"; bool Authorization::IsAuthorized() { // 获取可执行文件目录并构建完整路径 std::string exeDir = Utils::GetExecutableDirectory(); std::string licensePath = exeDir + "\\license.dat"; // 检查授权文件是否存在 std::ifstream licenseFile(licensePath); if (!licenseFile.is_open()) { LOG_WARNING("License file not found"); // 生成授权码文件 std::string machineCode = GetMachineCode(); if (!GenerateAuthorizationCodeFile(machineCode)) { LOG_ERROR("Failed to generate authorization code file"); } return false; } // 读取授权文件内容 std::stringstream buffer; buffer << licenseFile.rdbuf(); std::string encryptedData = buffer.str(); licenseFile.close(); // 验证授权文件 return VerifyLicenseFile(licensePath); } bool Authorization::GenerateAuthorizationCodeFile(const std::string &machineCode) { try { // 获取可执行文件目录并构建完整路径 std::string exeDir = Utils::GetExecutableDirectory(); std::string filePath = exeDir + "\\auth_code.dat"; std::ofstream codeFile(filePath); if (!codeFile.is_open()) { LOG_ERROR("Failed to create authorization code file"); return false; } // 写入机器码到授权码文件 codeFile << machineCode; codeFile.close(); LOG_INFO("Authorization code file generated successfully"); return true; } catch (const std::exception &e) { LOG_ERROR("Error generating authorization code file: " + std::string(e.what())); return false; } } std::string Authorization::GetMachineCode() { std::string cpuId = GetCPUID(); std::string diskSerial = GetHardDiskSerial(); std::string macAddress = GetMACAddress(); // 组合所有硬件信息 std::string hardwareInfo = cpuId + diskSerial + macAddress; // 使用SHA256生成机器码 CryptoPP::SHA256 sha256; std::string hash; CryptoPP::StringSource ss(hardwareInfo, true, new CryptoPP::HashFilter(sha256, new CryptoPP::HexEncoder( new CryptoPP::StringSink(hash)))); return hash; } std::string Authorization::GetCPUID() { int cpuInfo[4] = {0}; __cpuid(cpuInfo, 0); std::stringstream ss; ss << std::hex << std::uppercase << std::setfill('0'); for (int i = 0; i < 4; i++) { ss << std::setw(8) << cpuInfo[i]; } return ss.str(); } std::string Authorization::GetHardDiskSerial() { HANDLE hDevice = CreateFileA("\\\\.\\PhysicalDrive0", 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if (hDevice == INVALID_HANDLE_VALUE) { return "UNKNOWN"; } STORAGE_PROPERTY_QUERY query = {}; query.PropertyId = StorageDeviceProperty; query.QueryType = PropertyStandardQuery; STORAGE_DESCRIPTOR_HEADER header = {}; DWORD bytesReturned = 0; if (!DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY, &query, sizeof(query), &header, sizeof(header), &bytesReturned, NULL)) { CloseHandle(hDevice); return "UNKNOWN"; } std::vector buffer(header.Size); STORAGE_DEVICE_DESCRIPTOR *deviceDescriptor = reinterpret_cast(buffer.data()); if (!DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY, &query, sizeof(query), buffer.data(), header.Size, &bytesReturned, NULL)) { CloseHandle(hDevice); return "UNKNOWN"; } CloseHandle(hDevice); if (deviceDescriptor->SerialNumberOffset == 0) { return "UNKNOWN"; } std::string serialNumber(reinterpret_cast(buffer.data() + deviceDescriptor->SerialNumberOffset)); return serialNumber; } std::string Authorization::GetMACAddress() { PIP_ADAPTER_INFO adapterInfo; ULONG bufferSize = sizeof(IP_ADAPTER_INFO); adapterInfo = (IP_ADAPTER_INFO *)malloc(bufferSize); if (GetAdaptersInfo(adapterInfo, &bufferSize) == ERROR_BUFFER_OVERFLOW) { free(adapterInfo); adapterInfo = (IP_ADAPTER_INFO *)malloc(bufferSize); } if (GetAdaptersInfo(adapterInfo, &bufferSize) == NO_ERROR) { std::stringstream ss; ss << std::hex << std::uppercase << std::setfill('0'); for (int i = 0; i < adapterInfo->AddressLength; i++) { if (i > 0) ss << "-"; ss << std::setw(2) << static_cast(adapterInfo->Address[i]); } free(adapterInfo); return ss.str(); } free(adapterInfo); return "UNKNOWN"; } bool Authorization::VerifyLicenseFile(const std::string &licenseFile) { try { std::ifstream file(licenseFile, std::ios::binary); if (!file.is_open()) { return false; } std::stringstream buffer; buffer << file.rdbuf(); std::string encryptedData = buffer.str(); file.close(); // 解密授权文件 std::string decryptedData = DecryptLicense(encryptedData); // 检查解密后的数据是否包含当前机器码 std::string currentMachineCode = GetMachineCode(); if (decryptedData.find(currentMachineCode) == std::string::npos) { return false; } // 检查授权是否过期 // 查找Expiration行 size_t expirationPos = decryptedData.find("Expiration: "); if (expirationPos == std::string::npos) { // 如果没有找到Expiration行,则认为授权永不过期 return true; } // 提取过期日期 size_t dateStart = expirationPos + 12; // "Expiration: "的长度 size_t dateEnd = decryptedData.find('\n', dateStart); if (dateEnd == std::string::npos) { dateEnd = decryptedData.length(); } std::string expirationDate = decryptedData.substr(dateStart, dateEnd - dateStart); // 获取当前日期 time_t t = time(0); struct tm now; localtime_s(&now, &t); char currentDateStr[11]; strftime(currentDateStr, sizeof(currentDateStr), "%Y-%m-%d", &now); std::string currentDate(currentDateStr); // 比较日期 if (currentDate >= expirationDate) { LOG_WARNING("License has expired"); return false; } return true; } catch (const std::exception &e) { LOG_ERROR("Error verifying license file: " + std::string(e.what())); return false; } } std::string Authorization::DecryptLicense(const std::string &encryptedData) { try { // 使用Base64解码 std::string decoded; CryptoPP::StringSource ss(encryptedData, true, new CryptoPP::Base64Decoder( new CryptoPP::StringSink(decoded))); // 使用AES解密 CryptoPP::AES::Decryption aesDecryption((CryptoPP::byte *)ENCRYPTION_KEY.data(), 16); CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, (CryptoPP::byte *)ENCRYPTION_KEY.data()); std::string decrypted; CryptoPP::StringSource ss2(decoded, true, new CryptoPP::StreamTransformationFilter(cbcDecryption, new CryptoPP::StringSink(decrypted))); return decrypted; } catch (const std::exception &e) { LOG_ERROR("Error decrypting license: " + std::string(e.what())); return ""; } } std::string Authorization::EncryptData(const std::string &data) { try { // 使用AES加密 CryptoPP::AES::Encryption aesEncryption((CryptoPP::byte *)ENCRYPTION_KEY.data(), 16); CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, (CryptoPP::byte *)ENCRYPTION_KEY.data()); std::string encrypted; CryptoPP::StringSource ss(data, true, new CryptoPP::StreamTransformationFilter(cbcEncryption, new CryptoPP::Base64Encoder( new CryptoPP::StringSink(encrypted)))); return encrypted; } catch (const std::exception &e) { LOG_ERROR("Error encrypting data: " + std::string(e.what())); return ""; } }