TL;DR
The one-time pad encrypts a message by XORing each bit with a key bit. The key must be truly random, as long as the message, and used only once. Under those conditions the ciphertext tells an observer nothing about the message: every message of the same length is equally consistent with what they see. Claude Shannon proved in 1949 that perfect secrecy of this kind needs a key at least as long as the message, which makes the one-time pad the simplest scheme that reaches it.
Builds on: Binary Numbers (Bits and Bytes), XOR, Conditional Probability · Leads to: Symmetric Encryption, and quantum key distribution, which makes the keys this scheme needs
Where it's used
Cold War spy traffic, and what went wrong. In the 1940s, US codebreakers working on the Venona project read Soviet cables that had been encrypted with one-time pad material. They could read them because some pages of key material had been used for more than one message. Reuse is the one mistake that breaks the scheme, and the rest of this lesson explains why.
Quantum key distribution. Two parties who have never met can use quantum channels to create a shared random key, and an eavesdropper who tries to measure the channel leaves traces that the parties can detect. The key they create is one-time pad material: random, as long as the messages they will send, and never reused. Quantum cryptography is the reason this scheme is not only a textbook idea.
Stream ciphers, a practical relative. Modern stream ciphers such as ChaCha20 generate a long keystream from a short secret key and XOR it with the data, the same structure as the pad. They are not one-time pads, because the keystream is generated rather than truly random, and they give up perfect secrecy in exchange for short keys. Their security then rests on the generator being hard to predict, which is a computational assumption, not a proof. Symmetric Encryption covers this family.
How it works
Here is the smallest case. The message is two bits long, so the key must be two bits long too.
- Pick a random key. Flip a fair coin twice to get the key . Check: the key has two bits, the same length as the message , and each bit came from a fair coin.
- Encrypt with XOR. Compare the bits position by position. Position 1: . Position 2: . The ciphertext is . Check: each output bit is 1 because the two input bits in that position differ.
- Send the ciphertext and consider what an observer learns. An eavesdropper sees . Could the message have been ? Yes, with key . Could it have been ? Yes, with key , because . Could it have been ? Yes, with key . Check: for each possible two-bit message there is exactly one key that produces , so is consistent with every message.
- Decrypt with the same key. The receiver XORs the ciphertext with : gives , the original message. Check: position 1 is and position 2 is .
- Ask what happens if the key is not random. Suppose the key were always . Then the ciphertext is , and an eavesdropper reads the message directly. Suppose instead the key were fixed at and known to the attacker: then gives away again. Check: a key the attacker can predict removes all the uncertainty, which is why the key must be random.
flowchart LR M["Message bits"] --> X["XOR, bit by bit"] K["Random key, same length, used once"] --> X X --> C["Ciphertext"] C --> Y["XOR with the same key"] K --> Y Y --> R["Original message"]
Step 3 is the one to remember. Seeing the ciphertext gives no preference to any message, because the key was equally likely to be any value.
The math
Let be the message length in bits. The key is a random variable, uniform over all bit strings of length , so for every key , and is independent of the message . The ciphertext is , and decryption is .
In words: each key is equally likely, the key is chosen without looking at the message, and encryption and decryption are both XOR with the key.
Step 1 in symbols: a uniform random key
Write the message as and the key as . Each bit of is a random variable too. Each key bit is 0 or 1 with probability one half, and the bits are independent. That gives . For , each of the four keys has probability .
Step 2 in symbols: encryption bit by bit
Here is addition modulo 2, the same idea as modular arithmetic with modulus 2, so .
Step 3 in symbols: one key per ciphertext
Fix a message and a ciphertext . Exactly one key, , turns into , because forces . So
This value does not depend on . Every message gives the same chance of producing the same ciphertext.
Step 4 in symbols: decryption cancels the key
This uses the facts and , together with associativity of XOR, all of which hold in Boolean algebra.
Step 5 in symbols: perfect secrecy
A scheme has perfect secrecy when, for every message and every ciphertext with ,
Seeing the ciphertext changes nothing about what the attacker believes about the message. For the one-time pad, use the independence of and :
Add over all messages to get the chance of the ciphertext:
since the probabilities of all messages add to one. Divide the two:
Key reuse in symbols
Suppose two messages are encrypted with the same key . Then
and the key cancels. Anyone who sees both ciphertexts learns the XOR of the two messages, which is often enough to recover both.
Worked examples
The message is . Find the ciphertext for each of the four possible keys.
- Key : .
- Key : .
- Key : .
- Key : .
- Check: the four ciphertexts are all different. Each of the four possible outputs appears exactly once.
- Interpretation: if the key is uniform, every ciphertext is equally likely for this message, and seeing does not favour over any other message.
The letter H is the byte , which is 72. The key is , which is 179, and it is uniformly random.
- Encrypt: compare position by position. , , , , , , , . The ciphertext is .
- Check the value: .
- Now look at the letter I, which is , which is 73. Which key would turn I into the same ciphertext? It is . Column by column the result is , which is 178.
- Check: , the same ciphertext.
- Interpretation: H with key 179 and I with key 178 give the same ciphertext. Each of the 256 possible keys is equally likely, so both letters have chance of producing . The observer cannot tell them apart.
Two messages are sent with the same key . The first is H, , and the second is I, .
- First ciphertext: , which is 251, as in Example 2.
- Second ciphertext: . Column by column: , , , , , , , . So , which is 250.
- An eavesdropper computes , which is 1.
- Check: , the same value. The key cancelled out.
- Interpretation: the attacker learns that the two messages differ only in their last bit. Knowing one message, or guessing it from ordinary English, reveals the other.
Try it: an attacker sees and , both encrypted with one key. The attacker knows the first message is H, . What is the second message?
Answer: , so , which is the letter I.
Common mistakes and tradeoffs
It is tempting to think that a random key alone makes the pad secure. In fact three conditions are needed: the key must be truly random, as long as the message, and used only once. Example 3 shows that reuse alone leaks the XOR of the two messages, even though each key bit was random.
It is tempting to think that a short key stretched over a long message is nearly as good. In fact a key shorter than the message cannot give perfect secrecy. For a given ciphertext , the possible messages are over all keys . If there are fewer keys than messages, some message cannot appear in that set, so the ciphertext rules it out. The attacker then learns something, and for that message.
It is tempting to think that XOR with a pseudorandom keystream is a one-time pad. In fact the pad's secrecy is a guarantee that holds against an attacker with unlimited computing power. A keystream from a generator has no such guarantee; its security depends on the generator being hard to predict, which is a claim about computation, not a proof.
When to use it: when two parties can share random key material as long as all the messages they will ever send, and when the messages must stay unreadable whatever computing power an attacker has.
When not to use it: for bulk data, because the key must match the total length of everything sent. It also fails when the key cannot be delivered securely, since key distribution is then as hard as the original problem.
Alternatives: Symmetric Encryption uses short keys and relies on computational hardness, which is practical for large data. Public-Key Cryptography lets strangers agree on a key without meeting. Quantum key distribution creates the random key material over a channel that reveals eavesdropping.
History
Gilbert Vernam, an engineer at AT&T, patented a teletype cipher in 1919. It combined each character of the message with a key tape using a bitwise operation equivalent to XOR. Vernam's key tape was a repeating loop, which made it vulnerable to reuse.
Joseph Mauborgne, of the US Army Signal Corps, later proposed that the key tape be random and used only once. That change turned Vernam's cipher into the one-time pad, and it is why the scheme has two names.
In 1949, Claude Shannon published the mathematical theory of secrecy systems. He showed that perfect secrecy requires a key space at least as large as the message space, and that the one-time pad with a random, single-use key reaches it. That result gave the scheme its firm footing.
The one-time pad caught on for a small set of high-stakes uses where its guarantee justified the cost. It stayed rare for bulk data because the key material has to be as large as the traffic it protects, and because reuse by careless operators broke it in practice.
Recap
- The one-time pad XORs each message bit with a key bit that is random, as long as the message, and used once.
- Each possible message has exactly one key producing a given ciphertext, so the ciphertext is equally consistent with every message of the same length. That is perfect secrecy.
- Reusing a key cancels it out, and the XOR of the two ciphertexts equals the XOR of the two messages.
- A key shorter than the message cannot give perfect secrecy.
- Read next: Symmetric Encryption, which trades perfect secrecy for short keys, and quantum key distribution, which creates the random keys a pad needs.
Check yourself
QuestionWhat three conditions must the key of a one-time pad satisfy?Show answer
The key must be truly random, it must be at least as long as the message, and it must be used only once. Random but reused keys leak the XOR of the messages, and short keys cannot give perfect secrecy.
QuestionCompute the ciphertext for message with key .Show answer
Column by column: , , , . The ciphertext is . Check: , the original message.
QuestionWhy does seeing the ciphertext tell an attacker nothing about the message, when the key is uniform and used once?Show answer
For any message and ciphertext , exactly one key, , produces that pair, and every key has probability . So does not depend on . Combined with the independence of key and message, this gives , so the ciphertext changes no belief about the message.
QuestionA team encrypts two different files with the same one-time pad, copying the same key bytes into each. What does an eavesdropper who gets both ciphertexts learn, and how should the team fix it?Show answer
The eavesdropper learns the XOR of the two plaintexts, because the shared key cancels in . For text files this is often enough to recover both. The fix is to use fresh, independent key material of at least the total length for every message, and never reuse any part of it.