This is the most important chapter in Part II. Everything that follows, asymmetric encryption, Diffie-Hellman, certificates, the entire TLS handshake, exists because of this one problem.
We have AES. Itâs fast, itâs secure, and it can encrypt gigabytes of data per second. Both your browser and the server can use it. Thereâs just one problem.
Both sides need the same key.
Think of it like this. You and a friend want to pass secret notes in class. You agree to use a code where each letter shifts by 7 positions. Great. But how did you agree on the number 7? You had to whisper it to each other before class. If someone overheard the whisper, they can decode every note.
Thatâs the key distribution problem. You need a secure channel to share the key. But if you already had a secure channel, you wouldnât need encryption in the first place.
How do you get the key from your browser to the server? You canât send it over the internet, because the internet is the channel youâre trying to secure. If you send the AES key in plain text, anyone eavesdropping captures the key and can decrypt everything.
Itâs a chicken-and-egg problem. You need encryption to send the key securely. But you need the key to start encrypting.
In the old days, before the internet, you could solve this with physical key exchange. Two spies meet in a park, exchange codebooks, and use those codes for the next month. Banks would send encryption keys via armored courier.
This doesnât work for the internet. You connect to hundreds of different servers every day. Each connection needs its own unique key (reusing keys is a security disaster). You canât physically meet every server you want to talk to. And you canât pre-share keys with every website on the internet.
We need a way for two parties who have never communicated before to agree on a shared secret key, over a public channel, without anyone else being able to figure out what that key is.
That sounds impossible. How can you agree on a secret when everyone is listening?
In 1976, Whitfield Diffie and Martin Hellman published a paper that changed everything. They showed that itâs mathematically possible for two parties to agree on a shared secret over a public channel, even if an eavesdropper sees every message they exchange.
The idea relies on a special kind of math: operations that are easy to do in one direction but practically impossible to reverse. These are called one-way functions, or more precisely, trapdoor functions.
Weâll cover the actual mechanism (Diffie-Hellman key exchange) in Chapter 8. But first, letâs understand the broader concept that makes it possible: asymmetric encryption.
There are two ways to solve the key distribution problem:
Approach 1: Asymmetric encryption. One side generates a key pair (public key and private key). They publish the public key. The other side encrypts the AES key with the public key and sends it. Only the private key holder can decrypt it. Now both sides have the AES key.
Approach 2: Diffie-Hellman key exchange. Both sides contribute a piece of the puzzle publicly. Through mathematical magic, they both arrive at the same shared secret, but an eavesdropper who sees both public pieces canât compute the secret.
TLS has used both approaches historically. TLS 1.2 supported both RSA key exchange (Approach 1) and Diffie-Hellman (Approach 2). TLS 1.3 only supports Diffie-Hellman, because Approach 1 has a critical weakness weâll discuss later (hint: it breaks forward secrecy).
Both approaches rely on asymmetric cryptography. Letâs understand that next.