Search

Search papers and lessons

One-Time Pad

Level 2 · 13 min read

The one-time pad encrypts each bit with a truly random key bit of the same length as the message, used once, and that makes the ciphertext reveal nothing about the message.

On this page

  • Colour = topic mix
  • Discrete math
  • Probability
  • Calculus
  • Security & cryptography
  • Systems
  • Electrical engineering
  • ReadReady to readNot yet

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.

  1. Pick a random key. Flip a fair coin twice to get the key k=01k = 01. Check: the key has two bits, the same length as the message m=10m = 10, and each bit came from a fair coin.
  2. Encrypt with XOR. Compare the bits position by position. Position 1: 1⊕0=11 \oplus 0 = 1. Position 2: 0⊕1=10 \oplus 1 = 1. The ciphertext is c=11c = 11. Check: each output bit is 1 because the two input bits in that position differ.
  3. Send the ciphertext and consider what an observer learns. An eavesdropper sees 1111. Could the message have been 1010? Yes, with key 0101. Could it have been 0101? Yes, with key 1010, because 01⊕10=1101 \oplus 10 = 11. Could it have been 0000? Yes, with key 1111. Check: for each possible two-bit message there is exactly one key that produces 1111, so 1111 is consistent with every message.
  4. Decrypt with the same key. The receiver XORs the ciphertext with 0101: 11⊕0111 \oplus 01 gives 1010, the original message. Check: position 1 is 1⊕0=11 \oplus 0 = 1 and position 2 is 1⊕1=01 \oplus 1 = 0.
  5. Ask what happens if the key is not random. Suppose the key were always 0000. Then the ciphertext is c=m=10c = m = 10, and an eavesdropper reads the message directly. Suppose instead the key were fixed at 0101 and known to the attacker: then 1111 gives away 1010 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

Definition: One-time pad

Let nn be the message length in bits. The key KK is a random variable, uniform over all 2n2^n bit strings of length nn, so P(K=k)=2−nP(K = k) = 2^{-n} for every key kk, and KK is independent of the message MM. The ciphertext is C=M⊕KC = M \oplus K, and decryption is M=C⊕KM = C \oplus K.

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 m=m1m2…mnm = m_1 m_2 \ldots m_n and the key as k=k1k2…knk = k_1 k_2 \ldots k_n. Each bit of KK is a random variable too. Each key bit is 0 or 1 with probability one half, and the bits are independent. That gives P(K=k)=(1/2)n=2−nP(K = k) = (1/2)^n = 2^{-n}. For n=2n = 2, each of the four keys 00,01,10,1100, 01, 10, 11 has probability 1/41/4.

Step 2 in symbols: encryption bit by bit

ci=mi⊕kifor i=1,…,n,orC=M⊕K.c_i = m_i \oplus k_i \quad \text{for } i = 1, \ldots, n, \qquad\text{or}\qquad C = M \oplus K.

Here ⊕\oplus is addition modulo 2, the same idea as modular arithmetic with modulus 2, so 1⊕1=01 \oplus 1 = 0.

Step 3 in symbols: one key per ciphertext

Fix a message mm and a ciphertext cc. Exactly one key, k=m⊕ck = m \oplus c, turns mm into cc, because c=m⊕kc = m \oplus k forces k=m⊕ck = m \oplus c. So

P(C=c∣M=m)=P(K=m⊕c)=2−n.P(C = c \mid M = m) = P(K = m \oplus c) = 2^{-n}.

This value does not depend on mm. Every message gives the same chance of producing the same ciphertext.

Step 4 in symbols: decryption cancels the key

C⊕K=(M⊕K)⊕K=M⊕(K⊕K)=M⊕0=M.C \oplus K = (M \oplus K) \oplus K = M \oplus (K \oplus K) = M \oplus 0 = M.

This uses the facts a⊕a=0a \oplus a = 0 and a⊕0=aa \oplus 0 = a, 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 mm and every ciphertext cc with P(C=c)>0P(C = c) > 0,

P(M=m∣C=c)=P(M=m).P(M = m \mid C = c) = P(M = m).

Seeing the ciphertext changes nothing about what the attacker believes about the message. For the one-time pad, use the independence of KK and MM:

P(M=m′,C=c)=P(M=m′) P(K=m′⊕c)=P(M=m′) 2−n.P(M = m', C = c) = P(M = m') \, P(K = m' \oplus c) = P(M = m') \, 2^{-n}.

Add over all messages m′m' to get the chance of the ciphertext:

P(C=c)=∑m′P(M=m′) 2−n=2−n,P(C = c) = \sum_{m'} P(M = m') \, 2^{-n} = 2^{-n},

since the probabilities of all messages add to one. Divide the two:

P(M=m∣C=c)=P(M=m) 2−n2−n=P(M=m).P(M = m \mid C = c) = \frac{P(M = m)\, 2^{-n}}{2^{-n}} = P(M = m).

