Vernam cipher and the one-time pad: an unbreakable cipher
The Vernam cipher, also called the one-time pad, is the only cipher we know for certain that nobody can break. Not with the fastest computer, not in a thousand years. Yet it is almost the same as the Caesar cipher. Caesar shifts every letter of the message by the same number of places. Vernam shifts every letter by a different number, drawn at random each time. A cipher that any schoolchild can crack suddenly becomes one that even the secret services cannot manage. If you know the Vigenère cipher, you will recognise this as its strictest form.
How to encrypt
The key is a secret known only to the sender and the recipient. Here it is a row of random letters as long as the message, say XMCKL. We shift each letter of the message forward in the alphabet by as many places as the key letter beneath it says.
How many places that is comes from the letter’s number. We number the letters from zero:
| A | B | C | D | E | F | G |
|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 | 5 | 6 |
| H | I | J | K | L | M | N |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| O | P | Q | R | S | T | U |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| V | W | X | Y | Z | ||
| 21 | 22 | 23 | 24 | 25 |
We add the message letter and the key letter. When the sum goes over 25, the alphabet wraps around like the hands of a clock. Mathematicians call this counting mod 26: we subtract 26 from the sum as many times as we can and keep only the remainder, so
Let’s encrypt the message HELLO with the key XMCKL:
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| message | H | E | L | L | O |
| number | 7 | 4 | 11 | 11 | 14 |
| key | X | M | C | K | L |
| number | 23 | 12 | 2 | 10 | 11 |
| sum | 30 | 16 | 13 | 21 | 25 |
| mod 26 | 4 | 16 | 13 | 21 | 25 |
| cipher | E | Q | N | V | Z |
For the first letter, H is 7 and X is 23, making 30. That is over 25, so we subtract 26 and are left with 4, which is E. The other sums are below 26, so we leave them alone. The ciphertext is EQNVZ.
How Bára decrypts it
Adam sends EQNVZ to Bára. Bára has the same pad of keys as Adam, so she knows XMCKL too and subtracts the key letters from the cipher again. E is 4, X is 23 and 4 − 23 = −19. A negative number gives no letter, so we add 26 and get 7, which is H. The rest works the same way, and EQNVZ turns back into HELLO.
You can also do it without negative numbers and without adding. Count forward through the alphabet from the key letter until you reach the cipher letter. From X to E the steps are Y, Z, A, B, C, D, E, seven in all, and 7 is H. Encryption works the same way in reverse: from H you move 23 letters forward and land on E.
The one-time pad as a camp cipher
The key must be truly random. You cannot do it in your head, because people cannot write randomly: they repeat their favourite letters, and a piece of a word slips in anyway. Text from a book is no good either. It is not random: it contains words and common letters, so Eva can guess it just as she can guess the message itself.
At camp you can play the one-time pad for real, from about the age of 13:
- The camp leader prepares two identical pads of slips of paper with random letters, one for the sender and the other for the recipient. Draw the letters from a hat with 26 slips (write down the slip you drew and put it back) or with a random number generator from 0 to 25.
- Print the table A = 0 to Z = 25 from the section on encryption.
- Keep messages short, up to 20 letters, and write 20 key letters on each slip of paper.
- Each slip is used for one message and then burned. Hence the name: a pad for one use only.
- Younger children don’t need to work with numbers; they can simply count forward through the alphabet.
Why it cannot be broken
Imagine that Eva intercepts EQNVZ on its way. She can try keys for as long as she likes. The key XMCKL gives her HELLO. But the key LQMHI gives her TABOR (Czech for “camp”):
Check it: E − L is 4 − 11 = −7, and after adding 26 that is 19, which is T. For one ciphertext there is a key for every message of the same length. It is enough to subtract the message we want to get from the cipher, and the result is the key that leads to it. So from EQNVZ Eva can read HELLO, TABOR, PIVKO (Czech for “beer”) and ZEBRA.
She cannot tell which word is the real one, and that is the whole point. The keys were drawn at random, so they are all equally likely, and therefore every five-letter message is equally possible. Both HELLO and TABOR make sense and nothing tells them apart. The cipher tells Eva nothing except the length of the message.
Brute force does not help here. If Eva goes through all the keys, she gets all the five-letter words, and she knew that beforehand. Frequency analysis fails too, because every letter of the cipher appears equally often.
A little history
The first to describe a one-time pad was the American banker Frank Miller in 1882, in a telegraph codebook, but it was forgotten. It was reinvented by the engineer Gilbert Vernam of AT&T in 1917 for the teleprinter: both the message and the key were on punched paper tape, and the machine added them itself. He received a patent in 1919. The army officer Joseph Mauborgne helped establish that the key tape must be random and must never be used again. Historians still argue about which of them added which condition. The mathematical proof that the cipher really cannot be broken was published by Claude Shannon in 1949.
What happens when a key is used twice
Say that Adam is lazy and encrypts the next message, WORLD, with the same key XMCKL. He gets TATVO. Eva now has two ciphertexts, EQNVZ and TATVO, and subtracts them from each other letter by letter. She gets LQUAL. The key cancels out in the subtraction, and LQUAL is exactly HELLO − WORLD, that is, the difference of two messages.
That is something Eva can work with. Eva guesses that one message is HELLO and subtracts LQUAL. She gets WORLD, a meaningful word, so she guessed right. With a wrong guess she would get nonsense and would try another word.
The Soviet Union paid dearly for exactly this. During the Second World War it needed a huge number of pads, production could not keep up, and some pages were printed more than once. The Americans intercepted telegrams sent by Soviet official agencies. In 1943 they began to study them in a project called VENONA and soon found messages encrypted with the same page. They broke only a small part, but even that revealed Soviet spies, among them some who passed on the secrets of the American nuclear programme. The project ended only in 1980. The cipher was not defeated by mathematics. It was defeated by someone breaking the rule “only once”.
Why it is not used everywhere
If it is perfect, why don’t we encrypt everything with it? Because the key must be as long as everything you will ever send, and every piece of it is used only once. For every holiday photo you would need an equally large batch of random key. And you would have to get that batch to the other side securely somehow. Anyone who can securely deliver a key as long as the message could have delivered the message itself.
The internet therefore uses ciphers with a short key that the two sides agree on, for instance using the Diffie–Hellman key exchange, without having to hand it over beforehand. Computers also encrypt bits, not letters, and instead of adding mod 26 they add mod 2, which is called XOR.
Vernam cipher calculator
Type a message and a key with at least as many letters as the message. The calculator removes Czech accents and writes in lower case. Spaces and punctuation in the message stay in place and are not encrypted, and in the key only letters count. Try decrypting “eqnvz” with the key LQMHI.