The Vigenère cipher gets more secure as its keyword gets longer, and the autokey cipher gets a keystream as long as the message. Both still have patterns an attacker can use: a repeating keyword, or a keystream made of English. The most secure choice is a keystream that is as long as the message and completely random.
History¶
The idea was first described in 1882 by Frank Miller, an American banker, in a codebook for sending telegrams. It was invented again in 1917 by Gilbert Vernam, an engineer at AT&T, who built a teleprinter that combined each character of a message with a character from a punched paper key tape (his patent was granted in 1919). Joseph Mauborgne, a U.S. Army officer, saw that the system could not be broken if the key tape was random and never used twice. The name comes from the pads of paper that the random keys were often printed on: use a sheet, then tear it off and destroy it.
The Rules¶
A one-time pad is unbreakable only if all of these rules are followed:
The key consists of truly random characters.
The key is at least as long as the plaintext.
Only two copies of the key exist: one for the sender and one for the receiver.
The key is used only once.
Both copies of the key are destroyed right after use.
That is very hard to pull off in real life. Each message needs a new key as long as the message, and both people need a copy of it before the message is sent. This key distribution problem is one of the biggest problems in cryptography, and Chapter 12 shows how modern cryptography solves it.
Enciphering a Message¶
With letters, a one-time pad is a Vigenère cipher whose keyword is a random string as long as the message. Here is an example:
plaintext: wewil linfi ltrat ethei rtree house atdaw n
OTP key: YYIVF QPUBV KCPKD GYJDS WFRTS GOMDX WXXVH R
ciphertext: UCEDQ BXHGD VVGKW KRQHA NYIXW NCGVB WQAVD Eotp_key = 'YYIVF QPUBV KCPKD GYJDS WFRTS GOMDX WXXVH R'
ciphertext = vigenere('We will infiltrate their tree house at dawn', otp_key)
ciphertext'UCEDQ BXHGD VVGKW KRQHA NYIXW NCGVB WQAVD E'Why It Can’t Be Broken¶
Suppose an attacker intercepts the ciphertext and tries to guess the key. Every key of the right length is equally likely, so every guess is as good as any other. One guess gives this:
other_key = 'BVAOM NIWCV ICZGD TNMAM TGEXF JOMEW FIWIA M'
vigenere(ciphertext, other_key, decrypt=True)'thepeopleinthetreehouseareourfriends'That reads “the people in the tree house are our friends”, a perfectly sensible message that is completely different from the real one. In fact, for any 36-letter message there is a key that turns this ciphertext into it: subtract the message from the ciphertext to get that key. So the ciphertext tells the attacker nothing except the length of the message. All 36-letter messages are equally likely, and there is no way to tell which is the real plaintext without the real key.
This is called perfect secrecy. Claude Shannon proved in the 1940s that the one-time pad has it, and that any cipher with perfect secrecy needs a key at least as long as the message.
Never Use the Pad Twice¶
If good keys are so hard to make and share, why not reuse one for a second message? Suppose the same key enciphers a second message, begin preparing all the water balloons now:
ciphertext2 = vigenere('begin preparing all the water balloons now', otp_key)
ciphertext2'ZCODS FGYQV BKCQD RJCKW SFKXJ HOXOL KKPIV N'An attacker who has both ciphertexts can subtract one from the other. The key cancels out:
The result isn’t readable, but the randomness is gone. What’s left is the difference of two English messages, and that can be cracked the same way as the autokey cipher: guess a probable word in one message and see whether English appears in the other. Deciphering the first ciphertext with the second as the “keyword” computes :
difference = vigenere(ciphertext, ciphertext2, decrypt=True)
difference'vaqaywrjqiuleuttioxevtyangojhqmglnir'If the attacker guesses that the second message starts with begin, adding begin to the difference gives the start of the first message:
vigenere(difference[:5], 'begin')'WEWIL'The attacker can now extend WEWIL to we will, use that to reveal more of the second message, and so on. A key used twice is no longer a one-time pad.
Pseudorandom Keys in Python¶
One way to build one-time pad keys in Python is with the random module. random.randint(0, 25) gives a “random” whole number from 0 to 25, which picks a letter from LETTERS. Both people need the same keystream, and random.seed(n) makes that possible: after setting the same seed, Python always produces the same sequence of numbers. The toolkit’s otp_keygen(message_length, seed_value, LETTERS=...) builds a keystream this way, and otp_vigenere(message, seed_value, decrypt=False, LETTERS=...) uses it to encipher or decipher.
print( otp_keygen(40, 1234) )
print( otp_keygen(40, 1234) )
print( otp_keygen(40, 1235) )YODACZSBVWCDYLHAAZALUTPTOECFWDAQPHCVROCT
YODACZSBVWCDYLHAAZALUTPTOECFWDAQPHCVROCT
WNRSPMEWLXUZNGTDNIOLTOVLYBKZXYGUHWMAARQR
secret = otp_vigenere('Three may keep a secret, if two of them are dead.', 1735)
print(secret)
print( otp_vigenere(secret, 1735, decrypt=True) )
print( otp_vigenere(secret, 1734, decrypt=True) )GCDQH NIZHS RDLGS YVWEI RVXXN BITAJ ZAOUG KO
threemaykeepasecretiftwoofthemaredead
dqtlvepeztnayylxijjwbysasevlapbpoaogc
With the right seed the message comes back. With a different seed it’s nonsense.
Notice that the seed is the real key here. The keystream is pseudorandom: it looks random, but it is completely determined by the seed. An attacker who could try every possible seed, or who could predict Python’s random number generator, could recover the keystream. So this is almost a one-time pad, but not quite.
Stream ciphers, which build long pseudorandom keystreams from short keys, are the subject of Chapter 10.
Frequency Analysis of a One-Time Pad¶
Here are the letter frequencies of the first 20,000 letters of Pride and Prejudice, enciphered with otp_vigenere:

The bars are all close to . Every letter is about equally likely, so frequency analysis has nothing to work with. In Chapter 8 you’ll measure how flat a distribution is with the index of coincidence. For this ciphertext it is:
round(ioc(otp_ciphertext), 4)0.0384That is almost exactly the value 0.0385 expected for letters chosen completely at random.