Key reuse in symbols

Suppose two messages are encrypted with the same key KK. Then

C1⊕C2=(M1⊕K)⊕(M2⊕K)=M1⊕M2,C_1 \oplus C_2 = (M_1 \oplus K) \oplus (M_2 \oplus K) = M_1 \oplus M_2,

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

Example
Example 1: two bits, all four keys (tiny)

The message is m=10m = 10. Find the ciphertext for each of the four possible keys.

  1. Key 0000: 10⊕00=1010 \oplus 00 = 10.
  2. Key 0101: 10⊕01=1110 \oplus 01 = 11.
  3. Key 1010: 10⊕10=0010 \oplus 10 = 00.
  4. Key 1111: 10⊕11=0110 \oplus 11 = 01.
  5. Check: the four ciphertexts 10,11,00,0110, 11, 00, 01 are all different. Each of the four possible outputs appears exactly once.
  6. Interpretation: if the key is uniform, every ciphertext is equally likely for this message, and seeing 1111 does not favour 1010 over any other message.
Example
Example 2: one letter, one byte (standard)

The letter H is the byte 0100100001001000, which is 72. The key is 1011001110110011, which is 179, and it is uniformly random.

  1. Encrypt: compare position by position. 0⊕1=10 \oplus 1 = 1, 1⊕0=11 \oplus 0 = 1, 0⊕1=10 \oplus 1 = 1, 0⊕1=10 \oplus 1 = 1, 1⊕0=11 \oplus 0 = 1, 0⊕0=00 \oplus 0 = 0, 0⊕1=10 \oplus 1 = 1, 0⊕1=10 \oplus 1 = 1. The ciphertext is 1111101111111011.
  2. Check the value: 128+64+32+16+8+0+2+1=251128 + 64 + 32 + 16 + 8 + 0 + 2 + 1 = 251.
  3. Now look at the letter I, which is 0100100101001001, which is 73. Which key would turn I into the same ciphertext? It is 251⊕73251 \oplus 73. Column by column the result is 1011001010110010, which is 178.
  4. Check: 01001001⊕10110010=1111101101001001 \oplus 10110010 = 11111011, the same ciphertext.
  5. 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 1/2561/256 of producing 251251. The observer cannot tell them apart.
Example
Example 3: a key used twice (realistic)

Two messages are sent with the same key K=10110011K = 10110011. The first is H, M1=01001000M_1 = 01001000, and the second is I, M2=01001001M_2 = 01001001.

  1. First ciphertext: C1=M1⊕K=11111011C_1 = M_1 \oplus K = 11111011, which is 251, as in Example 2.
  2. Second ciphertext: C2=M2⊕KC_2 = M_2 \oplus K. Column by column: 0⊕1=10 \oplus 1 = 1, 1⊕0=11 \oplus 0 = 1, 0⊕1=10 \oplus 1 = 1, 0⊕1=10 \oplus 1 = 1, 1⊕0=11 \oplus 0 = 1, 0⊕0=00 \oplus 0 = 0, 0⊕1=10 \oplus 1 = 1, 1⊕1=01 \oplus 1 = 0. So C2=11111010C_2 = 11111010, which is 250.
  3. An eavesdropper computes C1⊕C2=00000001C_1 \oplus C_2 = 00000001, which is 1.
  4. Check: M1⊕M2=01001000⊕01001001=00000001M_1 \oplus M_2 = 01001000 \oplus 01001001 = 00000001, the same value. The key cancelled out.
  5. 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 C1=11111011C_1 = 11111011 and C2=11111010C_2 = 11111010, both encrypted with one key. The attacker knows the first message is H, 0100100001001000. What is the second message?

Answer: C1⊕C2=00000001C_1 \oplus C_2 = 00000001, so M2=M1⊕00000001=01001001M_2 = M_1 \oplus 00000001 = 01001001, 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 cc, the possible messages are {c⊕k}\{c \oplus k\} over all keys kk. 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 P(M=m∣C=c)≠P(M=m)P(M = m \mid C = c) \neq P(M = m) 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 11011101 with key 01100110.Show answer

Column by column: 1⊕0=11 \oplus 0 = 1, 1⊕1=01 \oplus 1 = 0, 0⊕1=10 \oplus 1 = 1, 1⊕0=11 \oplus 0 = 1. The ciphertext is 10111011. Check: 1011⊕0110=11011011 \oplus 0110 = 1101, 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 mm and ciphertext cc, exactly one key, m⊕cm \oplus c, produces that pair, and every key has probability 2−n2^{-n}. So P(C=c∣M=m)=2−nP(C = c \mid M = m) = 2^{-n} does not depend on mm. Combined with the independence of key and message, this gives P(M=m∣C=c)=P(M=m)P(M = m \mid C = c) = P(M = m), 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 C1⊕C2=M1⊕M2C_1 \oplus C_2 = M_1 \oplus M_2. 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.

Sources