I would like to ask if you have any idea or approach to reverse engineer a decryption algorithm to find the opposite encryption function. I do have all required keys and fields and of course the decryption source code, which I reverse engineered already.
I have analyzed the code and kinda know how it works but can`t figure out how to reverse (in the sense of undoing the encryption of) it.
The following information is available to me:
// I have all these fields (filled correctly)
public byte[] Keychain;
public uint Step, Mul, HeaderXor, Key;
The decryption function looks like this:
public void Decrypt(byte[] packet) {
fixed (byte* pp = packet, pk = Keychain) {
uint size = (uint)GetPacketSize(packet);
uint header = (first) ? /* Checks if it is a partial packet (It isnt!)*/
0x000eb7e2 :
*((uint*)&pp[0]) ^ HeaderXor;
// HeaderXor is an unsigned int
// It also changes after each decryption and if the key changes
if (first)
first = false;
uint token = *((uint*)&pp[0]);
*((uint*)&pp[0]) = header;
token &= 0x3FFF; // Get only last 14 bits
token *= Mul * 4;
// Mul is an unsigned int and changes sometimes
token = *((uint*)&pk[token]);
uint i, r, t;
size -= r = (size - 8) & 3; // Make size dividable by 4
for (i = 8; i < size; i += 4) {
t = *((uint*)&pp[i]);
token ^= t;
*((uint*)&pp[i]) = token;
t &= 0x3FFF;
token = *((uint*)&pk[t * Mul * 4]);
}
t = 0xFFFFFFFF >> 8 * (4 - (int)r);
token &= t;
*((uint*)&pp[i]) ^= token; // If something is left over ( if size - 8 == 5 then size & 3 has rest of 1)
* ((uint*)&pp[4]) = 0;
Step++;
Step &= 0x3FFF;
HeaderXor = *((uint*)&pk[Step * Mul * 4]);
}
}
Example results:
// Encrypted data
// 5b 54 34 23
// cc c2 5a a3
// 81 7e d6 27
// 36 c4 8f 36
// b9 3b 6f ce
// f4 8e 72 5b
//
// Decrypted data
// e2 b7 18 00
// 00 00 00 00
// be 00 56 00
// 2f 00 58 00
// 30 00 59 00
// 31 00 01 00
So, do you have any approach ? Maybe this source code can offer some more informations, but its Server-To-Client:
I think, the Server-To-Client encryption is not the right thing. The decryption code I posted above does returns right results but is not the same as you can see at the linked page.