Known-Plaintext XOR Key Recovery (Crib Dragging)
- •
What it is
- •
A technique for breaking a repeating-key (or otherwise structured) XOR cipher when you know or can guess a fragment of the plaintext — a "crib." Since XOR is invertible, XORing the crib against the aligned ciphertext bytes reveals the key bytes at that position directly.
- •
- •
When to apply
- •
Ciphertext is XORed with an unknown key, and the plaintext format is predictable — e.g. a CTF flag guaranteed to start with
crypto{and end with}.
- •
- •
Math
- •
. Knowing at a given offset recovers the key byte at that offset directly.
- •
- •
Worked example
- •
Ciphertext byte at offset 0 is
0x0e; the flag must start withc(0x63). Key byte =0x0e ^ 0x63=0x6d('m') — matching the recovered keymyXORkeyfrom the actual challenge.
- •
- •
Python
- •
def crack_byte(ct_hex, expected_char): for k in range(256): if chr(int(ct_hex, 16) ^ k) == expected_char: return k
- •
- •
Cards
- •
What do you need to know (or guess) to crib-drag an XOR cipher?
- •
A fragment of the plaintext (a crib) at a known offset in the ciphertext.
- •
- •
How do you recover the key byte at a given offset once you know the plaintext byte there?
- •
XOR the known plaintext byte with the ciphertext byte at that offset.
- •
- •