Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Every cipher in this book so far has one weakness that has nothing to do with its mathematics: before Alice can send Bob a secret message, the two of them have to agree on a key, and nobody else can learn it.

Cryptographers use the same cast of characters in almost every example:

  • Alice wants to send a message.

  • Bob wants to receive it.

  • Eve is an eavesdropper. She can see everything Alice and Bob send to each other, but she can’t change it.

Sharing a Key Is Hard

Suppose Alice encrypts the message Hello! with the key DOG and sends the ciphertext to Bob. If Bob already knows the key, he decrypts the message and all is well. Eve sees the ciphertext, but without the key she’s stuck.

Now suppose they decide to change keys, and Alice picks CAT. She has to tell Bob the new key somehow. If she sends it over the same channel as her messages, Eve sees it too, and from then on Eve can read everything. If Alice had a safe way to send the key, she could just use that safe way to send her messages instead.

The problem also gets worse as more people join in. If every pair of people needs its own key, so that nobody can read messages meant for someone else, then a group of nn people needs

n(n−1)2\frac{n(n-1)}{2}

keys. Each of the nn people needs a key with each of the other n−1n - 1 people, and dividing by 2 stops you from counting the key Alice shares with Bob a second time as the key Bob shares with Alice.

3 people need 3 keys
10 people need 45 keys
30 people need 435 keys
100 people need 4950 keys
1000 people need 499500 keys

A school of 1,0001,000 students would need almost half a million keys, and every one of them would have to be delivered in secret.

The Old Solutions

For most of history, people solved this problem in person, ahead of time.

  • Codebooks and key lists. Armies, navies and governments printed books of codes or lists of daily keys and handed them out before they were needed. Every copy had to be guarded, and if one was captured, every message sent with it was at risk.

  • Secure couriers. Governments and banks paid trusted people to carry keys by hand. That works, but it’s slow and expensive, and it doesn’t work at all for talking to a website you’ve never visited before.

You saw the extreme version of this problem with the one-time pad: it is unbreakable, but Alice and Bob have to meet ahead of time and share a key as long as every message they will ever send.

Agreeing on a Key in Public

What Alice and Bob really want is a way to create a shared key while Eve listens to every word. That sounds impossible, but a picture with paint shows why it isn’t.

  1. Alice and Bob publicly agree on a starting color, white. Eve knows it too.

  2. Alice secretly picks red. Bob secretly picks blue. They never tell anyone their secret colors.

  3. Alice mixes white with her red and gets pink. Bob mixes white with his blue and gets light blue.

  4. Alice sends Bob her pink paint, and Bob sends Alice his light blue paint. Eve sees both cans.

  5. Alice adds her secret red to Bob’s light blue. Bob adds his secret blue to Alice’s pink. They both end up with the same color, a light purple made of white, red and blue.

Alice knowsEve knowsBob knows
Public colorwhitewhitewhite
Secret colorredblue
Sent acrosspink, light bluepink, light bluepink, light blue
Shared resultlight purple?light purple

Eve has white, pink and light blue. To make light purple she would need to un-mix pink to find Alice’s red, or un-mix light blue to find Bob’s blue. Mixing paint is easy, and un-mixing it is practically impossible.

One-way functions are the idea behind every system in this chapter.

The shared light purple isn’t a message. It’s a key. Once Alice and Bob share it, they can use it with a fast symmetric cipher, such as AES, for the rest of their conversation.

The next section, Diffie–Hellman Key Exchange, replaces the paint with numbers.