Two people who have never met agree on a secret key while an eavesdropper hears every word they exchange. That’s the trick Diffie-Hellman key exchange performs millions of times a second across the internet, and it can be done by hand with the number 23. Scaling it up shows where the security comes from. A brute-force attack on a 28-bit version took about 10 seconds in Python. A smarter attack broke a 40-bit version in half a second. The prime most often recommended today has 2,048 bits, and rebuilding it from the formula in its standards document, then checking it, takes a fifth of a second.
The paint analogy
Mixing colors in public
The usual explanation uses paint. Two people, call them A and B, publicly agree on a common color, say yellow. Each secretly picks a private color. Each mixes their private color into the yellow and sends the mixture across in the open. Each then adds their own private color to the mixture they received. Both end up with the same three-color blend: yellow plus A’s secret plus B’s secret.
Why the eavesdropper is stuck
Someone watching sees the yellow and both two-color mixtures. Getting to the final blend would mean separating a mixture back into its ingredients, and paint can’t be unmixed. The analogy works because the operation is easy one way and hard to reverse.
Where the analogy stops
Paint is a stand-in. The real version replaces mixing with modular exponentiation, raising a number to a power and keeping only the remainder after dividing by a prime. That operation is quick to do and, for the right primes, believed to be very hard to undo.

The math with small numbers
A toy exchange
The public values are a prime, p = 23, and a base, g = 5. A picks a secret, a = 6, and sends A = 5⁶ mod 23. B picks b = 15 and sends B = 5¹⁵ mod 23.
| Step | A computes | B computes |
|---|---|---|
| Public message | 5⁶ mod 23 = 8 | 5¹⁵ mod 23 = 19 |
| Shared secret | 19⁶ mod 23 = 2 | 8¹⁵ mod 23 = 2 |
Why both get 2
A raises B’s message to the power a, getting (5¹⁵)⁶ = 5⁹⁰. B raises A’s message to the power b, getting (5⁶)¹⁵ = 5⁹⁰. The exponents multiply in either order, so both land on 5⁹⁰ mod 23, which is 2. The eavesdropper sees 5, 23, 8 and 19.
The hard problem
To get the shared secret, the eavesdropper needs a or b: the exponent that turns 5 into 8 modulo 23. That’s the discrete logarithm problem. For p = 23 anyone can solve it by trying powers until one matches. The whole security of the method rests on making p large enough that trying doesn’t work.
Timing the attack
Two methods
The script generated “safe” primes of increasing size, primes p where (p − 1)/2 is also prime, picked a random secret exponent, and measured how long it took to recover that exponent. The naive method tries every power in turn. Baby-step giant-step, a classic algorithm, trades memory for time: it stores about √p values in a table and then searches in large jumps, cutting the work to roughly the square root of p.
Results
| Prime size | Example prime | Naive search | Baby-step giant-step |
|---|---|---|---|
| 16 bits | 54,287 | 0.0002 s | 0.00007 s |
| 20 bits | 944,039 | 0.025 s | 0.0003 s |
| 24 bits | 13,134,959 | 0.61 s | 0.0007 s |
| 28 bits | 231,327,407 | 10.6 s | 0.0031 s |
| 32 bits | 2,879,161,283 | 0.016 s | |
| 36 bits | 39,434,621,363 | 0.12 s | |
| 40 bits | 863,601,406,103 | 0.54 s |
What the growth says
The naive search slowed by about 16 to 20 times for every 4 bits added to the prime, roughly tracking the size of the prime itself. Baby-step giant-step slowed by about 4 to 8 times per 4 bits, tracking the square root. Extending the faster method’s trend from 40 bits to 2,048 bits would need about 2¹⁰⁰⁴ times longer, a number with over 300 digits, and a lookup table far larger than the number of atoms in the observable universe. Square-root attacks don’t threaten real key sizes. The dangerous attacks are cleverer ones.

