我正试图让加密转换这个解密,但真的很难做..
unsigned int decrypt(unsigned char *encBuffer, unsigned int encBufferLen, unsigned char *decBuffer)
{
unsigned int decBufferLen = 0;
unsigned char table[] = { NULL, ' ', '-', '.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 0x0D };
unsigned int offset = 0, i = 0;
unsigned char encStrLen = 0, c = 0;
while (offset < encBufferLen)
{
if (encBuffer[offset] == 0xFFU) // if byte is 0xFF then add 0x0D to new buffer and continue
{
*(decBuffer + decBufferLen++) = 0x0D;
offset++;
continue;
}
encStrLen = encBuffer[offset++]; // get string length
i = 0;
if ((encStrLen & 0x80U) == 0) // if result is 0 then decrypt the string by Xor 33
{
while ((i++ < encStrLen) && (offset < encBufferLen))
{
*(decBuffer + decBufferLen++) = (encBuffer[offset++] ^ 0x33U);
}
}
else // otherwise
{
encStrLen &= 0x7FU;
while ((i < encStrLen) && (offset < encBufferLen))
{
c = encBuffer[offset++]; // c = current byte, increment the index
*(decBuffer + decBufferLen++) = (table[(c & 0xF0U) >> 4]);
if (table[c & 0x0FU] != NULL)
{
*(decBuffer + decBufferLen++) = table[c & 0x0FU];
}
i += 2;
}
}
}
return decBufferLen;
}
这是我在一些测试后得到的
std::vector<unsigned char> encrypt(std::vector<unsigned char> decryptedBuf)
{
std::vector<unsigned char> vector;
unsigned int offset = 0, decryptedStringLength = 0;
unsigned char currentByte = 0;
while (offset < decryptedBuf.size())
{
if (decryptedBuf[offset] == 0x0D)
{
vector.push_back(0xFF);
offset++;
continue;
}
decryptedStringLength = decryptedBuf.size() - 1; // <--- to edit if encrypt works
vector.push_back(decryptedStringLength); // <--- i'm not sure it will be interpreted right
if (decryptedStringLength < 0x80) // < '128' Xor 33 http://pastebin.com/b18JfBFK
{
for (unsigned int i = 0; i < decryptedStringLength; i++)
vector.push_back(decryptedBuf[offset++] ^ 0x33);
}
else // >= '128' Table
{
decryptedStringLength -= 0x80; // & 0x7F http://pastebin.com/THZjZJfs
for (unsigned int i = 0; i < decryptedStringLength; i += 2)
{
currentByte = decryptedBuf[offset++];
}
}
}
return vector;
}
您对如何扭转局面有所了解吗?我正在尝试2天,但我没有任何可行的工作..
答案 0 :(得分:0)
decrypt
不是一对一的 - 许多不同的密文可以解密到同一个明文。最简单的方法来实现相应的加密&#34; function是将明文分成不超过127个字节的块,并输出块长度,然后是块XOR 0x33。这样,您只需执行decrypt
算法的一小部分。 Working example
std::vector<unsigned char> encrypt(std::vector<unsigned char> decryptedBuf) {
vector<unsigned char> result;
size_t cur_offset = 0;
while (cur_offset < decryptedBuf.size()) {
unsigned char chunk_size = static_cast<unsigned char>(
min(decryptedBuf.size() - cur_offset, size_t(0x7F)));
result.push_back(chunk_size);
result.insert(result.end(), decryptedBuf.begin() + cur_offset,
decryptedBuf.begin() + cur_offset + chunk_size);
for (vector<unsigned char>::iterator it = result.end() - chunk_size;
it != result.end(); ++it) {
*it ^= 0x33;
}
cur_offset += chunk_size;
}
return result;
}