From a421793a5435f53bd8d39ad1e4f9cd4920bf72a0 Mon Sep 17 00:00:00 2001 From: neru Date: Mon, 13 Apr 2026 01:08:41 -0300 Subject: [PATCH] fix: utf8 to 16 conversion missing on encrypt --- src/unlocker/dbdcrypt.cpp | 54 ++++++++++++++++++++++++++++++++------- src/unlocker/dbdcrypt.h | 2 +- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/unlocker/dbdcrypt.cpp b/src/unlocker/dbdcrypt.cpp index a373397..6718fba 100644 --- a/src/unlocker/dbdcrypt.cpp +++ b/src/unlocker/dbdcrypt.cpp @@ -42,23 +42,28 @@ std::string DBDCrypt::encrypt(const std::string& data, const std::string& access { if (type == TYPE_1) { - auto compressed = zLibCompress(data); + auto compressed = zLibCompress(data); // Type 1 is UTF-8 if (compressed.empty()) return ""; - std::vector fullPayload(8, 0); + uint32_t rawSize = (uint32_t)data.size(); + std::vector fullPayload(4); + std::memcpy(fullPayload.data(), &rawSize, 4); + fullPayload.insert(fullPayload.end(), compressed.begin(), compressed.end()); return "DbdDAQEB" + b64Enc(fullPayload); } + if (type == TYPE_2) { auto transformedKey = transformCDNKey(CDN_KEY_BASE64); - std::string shiftedData = data; - for (char& c : shiftedData) + std::string utf16Data = utf8ToUtf16(data); + for (char& c : utf16Data) c = (char)((unsigned char)c - 1); - std::vector padded(shiftedData.begin(), shiftedData.end()); + std::vector padded(utf16Data.begin(), utf16Data.end()); + int padLen = 16 - (padded.size() % 16); if (padLen < 16) padded.insert(padded.end(), padLen, 0); @@ -70,16 +75,16 @@ std::string DBDCrypt::encrypt(const std::string& data, const std::string& access } if (type == TYPE_3) - { auto decodedKey = b64Dec(accessKey); if (decodedKey.empty()) return ""; - std::string shiftedData = data; - for (char& c : shiftedData) + std::string utf16Data = utf8ToUtf16(data); + for (char& c : utf16Data) c = (char)((unsigned char)c - 1); - std::vector padded(shiftedData.begin(), shiftedData.end()); + std::vector padded(utf16Data.begin(), utf16Data.end()); + int padLen = 16 - (padded.size() % 16); if (padLen < 16) padded.insert(padded.end(), padLen, 0); @@ -420,3 +425,34 @@ std::string DBDCrypt::utf16ToUtf8(const std::string& utf16) } return utf8; } + +std::string DBDCrypt::utf8ToUtf16(const std::string& utf8) +{ + if (utf8.empty()) return ""; + std::string utf16; + for (size_t i = 0; i < utf8.length(); ) + { + uint32_t cp = 0; + unsigned char c = utf8[i]; + if (c < 0x80) { cp = c; i += 1; } + else if (c < 0xE0) { cp = ((c & 0x1F) << 6) | (utf8[i + 1] & 0x3F); i += 2; } + else if (c < 0xF0) { cp = ((c & 0x0F) << 12) | ((utf8[i + 1] & 0x3F) << 6) | (utf8[i + 2] & 0x3F); i += 3; } + else { cp = ((c & 0x07) << 18) | ((utf8[i + 1] & 0x3F) << 12) | ((utf8[i + 2] & 0x3F) << 6) | (utf8[i + 3] & 0x3F); i += 4; } + + if (cp < 0x10000) { + utf16.push_back((char)(cp & 0xFF)); + utf16.push_back((char)(cp >> 8)); + } else { + cp -= 0x10000; + uint16_t high = (uint16_t)(0xD800 | (cp >> 10)); + uint16_t low = (uint16_t)(0xDC00 | (cp & 0x3FF)); + + utf16.push_back((char)(high & 0xFF)); + utf16.push_back((char)(high >> 8)); + utf16.push_back((char)(low & 0xFF)); + utf16.push_back((char)(low >> 8)); + } + } + return utf16; +} + diff --git a/src/unlocker/dbdcrypt.h b/src/unlocker/dbdcrypt.h index 3b2e81c..becde11 100644 --- a/src/unlocker/dbdcrypt.h +++ b/src/unlocker/dbdcrypt.h @@ -47,5 +47,5 @@ class DBDCrypt static std::vector transformCDNKey(const std::string& b64CDNKey); static std::string shiftKeyID(const std::string& id, int shift); static std::string utf16ToUtf8(const std::string& utf16); - + static std::string utf8ToUtf16(const std::string& utf8); };