When primes were too small
Logjam
The research team behind the Logjam attack, made public on May 20, 2015, showed that a man-in-the-middle could “downgrade vulnerable TLS connections to 512-bit export-grade cryptography,” a weakened mode left over from old export rules. At first, 8.4 percent of the top million web domains were vulnerable.
Precomputation
The team’s site explains why size alone wasn’t the whole story. The most demanding stage of the best known attack, the number field sieve, depends only on the prime, not on any particular conversation. Because many servers used the same few primes, one large precomputation against a common prime could then be reused to break individual connections quickly. The researchers ran it against the most common 512-bit prime and report that it let Logjam downgrade connections to about 80 percent of the servers supporting export ciphers.
1,024 bits
The site also estimates that breaking a 1,024-bit prime was within reach of a nation-state, and that breaking the single most common 1,024-bit prime would have allowed passive eavesdropping on about 18 percent of the top million HTTPS domains. Its recommendations were to disable export ciphers, use 2,048-bit groups, and prefer elliptic-curve Diffie-Hellman where available.
Building the 2,048-bit prime
The standard group
Internet standards publish fixed primes so that software doesn’t have to generate its own. RFC 3526, from May 2003, defines a 2,048-bit group whose prime is 2²⁰⁴⁸ − 2¹⁹⁸⁴ − 1 + 2⁶⁴ × {[2¹⁹¹⁸ π] + 124476}, with a generator of 2. The square brackets mean the integer part.
Rebuilding it from π
The formula can be checked directly. The script computed 2¹⁹¹⁸ × π to about 2,100 bits of precision, took the integer part, and assembled the prime. Its hexadecimal form begins FFFFFFFFFFFFFFFFC90FDAA22168C234 and ends with sixteen Fs, exactly 2,048 bits long. A probabilistic primality test confirmed it is prime, and that (p − 1)/2 is prime too, which makes it a safe prime. The checks took about 0.2 seconds.
Why π?
Building the prime from π is a way of showing that nobody chose it to hide a weakness. The digits of π can’t have been tweaked. As for the small number added at the end, a separate run tried every offset from 0 upward and found that 124476 is the first one that makes the result a safe prime, which fits a number found by searching rather than one chosen by hand. The top and bottom 64 bits of the prime are all ones.
Why “safe” matters
Size isn’t the only requirement. A further test built a 64-bit prime of a deliberately bad kind, one where p − 1 breaks down entirely into small factors, the largest of them 31. Recovering a secret exponent for that prime took SymPy’s discrete-log routine 0.0008 seconds, faster than baby-step giant-step managed on a safe prime with only 24 bits. When p − 1 splits into small pieces, the problem can be solved one small piece at a time and the answers stitched together, a method known as Pohlig-Hellman. A safe prime leaves p − 1 with one huge factor, so there are no small pieces to attack. That is why the RFC’s prime was checked for safety as well as primality, and why reusing a well-vetted published prime beats generating one carelessly.
How strong it is
RFC 3526 gives two estimates of the 2,048-bit group’s strength, about 110 bits and about 160 bits, and says the true figure likely lies between them. Even the lower figure means roughly 2¹¹⁰ operations, far beyond any computer that exists.

Where it stands now
What changed after 2015
The lessons from Logjam were practical. Export-grade cryptography was switched off, servers moved to 2,048-bit groups or larger, and much of the web shifted toward the elliptic-curve version of the exchange, which gets similar security from much smaller numbers.
What hasn’t changed
The idea itself is the same one A and B used with 23. Each side keeps a secret exponent, sends a public result, and combines the other side’s result with its own secret. Everything that makes the real version safe comes from the size and quality of the prime and from refusing to fall back to weaker ones.
Running it yourself
Python’s built-in pow function does modular exponentiation directly, so a full toy exchange fits in four lines. Swapping 23 for the RFC 3526 prime changes nothing in those lines except the numbers, and the exchange still runs in a fraction of a second. Reversing it is what can’t be done.
Sources
- Logjam research team, “Weak Diffie-Hellman and the Logjam Attack”: https://weakdh.org/
- RFC 3526, “More Modular Exponential (MODP) Diffie-Hellman groups for Internet Key Exchange (IKE),” May 2003: https://www.rfc-editor.org/rfc/rfc3526
