Cybersecurity Topics: Threats, Defences and Careers · Course book
Chapter 6: The Diffie-Hellman Key Exchange
Symmetric encryption is fast and strong, but it has one awkward requirement: both parties must already share the same secret key. Two strangers communicating over the internet, where anyone might be listening, seem to have no safe way to agree on such a key. In 1976 Whitfield Diffie and Martin Hellman published a solution that still underpins almost every secure connection made today. The Diffie–Hellman key exchange lets two parties create a shared secret by exchanging only public messages, so that an eavesdropper who sees every message still cannot work out the secret. This chapter explains the idea with an everyday analogy, introduces the modular arithmetic that makes it work, follows a complete numerical example, and then examines the method's main weakness, the man-in-the-middle attack, along with how modern protocols defend against it and why ephemeral keys provide forward secrecy.
The key-sharing problem and a colour analogy
Suppose Alice and Bob want to encrypt their conversation with AES. They need a shared key, but every message they send can be read by an eavesdropper, traditionally called Eve. If Alice simply sends the key, Eve learns it too. What Alice and Bob need is a process in which each of them combines a private secret with public information, so that they end up with the same result while Eve, who sees only the public parts, cannot.
A popular analogy uses paint. Alice and Bob publicly agree on a common starting colour, say yellow. Each secretly chooses a private colour: Alice red, Bob blue. Each mixes their private colour with yellow and sends the mixture to the other. Eve sees yellow, the orange mixture and the green mixture. Alice now adds her secret red to Bob's green mixture, and Bob adds his secret blue to Alice's orange mixture. Both end up with the same final colour, yellow plus red plus blue, but Eve cannot produce it, because separating a mixture back into its ingredients is impractical. The mathematical version replaces mixing paint with an operation that is easy to perform but very hard to undo.
Modular arithmetic: the one-way operation
The operation Diffie–Hellman uses is modular exponentiation. In modular arithmetic, numbers wrap round after reaching a fixed value called the modulus, just as hours on a clock wrap round after 12. Writing x mod p means the remainder when x is divided by p; for example, 17 mod 5 = 2, because 17 = 3 × 5 + 2.
The protocol works with a large prime number p and a base number g called a generator. Computing ga mod p for a secret number a is quick, even for numbers hundreds of digits long, using a technique called repeated squaring. But going backwards, finding a when you know only g, p and the result, is the discrete logarithm problem, and for well-chosen large primes no efficient method is known. Part of the difficulty is that the results jump around unpredictably as the exponent increases, so there is no pattern to follow back.
The choice of numbers matters. The prime must be large, and the generator must be chosen so that its powers run through many different values before repeating; as Figure 6.1 shows, the powers of 2 modulo 29 visit every number from 1 to 28 before returning to 1, which is the ideal behaviour. Poorly chosen parameters, or primes that are too small, have caused real weaknesses in the past. In 2015, researchers showed that many servers still accepted deliberately weakened 512-bit parameters left over from old export rules, allowing connections to be downgraded and broken. Standard, well-studied parameter sets now avoid such mistakes, which is one reason developers are advised never to invent their own cryptography.
The exchange step by step
The protocol has five steps. Alice and Bob first agree publicly on a prime p and a generator g. Alice picks a secret number a and sends Bob A = ga mod p. Bob picks a secret b and sends Alice B = gb mod p. Alice computes Ba mod p, and Bob computes Ab mod p. Both results equal gab mod p, so they now share a secret, which can be turned into a symmetric encryption key.
Alice computes 185 mod 29. Working step by step: 182 = 324, and 324 mod 29 = 5; so 184 mod 29 = 52 = 25; and 185 mod 29 = 25 × 18 mod 29 = 450 mod 29 = 15, since 29 × 15 = 435.
Bob computes 311 mod 29. 33 = 27; 36 = 729, and 729 mod 29 = 4; 39 mod 29 = 4 × 27 = 108 mod 29 = 21; 311 mod 29 = 21 × 9 = 189 mod 29 = 15, since 29 × 6 = 174.
Both arrive at the shared secret 15. Eve knows 29, 2, 3 and 18, but to find the secret she would have to discover a = 5 or b = 11 from the public values.
| Value | Alice knows | Bob knows | Eve sees |
|---|---|---|---|
| Prime p = 29 and generator g = 2 | Yes | Yes | Yes |
| Alice's secret a = 5 | Yes | No | No |
| Bob's secret b = 11 | No | Yes | No |
| A = 3 and B = 18 | Yes | Yes | Yes |
| Shared secret 15 | Yes | Yes | No |
With numbers this small, Eve could find the secrets by trying every exponent from 1 to 28. Real implementations use primes of at least 2,048 bits, more than 600 decimal digits, or an equivalent method based on elliptic curves, which gives the same security with much smaller numbers and is now the most common choice. At those sizes, trying exponents one by one would take longer than the age of the universe.
The man-in-the-middle weakness
Diffie–Hellman protects against an attacker who only listens. It does not, on its own, protect against one who can intercept and replace messages. In a man-in-the-middle attack, Mallory sits between Alice and Bob. When Alice sends A, Mallory blocks it and sends Bob her own value instead; when Bob replies, Mallory does the same in the other direction. Alice unknowingly agrees a key with Mallory, and so does Bob. Mallory can now decrypt each message, read or alter it, and re-encrypt it for the other party, and neither Alice nor Bob notices anything unusual.
The defence is authentication: each party must be able to confirm that the public values really came from the other. In TLS, the protocol behind secure websites, the server signs its Diffie–Hellman values with the private key belonging to its certificate, and the browser checks that signature against the certificate, which has itself been signed by a trusted certificate authority. Mallory cannot forge that signature, so her substituted values are rejected. Messaging apps that use end-to-end encryption authenticate keys in similar ways and often let users compare a "safety number" or scan a code in person to confirm that no one is in the middle. This is a recurring lesson in cryptography: confidentiality is worthless without knowing who you are being confidential with.
Ephemeral keys and forward secrecy
Diffie–Hellman has a further benefit when used carefully. If Alice and Bob pick fresh secret numbers for every session and delete them afterwards, the method is called ephemeral Diffie–Hellman. Each session then has its own key, which exists only for the length of that conversation. This provides forward secrecy: even if an attacker records encrypted traffic today and, years later, steals the server's long-term private key, they still cannot decrypt the old sessions, because the session keys were never stored anywhere and cannot be recomputed from the long-term key.
Forward secrecy matters because some attackers, particularly well-resourced ones, collect large amounts of encrypted traffic in the hope of decrypting it later. The current version of TLS, version 1.3, requires ephemeral key exchange for this reason. Concerns about future quantum computers, which could solve the discrete logarithm problem efficiently, have also led to hybrid key exchanges that combine a classical elliptic-curve exchange with one of the newly standardised post-quantum algorithms, so that the session stays secure as long as either method holds.
Practice tasks
- Explain the paint-mixing analogy in your own words and identify which part represents the one-way operation.
- Calculate 23 mod 7, 100 mod 29 and 28 mod 29.
- Using p = 29 and g = 2, Alice chooses a = 3 and Bob chooses b = 7. Calculate A, B and the shared secret, and check that both sides agree.
- Describe step by step how a man-in-the-middle attacker defeats an unauthenticated key exchange.
- Explain how a website's certificate prevents the attack described in task 4.
- What is forward secrecy, and why might an attacker record encrypted traffic they cannot yet read?
This course book is available to read online only at chrysolites.org.