1
$\begingroup$

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.

$\endgroup$
0

1 Answer 1

1
$\begingroup$

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}

$\endgroup$
5
  • 1
    $\begingroup$ Thanks a lot, it worked flawlessly. And big thank for the equations! $\endgroup$
    – Roberto
    Oct 9, 2022 at 23:33
  • $\begingroup$ You can edit my answer and take the $\LaTeX$ code, too. What kind of project is this on what kind of school? $\endgroup$
    – kelalaka
    Oct 9, 2022 at 23:42
  • $\begingroup$ I'm in normal highschool in France, it's a project about reversing communication protocol at my school's computer club. $\endgroup$
    – Roberto
    Oct 9, 2022 at 23:53
  • $\begingroup$ I hope getting help was not prohibited $\endgroup$
    – kelalaka
    Oct 9, 2022 at 23:55
  • $\begingroup$ No, don't worry. It's even recommended. :) $\endgroup$
    – Roberto
    Oct 10, 2022 at 0:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.