Lesson 2 of 6
Reversing the keygen
- 20 min
- 5 checks
- 3 hints
- Go to task
Goal
Undo the transformation one step at a time to recover the key
Explanation
This is the heart of cracking challenges: you read how a program turns a key into a target, then undo its steps in reverse order to get the key back. You found a key generator, or keygen, in a program. It takes the key as a string, pairs each of its characters with a constant from the list konst, and computes each target byte in three steps, in this order:
t = ord(key[i]) ^ konst[i] # first, XOR with the position's constant t = ((t << 3) | (t >> 5)) & 0xFF # then rotate left by three bits target[i] = (t + i) & 0xFF # then add the position number
The key here is long, twelve to sixteen printable characters, so you can't try every possible key: there are far too many. But each of the three steps can be undone on its own. Addition is undone by subtraction, a left rotation by a right rotation of the same amount, and XOR by XOR with the same constant. So you undo the steps from last to first to get each character back.
Your task
Write a function recover(target, konst) that takes the list of target bytes and the list of constants and returns the key as a string. The checks build targets from hidden keys and constants, so copying a ready-made key won't help.
Checks
5 checks- Recovers the example key
- Recovers a longer key with other constants
- Undoes the rotation, not just the subtraction
- Recovers a key with digits and symbols
- Undoes the position step, so repeated characters come out right