Score:1

Reversing an XOR encryption/decryption function

hr flag

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.

Score:1
in flag

What about this?

decrypted_msg[i] = (xor_key[i % 128] - i) ^ (encrypted_msg[i] - i / 16 - 12);

convert this into equations

\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}

Roberto avatar
hr flag
Thanks a lot, it worked flawlessly. And big thank for the equations!
kelalaka avatar
in flag
You can edit my answer and take the $\LaTeX$ code, too. What kind of project is this on what kind of school?
Roberto avatar
hr flag
I'm in normal highschool in France, it's a project about reversing communication protocol at my school's computer club.
kelalaka avatar
in flag
I hope getting help was not prohibited
Roberto avatar
hr flag
No, don't worry. It's even recommended. :)
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.