skip to main content
hacking.communityplay in the sandbox.

Break the Repeating-Key XOR Cipher

crypto · medium · +75 XP

Repeating-Key XOR: A Classic Weakness

XOR encryption is beautifully simple: every byte of plaintext is XORed with a byte from a repeating key. If the key is short and the plaintext is known to follow a predictable pattern (like printable ASCII), the cipher crumbles quickly.

Why It Breaks

When the same key byte encrypts many plaintext bytes, statistical patterns survive. For a key length of n, bytes at positions 0, n, 2n, … were all encrypted with the same key byte. If the plaintext is ASCII, the most common byte in each group is likely 0x20 (space) or the character e — XORing your guess against the most-frequent ciphertext byte reveals the key byte. Repeat for each position and you recover the full key.

How to Decode

Your intercepted blob is delivered as a hex string. The workflow is:

  1. Hex-decode the blob — in Python: bytes.fromhex(blob) or CyberChef's From Hex operation.
  2. Identify key length — try lengths 1–8; for each, split ciphertext into columns and score each column's byte frequencies against expected English/ASCII.
  3. XOR each column with the best-guess key byte to recover the key, then XOR the full ciphertext with the repeating key.
  4. In Python: ''.join(chr(b ^ ord(k)) for b, k in zip(ct, itertools.cycle(key))) — the flag will appear in the plaintext.

Tools

  • Python 3 (recommended): itertools.cycle, bytes.fromhex, list comprehensions.
  • CyberChef: chain From Hex → XOR with your discovered key set to UTF-8 / Cycling.
  • xortool (pip install xortool): automates key-length detection and key recovery in one step.
// graded hints — spend XP when stuck

Sign in to unlock escalating hints for this medium lab.

// captured payload · xor
1f570d1d18465b0e124d0511055117085a0546542857420144011457140710074e49

XOR the bytes against the key `w4ve` (UTF-8, repeating).

// this payload is unique to your account — decode it, then submit the recovered hc{...} flag below

// submit flag
// no solves yet

// go deeper

// install app

Install hacking.community for fast access, offline reading, and push notifications. No app store needed.