fix: utf8 to 16 conversion missing on encrypt
This commit is contained in:
@@ -42,23 +42,28 @@ std::string DBDCrypt::encrypt(const std::string& data, const std::string& access
|
|||||||
{
|
{
|
||||||
if (type == TYPE_1)
|
if (type == TYPE_1)
|
||||||
{
|
{
|
||||||
auto compressed = zLibCompress(data);
|
auto compressed = zLibCompress(data); // Type 1 is UTF-8
|
||||||
if (compressed.empty()) return "";
|
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());
|
fullPayload.insert(fullPayload.end(), compressed.begin(), compressed.end());
|
||||||
return "DbdDAQEB" + b64Enc(fullPayload);
|
return "DbdDAQEB" + b64Enc(fullPayload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (type == TYPE_2)
|
if (type == TYPE_2)
|
||||||
{
|
{
|
||||||
auto transformedKey = transformCDNKey(CDN_KEY_BASE64);
|
auto transformedKey = transformCDNKey(CDN_KEY_BASE64);
|
||||||
|
|
||||||
std::string shiftedData = data;
|
std::string utf16Data = utf8ToUtf16(data);
|
||||||
for (char& c : shiftedData)
|
for (char& c : utf16Data)
|
||||||
c = (char)((unsigned char)c - 1);
|
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);
|
int padLen = 16 - (padded.size() % 16);
|
||||||
if (padLen < 16) padded.insert(padded.end(), padLen, 0);
|
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)
|
if (type == TYPE_3)
|
||||||
|
|
||||||
{
|
{
|
||||||
auto decodedKey = b64Dec(accessKey);
|
auto decodedKey = b64Dec(accessKey);
|
||||||
if (decodedKey.empty()) return "";
|
if (decodedKey.empty()) return "";
|
||||||
|
|
||||||
std::string shiftedData = data;
|
std::string utf16Data = utf8ToUtf16(data);
|
||||||
for (char& c : shiftedData)
|
for (char& c : utf16Data)
|
||||||
c = (char)((unsigned char)c - 1);
|
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);
|
int padLen = 16 - (padded.size() % 16);
|
||||||
if (padLen < 16) padded.insert(padded.end(), padLen, 0);
|
if (padLen < 16) padded.insert(padded.end(), padLen, 0);
|
||||||
|
|
||||||
@@ -420,3 +425,34 @@ std::string DBDCrypt::utf16ToUtf8(const std::string& utf16)
|
|||||||
}
|
}
|
||||||
return utf8;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -47,5 +47,5 @@ class DBDCrypt
|
|||||||
static std::vector<uint8_t> transformCDNKey(const std::string& b64CDNKey);
|
static std::vector<uint8_t> transformCDNKey(const std::string& b64CDNKey);
|
||||||
static std::string shiftKeyID(const std::string& id, int shift);
|
static std::string shiftKeyID(const std::string& id, int shift);
|
||||||
static std::string utf16ToUtf8(const std::string& utf16);
|
static std::string utf16ToUtf8(const std::string& utf16);
|
||||||
|
static std::string utf8ToUtf16(const std::string& utf8);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user