Coding help :D

Doubts, help and support about QuickBMS and other game research tools
Ekey
Posts: 1383
Joined: Sat Aug 09, 2014 2:34 pm

Coding help :D

Post by Ekey »

Hey guys. I have a specific project for which it is necessary to encrypt some files back after decryption. The algorithm is simple (XOR) and I was hoping that it would work both ways (decrypt and encrypt data) but the function works as it turned out only in one direction - decrypt only. I need expert help and teach the function to correctly encrypt data back :)


Code: Select all

uint8_t kBox1[20] = {
   0xEB, 0x57, 0x50, 0x86, 0x32, 0x0D, 0x27, 0x60, 0xF4, 0xDE,
   0xF2, 0x4F, 0x6F, 0x13, 0xDA, 0x7B, 0x29, 0x3F, 0xF1, 0x41};

uint8_t kBox2[20] = {
   0x2D, 0x50, 0xD7, 0x64, 0x17, 0xAB, 0x25, 0x0B, 0xC0, 0x3E,
   0x61, 0x49, 0x20, 0x7A, 0x8E, 0x78, 0xA0, 0x67, 0x8D, 0x29};
   
uint32_t Var4 = 0;
int32_t Var8 = (kBox2[16] * kBox1[12]) * dwSize;

for(int32_t i = 0; i < dwSize; i++)
{
    uint32_t j = int32_t(Var8 & (Var4 << 11)) >> (kBox2[19-(i % 20)] % 7);
    lpBuffer[i] ^= kBox1[j % 20] ^ (j + j / 255) ^ kBox2[i % 20];
    Var4 += dwSize;
}


Files for testing are attached. I will be glad to any help and advice.

Thanks in advance :)
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Coding help :D

Post by aluigi »

The function works with both encryption and decryption, it's the same.

The only difference is that the encrypted file has an additional footer at the end "TGCA\x14\x10\x06\x15" so when you decrypt it you must remove the 8 bytes from dwSize.
Ekey
Posts: 1383
Joined: Sat Aug 09, 2014 2:34 pm

Re: Coding help :D

Post by Ekey »

Thanks Luigi for answer.

Unfortunately, I already tried to encrypt but the data is different, except for the 1st byte. In addition, the encrypted file is not decrypted by this function - the output data is like garbage :)

Comparison:

First 0x200 bytes of original:

Image

And first 0x200 of new file

Image

Therefore, I wonder what could be the solution :)
Ekey
Posts: 1383
Joined: Sat Aug 09, 2014 2:34 pm

Re: Coding help :D

Post by Ekey »

Ah, i'm idiot :lol: