fix: utf8 to 16 conversion missing on encrypt

This commit is contained in:
2026-04-13 01:08:41 -03:00
parent bda519e70a
commit a421793a54
2 changed files with 46 additions and 10 deletions
+45 -9
View File
@@ -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<uint8_t> fullPayload(8, 0);
uint32_t rawSize = (uint32_t)data.size();
std::vector<uint8_t> 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<uint8_t> padded(shiftedData.begin(), shiftedData.end());
std::vector<uint8_t> 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<uint8_t> padded(shiftedData.begin(), shiftedData.end());
std::vector<uint8_t> 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;
}