XOR is the building block of most "homemade" crypto in CTFs. It's simple, fast and perfectly secure when used as a one-time pad — and broken in almost every other way challenge authors use it. Three properties are all you need:
a ^ b ^ b = a— XORing twice with the same key undoes itplaintext ^ ciphertext = key— knowing two of the three gives you the thirda ^ 0 = a— XOR with zero changes nothing
Single-byte XOR: brute force all 256 keys
If every byte was XORed with the same single byte, there are only 256 possible keys. Try them all and keep the result that looks like text:
ct = bytes.fromhex("1b37373331363f78151b7f2b7834")
for k in range(256):
pt = bytes(b ^ k for b in ct)
if b"picoCTF" in pt or pt.isascii():
print(k, pt)
For longer texts, score each candidate by how many characters are common English letters and spaces instead of eyeballing them.
Known plaintext: use the flag format
You almost always know how the plaintext starts: picoCTF{, flag{, CTF{. XOR those bytes with the start of the ciphertext and you get the start of the key:
known = b"picoCTF{"
key_start = bytes(c ^ p for c, p in zip(ct, known))
print(key_start)
If the key is shorter than the known prefix, you'll see it repeat — and you have the entire key.
Repeating-key XOR
With a multi-byte key repeated over the message, every Nth byte is XORed with the same key byte. Once you know (or guess) the key length N, split the ciphertext into N columns and solve each one as a single-byte XOR:
def xor_repeat(data, key):
return bytes(b ^ key[i % len(key)] for i, b in enumerate(data))
To find N, try each length from 2 to 40 and pick the one with the smallest normalized Hamming distance between consecutive blocks — or let a tool do it: xortool -c 20 ciphertext.bin guesses the key length and the key, assuming the most common plaintext byte is a space (0x20).
Two messages, same key
If two ciphertexts were encrypted with the same key stream, XORing them cancels the key: c1 ^ c2 = p1 ^ p2. Guess a word in one message and slide it along ("crib dragging") to reveal the other.
Tools
Python's bytes and a loop cover most cases. CyberChef has an XOR and an XOR Brute Force operation for quick checks, and pwntools provides xor(). For more classic cipher weaknesses, see common weak ciphers in crypto CTFs.