A single-byte XOR cipher has only 256 possible keys, so an automated attack can try every key by trying every key and scoring which decryption looks most like English. The attacker XORs each of the 256 candidate key bytes against the ciphertext, runs frequency analysis on each result, and keeps the one whose letter distribution best matches normal text. Repeating-key (Vigenere-style) XOR can also be attacked: a computer estimates the key length using the Hamming distance between ciphertext blocks, splits the message into columns that each share one key byte, then breaks every column as an independent single-byte problem. XOR encryption is only unbreakable as a one-time pad - a truly random key, as long as the message, never reused.
That is the summary an AI overview will give you. What it can't give you is the part that makes it click: the actual scoring functions, the reason Hamming distance leaks the key length, and working code you can run against a real ciphertext. That is the rest of this article.
The whole attack in one picture
Automated XOR cryptanalysis is a pipeline. For repeating-key ciphertext it has three stages - recover the key length, split into single-byte columns, then solve each column by frequency analysis - and the single-byte case is just the last stage on its own.
The single-byte case skips stages 1 and 2 entirely - the whole ciphertext is one column, and you go straight to trying all 256 keys.
Why XOR gives itself away
XOR encryption is plaintext XOR key = ciphertext, and it is its own inverse: ciphertext XOR key = plaintext. That symmetry is exactly what makes it fast to attack. The attacker doesn't need to "reverse" anything - they just need to guess the key, and XOR happily hands back plaintext for any guess. The only hard part is recognizing which of the guesses produced real text. That recognition problem is what frequency analysis solves.
The weakness is structural, not a bug in any implementation. A short key that repeats means the same key byte lands on many plaintext bytes, and repetition is a signal. The XOR cipher itself is not the flaw - XOR is the core of the unbreakable one-time pad. The flaw is reusing key material, which every practical XOR "cipher" does.
The techniques, side by side
Two attacks cover essentially all breakable XOR. Which one you reach for depends only on whether the key is one byte or several.
| Single-byte XOR | Repeating-key (Vigenere) XOR | |
|---|---|---|
| Key | One byte (0-255) | N bytes, cycled over the message |
| Search space | 256 keys total | ~256 x N (linear in key length) |
| Step 1 | - | Recover key length N via Hamming distance |
| Step 2 | - | Transpose ciphertext into N columns |
| Core method | Try all 256 keys, score each output | Solve each column as single-byte XOR |
| Scoring | Chi-square or frequency-weight sum vs. English | Same, applied per column |
| Time to break | Depends on input and scoring | Depends on input, candidate lengths, and scoring |
| Real-world example | XOR-obfuscated malware strings, CTF challenges | Weak "encryption" in legacy software, CTFs |
The chi-square method scores a candidate by summing (observed - expected)^2 / expected over each letter's count - lower is more English-like. A simpler frequency-weight method just adds up a per-character score (high for common letters and space, zero for non-printable bytes) - higher is better. Both work; chi-square is more robust because it punishes a letter appearing too often, not just too rarely.
Breaking single-byte XOR (the core loop)
Everything reduces to this. Try each of the 256 possible key bytes, decrypt, and keep the highest-scoring result. Here is a complete, runnable Python attack:
# English letter/space frequencies as scoring weights (higher = more common).
FREQ = {
' ': 13.0, 'e': 12.7, 't': 9.1, 'a': 8.2, 'o': 7.5, 'i': 7.0,
'n': 6.7, 's': 6.3, 'h': 6.1, 'r': 6.0, 'd': 4.3, 'l': 4.0,
'u': 2.8, 'c': 2.8, 'm': 2.4, 'w': 2.4, 'f': 2.2, 'g': 2.0,
'y': 2.0, 'p': 1.9, 'b': 1.5, 'v': 1.0, 'k': 0.8, 'j': 0.15,
'x': 0.15, 'q': 0.10, 'z': 0.07,
}
def score(text_bytes):
"""Higher score = more English-like. Non-printable bytes are penalized."""
total = 0.0
for b in text_bytes:
if b == 0 or b > 126: # control / non-ASCII: this key is wrong
total -= 5.0
continue
total += FREQ.get(chr(b).lower(), 0.0)
return total
def break_single_byte_xor(ciphertext):
best = None
for key in range(256):
plain = bytes(b ^ key for b in ciphertext)
s = score(plain)
if best is None or s > best[0]:
best = (s, key, plain)
return best # (score, key_byte, plaintext_bytes)
# Demo: encrypt then break it
msg = b"The quick brown fox jumps over the lazy dog."
cipher = bytes(b ^ 0x42 for b in msg) # key = 0x42
s, key, plain = break_single_byte_xor(cipher)
print(f"Recovered key: 0x{key:02x}") # -> 0x42
print(f"Plaintext: {plain.decode()}")
The whole cipher collapses because the key space (256) is smaller than the number of characters in a typical message. There is simply nothing to search.
Breaking repeating-key XOR
When the key is several bytes long and cycles over the message, the attacker first has to learn the length. The trick is the Hamming distance - the count of differing bits between two byte strings.
Blocks that were encrypted with the same key bytes are more similar to each other than random data (English text is far from random, and XOR with the same bytes preserves that non-randomness). The correct key length may have a low normalized Hamming distance, though multiples and noisy samples can rank ahead of it:
With the length known, the ciphertext is transposed into columns - column 0 is bytes 0, N, 2N...; column 1 is bytes 1, N+1, 2N+1... - and each column is broken with the single-byte attack above. Here is the full pipeline:
def hamming(a, b):
"""Number of differing bits between two equal-length byte strings."""
return sum(bin(x ^ y).count("1") for x, y in zip(a, b))
def guess_keysize(ciphertext, lo=2, hi=40, blocks=4):
candidates = []
for size in range(lo, hi + 1):
if len(ciphertext) < size * blocks:
break
chunks = [ciphertext[i*size:(i+1)*size] for i in range(blocks)]
# average pairwise Hamming distance, normalized by key size
pairs = [(chunks[i], chunks[j])
for i in range(blocks) for j in range(i + 1, blocks)]
dist = sum(hamming(a, b) for a, b in pairs) / len(pairs) / size
candidates.append((dist, size))
return sorted(candidates) # use distance to guide, not decide, the search
def break_repeating_xor(ciphertext):
candidates = guess_keysize(ciphertext)
if not candidates:
raise ValueError("Need enough ciphertext to compare key sizes")
results = []
for _, size in candidates:
key = bytes(break_single_byte_xor(ciphertext[col::size])[1]
for col in range(size))
plain = bytes(c ^ key[i % size] for i, c in enumerate(ciphertext))
# Compare full plaintexts and prefer a simpler key when scores are close.
rank = score(plain) / len(plain) - 0.04 * size
results.append((rank, -size, key, plain))
_, _, key, plain = max(results)
return key, plain
# Demo
key = b"SECRET"
msg = (b"XOR with a repeating key is just a Vigenere cipher in binary, "
b"and it was broken in the nineteenth century. Frequency analysis wins.")
cipher = bytes(c ^ key[i % len(key)] for i, c in enumerate(msg))
recovered_key, plain = break_repeating_xor(cipher)
print(f"Recovered key: {recovered_key}") # -> b'SECRET'
print(plain.decode())
This follows the approach in Cryptopals Set 1, Challenge 6. Hamming distance guides the key-length search; scoring each recovered full plaintext avoids treating its lowest distance as certain. The small preference for shorter keys in the demo is a heuristic, so inspect the output when text is short or unusual.
Try it yourself
Encrypt a message with the XOR cipher below, then imagine running the loops above against the output - with a short key, they win every time. This is exactly why XOR is a learning tool and an obfuscation trick, not a real cipher.
The one case that doesn't break: the one-time pad
Everything above depends on the key being shorter than the message, so key bytes repeat and repetition leaks structure. Remove that assumption and the attack dies. A one-time pad uses a key that is truly random, at least as long as the message, and never reused. Now every possible plaintext of the right length is equally consistent with the ciphertext - "ATTACK AT DAWN" and "RETREAT AT SIX" can both be produced by some valid key - so there is no way to tell the real one from the fakes. That is Claude Shannon's perfect secrecy, and XOR with a one-time pad achieves it.
The catch is entirely operational: you need to generate, distribute, and destroy key material as large as everything you'll ever send, and never reuse a byte. Reuse even once and an attacker XORs two ciphertexts together to cancel the key, dropping straight back into frequency analysis on the combined stream. The math is perfect; the logistics of running local, private crypto are what make one-time pads impractical for general use and drive real systems to AES and modern key exchange instead.
The bottom line
Automated XOR cryptanalysis is fast because XOR hands back plaintext for any key guess, leaving only the easy problem of recognizing English. Single-byte keys fall to a 256-way brute force with frequency scoring; repeating keys fall to Hamming-distance length detection plus per-column frequency analysis. Runtime and confidence depend on the input and scoring; candidate key lengths should be checked against the recovered plaintext. XOR earns real security only as a one-time pad - random, message-length, single-use key - and the moment any of those conditions slips, the loops in this article take over.