# Reversing an XOR encryption/decryption function

I have a help recommended high-school project that I'm stuck with. To basically explain the problem: I have an encryption function that is used as the decryption function and I need to reverse it to make an encryption function.

I tried many things but I can't find the proper result. The best I came with was to encrypt 1 byte on 2 properly.

Here is the core of the function (the rest is variables initialization:

// Decrypt
for (i = 0; i < 256; i++) {
decrypted_msg[i] = (xor_key[i % 128] - i) ^ (encrypted_msg[i] - i / 16 - 12);
}


So can someone explain me (like I'm five) how to reverse this code to produce the encryption?

Thank you by advance for help.

decrypted_msg[i] = (xor_key[i % 128] - i) ^ (encrypted_msg[i] - i / 16 - 12);
\begin{align} p[i] &= (\phantom{(}key[i \bmod 128]-i) \oplus (c[i] - i/16 -12)\\ c[i] - i/16 -12 &= (\phantom{(}key[i \bmod 128]-i) \oplus p[i]\\ c[i] - i/16 &= \left( (key[i \bmod 128]-i) \oplus p[i] \right) +12\\ c[i] &= \left( (key[i \bmod 128]-i) \oplus p[i] \right) +12 +i/16 \end{align}
• You can edit my answer and take the $\LaTeX$ code, too. What kind of project is this on what kind of school? Oct 9, 2022 at 23:42