Search code examples
cida

C math function with _BYTE and


I used IDA to decompile a function a program and i don`t know what exactly this code work.

flag[i] ^= *((_BYTE *)&v2 + (signed int)i % 4);

How does this work?


Solution

  • This could be used for xor-"decrypting" (or encrypting, the operation is symmetric) a buffer with a 4-byte key. See the following code, which might be a bit better readable, than the decompiler output

    char flag[SIZE];
    char key[4];
    
    for (int i = 0; i < SIZE; i++) {
        flag[i] = flag[i] ^ key[i%4];
    }
    

    So if your data is "ZGUIHUOHJIOJOPIJMXAR" and your key is "akey", then the snippet basically does

      ZGUIHUOHJIOJOPIJMXA
    ^ akeyakeyakeyakeyake
    =====================
      yourplaintextresult (<- not really the result here, but you get the idea)