Perfect Secrecy and the One-Time Pad
Perfect secrecy is the cleanest possible confidentiality definition: after seeing the ciphertext, the adversary learns nothing about which message was sent. This is stronger than "hard to break" and stronger than "no known attack." It is an information-theoretic statement, so it does not depend on the attacker's running time or computing power.
The one-time pad is the central construction. Katz and Lindell present it as the canonical perfectly secret encryption scheme and use Shannon's theorem to explain its cost: the secret key must be at least as long as the message. Smart adds information-theoretic language such as entropy, equivocation, and unicity distance, which helps connect perfect secrecy to statistical leakage in classical systems.
Definitions
Let be a private-key encryption scheme with message space , key space , and ciphertext space .
The scheme is perfectly secret if for every probability distribution over , every message , and every ciphertext with ,
Equivalently, for every pair and every ciphertext ,
where is sampled by . This equivalent condition says that the ciphertext distribution is the same no matter which message was encrypted.
The one-time pad over -bit strings has:
Key generation samples uniformly. Encryption and decryption are:
Correctness holds because:
The phrase one-time is essential. Reusing the pad destroys perfect secrecy.
Key results
The one-time pad is perfectly secret. For any fixed and , exactly one key makes encrypt to , namely . Since all keys are equally likely,
for every . The ciphertext distribution is uniform and independent of the message.
Shannon's lower bound says that perfect secrecy requires enough key material. In the common finite setting where every message can be sent with positive probability and decryption is correct, any perfectly secret scheme must satisfy:
For -bit messages, this means the key must contain at least bits of uncertainty. The one-time pad is therefore optimal in key length, but not convenient: sender and receiver must share a fresh key as long as all data they will ever send.
The reuse attack explains why modern systems do not simply reuse a one-time pad. If
then
The key cancels. The adversary may not learn both messages immediately, but it learns a relation between them. Natural language, file headers, JSON syntax, and protocol constants often make that relation enough to recover large parts of the messages.
Perfect secrecy is different from computational secrecy. Perfect secrecy tolerates unlimited attackers but pays with long secret keys. Computational secrecy permits tiny leakage probabilities against efficient attackers and uses short keys, pseudorandom generators, and block ciphers. Modern cryptography mostly uses computational security because perfect secrecy is too expensive for ordinary network communication.
Proof sketch of Shannon's bound: fix a ciphertext that can occur. For every message , perfect secrecy requires that remain possible after seeing . Correct decryption means that for each key , the ciphertext decrypts to only one message. Therefore the number of messages that can remain possible after is at most the number of keys. If all messages must remain possible, .
The definition quantifies over message distributions because secrecy should not rely on messages being uniform. Real messages are highly nonuniform: yes may be more likely than a random 128-bit string, English text has redundancy, and protocol messages often start with predictable headers. Perfect secrecy says even if the adversary knows that prior distribution exactly, seeing the ciphertext does not update it. The posterior equals the prior.
Entropy gives another way to phrase the same idea. If is the message random variable and is the ciphertext random variable, perfect secrecy means:
The ciphertext does not reduce uncertainty about the message. This does not mean the ciphertext is useless to the receiver. The receiver has additional information, the key , and with the conditional entropy collapses:
for a correct deterministic decryption scheme.
The one-time pad also shows that information-theoretic security and usability can conflict. To send a gigabyte with perfect secrecy, the parties must already share a gigabyte of fresh uniform secret key. If they can securely share that much key in advance, they might also be able to share the message itself. This does not make the one-time pad irrelevant; it is useful in special high-assurance settings and as a conceptual benchmark. It tells us exactly what true secrecy costs.
Computational encryption can be understood as replacing the uniform pad with a pseudorandom pad and replacing equality of distributions with computational indistinguishability. That replacement is enormous. It gives practical communication from short keys, but the guarantee now depends on efficient attackers, assumptions, nonce discipline, and implementation behavior. Perfect secrecy remains the comparison point that makes those compromises visible.
Perfect secrecy also says nothing about authenticity. An attacker can flip any bit of a one-time-pad ciphertext, and the receiver's decrypted plaintext will have the corresponding bit flipped. The receiver has no way to tell from the pad alone whether the ciphertext was modified. This is why information-theoretic encryption still needs an authentication mechanism if active attacks are possible.
The key distribution problem is the main practical obstacle. If two parties share fresh secret pads through a physically secure courier, then the pad can protect later radio messages. If the parties are ordinary web clients and servers meeting for the first time, they need public-key key exchange or a KEM to establish shorter session keys. Perfect secrecy solves message leakage after the key exists; it does not solve how the key was delivered.
The definition is also all-or-nothing. A scheme that leaks the length of a variable-length message is not perfectly secret over a message space containing different lengths, unless length is excluded from the secret. Modern systems often accept length leakage, but perfect secrecy forces the designer to say exactly what the message space contains.
That precision is the enduring value of the definition.
Visual
| Property | One-time pad | Ordinary stream cipher |
|---|---|---|
| Key length | same as message | short seed key |
| Security type | information-theoretic | computational |
| Reuse allowed | never | nonce/state rules vary |
| Main proof idea | ciphertext uniform for every message | keystream indistinguishable from random |
| Main failure | reused pad leaks | nonce reuse can imitate pad reuse |
Worked example 1: encrypting and decrypting a one-time pad
Problem: encrypt with one-time key , then decrypt.
Method:
-
XOR bit by bit:
m = 1 0 1 1 0 0 1 0k = 0 1 1 0 1 1 0 0-----------------c = 1 1 0 1 1 1 1 0So .
-
Decrypt by XORing the same key:
c = 1 1 0 1 1 1 1 0k = 0 1 1 0 1 1 0 0-----------------m = 1 0 1 1 0 0 1 0 -
Algebraic check:
The checked answer is ciphertext and recovered plaintext .
Worked example 2: seeing why pad reuse leaks information
Problem: two 8-bit messages are encrypted with the same one-time pad:
Assume the attacker guesses that , the ASCII byte for lowercase a. Recover .
Method:
-
Compute the XOR of ciphertexts:
c1 = 0 0 1 1 0 1 1 1c2 = 0 1 0 1 0 1 1 0-----------------x = 0 1 1 0 0 0 0 1So .
-
Because the same key was used,
- Solve for :
- Check by deriving the key from the guess:
Then .
The checked answer is . The example is artificial, but the lesson is real: a single plaintext guess can expose another message when a pad is reused.
Code
import secrets
def otp_encrypt(message: bytes, key: bytes) -> bytes:
if len(message) != len(key):
raise ValueError("one-time pad key must match message length")
return bytes(m ^ k for m, k in zip(message, key))
message = b"attack at dawn"
key = secrets.token_bytes(len(message))
ciphertext = otp_encrypt(message, key)
recovered = otp_encrypt(ciphertext, key)
print(ciphertext.hex())
print(recovered.decode())
Common pitfalls
- Reusing a one-time pad. This is the classic catastrophic error.
- Generating the pad with a predictable pseudorandom source and still calling it perfect secrecy.
- Forgetting that perfect secrecy says the ciphertext leaks no information about the message, not that keys are easy to distribute.
- Treating compression, encoding, or a fixed XOR mask as an OTP.
- Using a key shorter than the message and cycling it, which recreates the Vigenere problem in binary form.
- Confusing "the attacker cannot read this one example" with a proof that the ciphertext distributions are identical.