 Your field operative Alice sends you a top-secret message: hqsoew. Many past spies have been executed after their messages were deciphered. But this time, you and Alice are using a one-time pad — the simplest and most secure cipher that exists!

In this tutorial, you’ll learn how to decipher Alice’s message, and save Alice and your country along the way.

Your first spy, Bob, used a different cipher. It worked like this:

Every letter in his secret message was replaced by the letter two places before it in the alphabet. For example, the message dead becomes the ciphertext bcyb.

The last ciphertext you received from Bob was mpylecq. What was the original message Bob sent?

No, but that sounds like something a spy would send, doesn’t it? Bob sent the word oranges, which was encoded as follows. First, he encodes the letter o. Write out the letters of the alphabet: you will find that o is the $15^{th}$ letter. He subtracts $2$, getting $15-2=13$. Then he looks up the $13^{th}$ letter of the alphabet, which is m. So he sends the letter m.

He does this for each letter in succession. The main tricky letter is a. For this, we ‘wrap around’ back to the end of the alphabet. See the picture above, where a goes to y.

Yes, Bob loved his oranges! Sadly, it was his downfall. Counter-intelligence deciphered Bob’s message, and he was arrested at the local orange grove.

You probably worked out the method to decode Bob’s secret message. Beforehand, you had agreed with Bob that he would shift each letter backwards by $2$ places. We say you agreed on the shift $\red{S_{bob}} = -2$. This is the secret amount that Bob adds to each letter. To decode the message, you subtract $\red{S_{bob}}$ from each letter. With $\red{S_{bob}} = -2$, you shift each letter forward by $2$ places.

But how did the enemy decode Bob’s message? The number $\red{S_{bob}} = -2$ was secret between you and Bob, so the enemy had to figure out the value of $\red{S_{bob}}$ in some other way.

To see the weakness, consider a ciphertext you intercepted from an enemy spy, Jon. The ciphertext is cbobobt. But what does it say? You suspect the word is another fruit 🍎🍇🍌🍍🍓, but you don’t know Jon’s secret shift value, $\pink{S_{jon}}$. Can you figure it out?

No, Jon’s secret shift is $\pink{S_{jon}} = 1$. The secret word was bananas, which was shifted forward by $1$ to produce cbobobt.

Not quite! It’s $1$, not $-1$. The number $\pink{S_{jon}}$ is the number of places that Jon shifts forward, not backward. The secret word was bananas, which was shifted forward by $1$ to produce cbobobt.

Yes, the word was bananas. You arrest Jon at the banana plantation, and have him executed in the main square.

Repeated letters in the message become repeated letters in the ciphertext. Look at those three as in bananas, all shifted forwards to b in the ciphertext cbobobt. Patterns in the message become patterns in the ciphertext. Cryptanalysts can use these patterns to figure out what the original message was!

The weakness in this cipher is that every letter is shifted by the same secret value, $\red{S_{bob}}=-2$. That is, the secret $\red{S_{bob}}$ is used repeatedly. To keep Alice safe, you have agreed to shift every letter by a different amount. Surely this will keep enemy prying eyes away!

You and Alice agree on the following sequence of shifts:

$\green{P_{alice}} = 7,1,3,3,0,4,8,5, \ldots$

To encode the first letter, Alice will shift it by $7$; to encode the second letter, Alice will shift it by $1$; and so on. You receive a ciphertext from Alice: hqsoew. What does it say?

No, that’s not it. Alice sent the word apples, encoded as follows. For the first letter, a, she took the first number from the list $\green{P_{alice}}$. That number is $7$. She then shifted a right $7$ places, to get h. For the second letter, p, she took the second number from the list above, which is $1$. She shifted p right $1$ place, to get q. And so on, using the shift sequence $7,1,3,3,0,4$.

Yes, she needs to meet in the apple orchard! 🍏

You and Alice are now using a one-time pad. This cipher, used correctly, is unbreakable! 🔐

In the next lesson, you’ll learn why this cipher is unbreakable, some ways the one-time pad can go wrong, and how to avoid those. Stay tuned!