Afterglowscreensavers

Diffie–Hellman Key Exchange Screensaver

The Diffie–Hellman Key Exchange screensaver demonstrates the real modular-exponentiation math behind Diffie-Hellman, with two sides reaching a matching shared secret live. It is free, runs in your browser, and ESC exits, and uses small teaching-scale numbers, not production-strength cryptography.

Live preview

Diffie–Hellman Key Exchange screensaver — A real modular-exponentiation key exchange, step by step.

Press ESC to leave fullscreen. Settings are saved in your browser only.

How the Diffie–Hellman Key Exchange screensaver works

The saver picks a real small prime and a base, then generates two random secret exponents, one for each of two labeled sides.

Each side computes its real public value by raising the shared base to its own secret power, modulo the shared prime — genuine modular exponentiation, shown step by step.

The sides then exchange only their public values and each raises the other side's public value to their own secret power, arriving at a real matching shared secret.

The matching secret is confirmed on screen, then a fresh prime, base and pair of secret exponents begin a new exchange.

Sharing a secret in public

Until the mid-1970s, public work on cryptography assumed that two parties needed to share a secret key in advance. Whitfield Diffie and Martin Hellman published a method in 1976 that removes that need: two parties can agree on a shared secret by exchanging messages over an open channel, even though an eavesdropper hears everything. Ralph Merkle's public-key work was closely related, and in 2006 Hellman suggested the name Diffie-Hellman-Merkle key exchange to recognise it. It was later revealed that GCHQ researchers had shown public-key cryptography to be possible in 1969.

The security rests on the discrete logarithm problem. Raising a number to a power modulo a prime is easy, but working backwards from the result to the exponent is believed to be hard for large numbers. Each side keeps its exponent private and sends only the result of the easy direction, and the two end up computing the same value by different routes because of how exponents combine.

The scale matters. Toy primes like the ones used for a demonstration can be broken instantly, while the Logjam researchers recommend primes of at least 2048 bits, or preferably elliptic-curve cryptography. The original exchange also does not authenticate the parties, so a man in the middle could interfere unless the exchange is authenticated. TLS uses ephemeral Diffie-Hellman modes to give forward secrecy.

A worked example

Both sides independently arrive at the exact same final number despite each raising a different public value to their own different secret power — the real mathematical property that makes key exchange work.

Settings & tips

Frequently asked questions

Is this safe to use for real encryption?
No — it uses small, teaching-scale numbers for visual clarity, not the large primes real cryptographic systems require.
Is the underlying math real?
Yes — genuine modular exponentiation and the real Diffie-Hellman shared-secret property, just at a demonstration scale.
Is a key needed?
No — it is a pure local computation.
Is it free?
Yes — free, no download, in your browser.
What is the man-in-the-middle problem?
The original exchange does not verify who the parties are, so an attacker sitting between them could set up separate secrets with each. Authenticating the exchange closes that gap.
What is the discrete logarithm problem?
Given a prime, a base and a result, it asks for the exponent that produced it. It is believed to be hard for large numbers, which is what keeps the exchange secure.
Is Diffie-Hellman still used?
Yes. TLS uses ephemeral Diffie-Hellman modes to provide forward secrecy, because discarded ephemeral keys cannot be used to decrypt later.

← All Science & Research screensavers