The One-Time Pad and Why It Is Impractical
There is exactly one cipher with a proof that it can't be broken even by an attacker with unlimited computing power: the one-time pad. It is also almost never the right choice. Understanding both facts explains the design of every modern stream cipher, and the two ways the pad fails (reuse and malleability) come back in the lessons on CTR mode and nonce reuse.
How it works
Take a random key, the pad, exactly as long as the message. Encrypt by XOR-ing each byte of the plaintext with the matching byte of the pad; decrypt by XOR-ing again, since x ^ k ^ k == x.
plaintext T |
0x54 |
0101 0100 |
||
| pad | 0x8f |
1000 1111 |
XOR | |
| ciphertext | 0xdb |
1101 1011 |
Shannon proved in 1949 that if the pad is truly random, as long as the message, kept secret and never reused, the ciphertext gives no information about the plaintext: every plaintext of that length is equally likely given the ciphertext, because for each candidate there is exactly one pad that would produce it. This is called perfect secrecy. Brute force is useless: trying all pads produces all messages, the right one among them, with nothing to tell it apart.
A pad in Python, and its two failures
The pad below is fixed so the output is stable; a real pad would come from secrets.token_bytes(len(message)).
import secrets
def xor(a, b): return bytes(x ^ y for x, y in zip(a, b))
p1 = b"TRANSFER 0100 EUR TO ALICE"
pad = bytes.fromhex("8f1e2a77c3d4b5960a1b2c3d4e5f60718293a4b5c6d7e8f90a1b") # fixed for the lesson
assert len(pad) == len(p1)
c1 = xor(p1, pad)
print("c1 :", c1.hex())
print("decrypt :", xor(c1, pad).decode())
# Malleability: an attacker who guesses the format changes the amount without knowing the pad.
delta = xor(b"0100", b"9900")
forged = c1[:9] + xor(c1[9:13], delta) + c1[13:]
print("forged :", xor(forged, pad).decode())
# Reuse: a second message with the SAME pad.
p2 = b"MEETING MOVED TO 14:00 UTC"
c2 = xor(p2, pad)
x = xor(c1, c2)
print("c1^c2 == p1^p2:", x == xor(p1, p2))
print("guess p1 -> p2:", xor(x, p1).decode())
print("real pads come from:", type(secrets.token_bytes(26)).__name__, "secrets.token_bytes")c1 : db4c6b399092f0c42a2b1d0d7e7f2524d0b3f0fae696a4b0495e
decrypt : TRANSFER 0100 EUR TO ALICE
forged : TRANSFER 9900 EUR TO ALICE
c1^c2 == p1^p2: True
guess p1 -> p2: MEETING MOVED TO 14:00 UTC
real pads come from: bytes secrets.token_bytes
Failure 1: malleability. Flipping a ciphertext bit flips the same plaintext bit. The attacker never learned the pad but turned 0100 into 9900 by XOR-ing the ciphertext with "0100" ^ "9900". Perfect secrecy says nothing about integrity, which is why every XOR-based cipher (CTR mode, ChaCha20) is paired with a MAC.
Failure 2: reuse. With two messages under the same pad, c1 ^ c2 cancels the pad and leaves p1 ^ p2. Knowing or guessing one message gives the other. Even without a full guess, an attacker drags likely words ("cribs" such as the , HTTP/1.1, {"user":) along p1 ^ p2 and reads off fragments wherever the other plaintext becomes readable. This is the famous "two-time pad": the US VENONA project read Soviet messages from the 1940s because pages of one-time pads had been duplicated.
Why the one-time pad is impractical
- Key distribution. The pad is as long as all the traffic you will ever send. If you can deliver that much secret material safely in advance, you could often deliver the messages the same way.
- True randomness at volume. The proof needs uniformly random pads. Pads made from a password, a seeded
randomgenerator or a short key repeated are not one-time pads at all; they are weak stream ciphers. - Never reuse, ever. Every byte of pad must be destroyed after use, on both ends, with no backups that might be reused. Operational mistakes here are exactly what broke VENONA.
- No integrity. As shown, it needs a MAC anyway, and the MAC needs its own key.
What we use instead: stream ciphers
A stream cipher takes a short key (32 bytes) and a nonce and expands them into a long keystream that looks random to anyone without the key. Encryption is the same XOR as the pad. The security is now computational, not perfect: it holds as long as nobody can tell the keystream from random, which for ChaCha20 or AES in CTR mode means nobody with less than about 2^256 or 2^128 work. Both failures carry over unchanged: the same (key, nonce) twice is a two-time pad, and XOR is malleable, which is why the modern ciphers are AEADs with a built-in tag (Authenticated Encryption, Nonce Reuse).
How pad and keystream misuse is detected
- Code review: XOR loops over data with a fixed or repeated key (
data[i] ^ key[i % len(key)]), a keystream generated fromrandom.seed(...), a nonce that is a constant or a counter reset at restart. - Ciphertext analysis: identical ciphertext prefixes for messages with identical plaintext prefixes mean the keystream repeats. XOR two ciphertexts and look for many zero bytes or printable ASCII: a strong sign of reuse.
- Protocol review: no MAC or tag on XOR-encrypted data.
How it is fixed
- Don't build pads or keystreams yourself. Use an AEAD (
AESGCM,ChaCha20Poly1305) from a maintained library, which adds a tag against malleability. - Never reuse a (key, nonce) pair: random 96-bit nonces with a per-key message limit, a persistent counter, or a cipher with a 192-bit nonce (XChaCha20-Poly1305) where random nonces are safe at scale.
- Reserve true one-time pads for the rare case where key material can be couriered and destroyed, such as some diplomatic hotlines, and even then add authentication.
Pitfalls
- "Our pad is random" when it is
random.random()or derived from a password: that is a stream cipher of unknown strength. - Thinking a single reuse is harmless: one reuse exposes
p1 ^ p2for that pair forever. - Reading "perfect secrecy" as "perfect security": the pad does nothing about integrity or authenticity.
Practice
1. A log shipper XORs each line with the same 16-byte key repeated. You capture 1,000 encrypted lines. What can you do?
It is a many-time pad: XOR any two lines (at the same offsets) to remove the key, then use known structure (timestamps, log levels like INFO, IP addresses) as cribs. With 1,000 lines, simple frequency analysis per key position recovers the key. Fix: an AEAD with a fresh nonce per line or per batch.
2. In the example, the attacker wants the transfer to go to EVE instead of ALICE. What do they XOR, and where?
The last five ciphertext bytes (positions 21–25) with b"ALICE" ^ b"EVE ". They need to know (or guess) the original name and its position, not the pad.
3. Why can't you brute-force a correctly used one-time pad, even with unlimited computing?
Every possible plaintext of the right length corresponds to exactly one pad, so all candidates are equally consistent with the ciphertext; there is no way to recognise the right one.
Practice problem in 21 languages: Repeating-Key XOR.
Test yourself
What does c1 XOR c2 reveal when two messages share a pad?
- The pad
- p1 XOR p2
- Nothing
- The length of the pad only
Which property does the one-time pad lack even when used perfectly?
- Confidentiality
- Integrity
- Perfect secrecy
- Reversibility
What replaces the pad in a modern stream cipher such as ChaCha20?
- A password
- A keystream generated from a short key and a nonce
- A hash of the message
- A public key
Which historical project read enemy traffic because one-time pad pages were reused?
- Enigma at Bletchley Park
- VENONA
- SHAttered
- Deep Crack