Lesson 2 of 6

Reversing the keygen

Goal

Undo the transformation one step at a time to recover the key

01

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.

02

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
  1. Recovers the example key
  2. Recovers a longer key with other constants
  3. Undoes the rotation, not just the subtraction
  4. Recovers a key with digits and symbols
  5. Undoes the position step, so repeated characters come out right
03

Hints

0/3
Track lessons0 of 6 done

Six challenges of rising difficulty where you reverse a check function or bytecode and extract the key with your own code, not by copying it.

Advanced level, 6 lessons, 2 hours in total.

main.py
OutputPython (Pyodide)

Loading Python. This only happens once. Your browser keeps it after that.

–/5

Checks start once the environment is ready.

5 checks listed under “Your task”.

Track lessons0 of 6 done

Six challenges of rising difficulty where you reverse a check function or bytecode and extract the key with your own code, not by copying it.

Advanced level, 6 lessons, 2 hours in total.