Join our Newsletter — 33% off our NHI Course
Home› Guides› Key Exchange for Beginners: How Two Strangers Agree…
Guide Web & API Security

Key Exchange for Beginners: How Two Strangers Agree a Secret in Public

PDF for newsletter subscribers · free to read online

← All guides
By NHI Mgmt Group, under the editorial direction of Lalit Choda Updated 1 December 2026 11 min read
On this page

A key exchange lets two parties agree a shared secret key, even when others can see every message.

This guide is for anyone curious about how secure websites and apps start a private conversation. You do not need maths. If you have used a padlocked website, a key exchange has already happened on your behalf.

In short

  • Encryption needs a shared secret key, but the two sides often have never met and can only talk in public.
  • Key exchange solves this: each side keeps a private value and sends a public one, and both work out the same secret.
  • Modern web security makes a fresh secret for each session, so stealing one key later does not unlock old traffic.
  • A key exchange alone does not prove who is on the other end. Certificates and signatures do that part.

What you will learn

  • Explain why two strangers need a key exchange before they can encrypt.
  • Describe the Diffie-Hellman idea using the paint-mixing picture.
  • Explain forward secrecy in one sentence.
  • Know why identity checks and new quantum-safe methods matter.
Key
a secret value used to lock and unlock data.
Public channel
any route, such as the internet, where others may see the messages.
Shared secret
a value that only the two sides know at the end of the exchange.

What is key exchange?

Imagine two painters who want the same secret colour, but can only pass pots of paint through a busy room. They each start with the same public yellow. Each adds a secret colour of their own, swaps the mix, then adds their secret colour again. Both end with the same final colour.

A key exchange works the same way. Each side picks a secret number and sends out a public value made from it. Each then combines its own secret with the other side's public value. Both reach the same shared key [1].

The idea was published by Whitfield Diffie and Martin Hellman in 1976, to reduce the need for secret channels for handing out keys [1]. Its strength rests on a maths problem that is easy one way and very hard in reverse [1].

US government guidance describes key exchange as swapping public keys and other information to set up secure communication [2]. In the most common form, both sides add to the result, so neither one decides the key alone [3].

Where the comparison stops working: real paint can be analysed in a lab. The maths behind a key exchange has no known quick way to "unmix" it with today's computers.

Why does it matter?

Almost every secure connection starts with a key exchange. If it is weak, old or not checked properly, everything sent afterwards may be exposed.

If you run websites or APIs

What could happen: old settings allow connections without forward secrecy, so one stolen server key could expose past recorded traffic.

Check this: ask whether your services use modern TLS versions only.

Next: TLS and HTTPS for Beginners

If you build software that talks to other services

What could happen: code that skips certificate checks lets an impostor sit in the middle, even though the key exchange worked.

Check this: search your code and settings for options that turn off certificate checks.

Next: Certificate Errors for Beginners

If you plan long-term security

What could happen: older key exchange methods may not stand up to a future quantum computer.

Check this: list the systems that would need a quantum-safe method, starting with long-lived secrets.

Next: Crypto Agility for Beginners

What does it look like?

Here is a made-up summary of a secure connection, the kind a browser or test tool can show. It is simplified to the parts that matter.

Example only. All values are made up.

Connection to: shop.example.com                (1)
Protocol:      TLS 1.3                         (2)
Key exchange:  ECDHE (fresh for this session)  (3)
Server proved identity with: certificate       (4)
Session key:   0000-FAKE (never sent on the wire) (5)
  1. The site you are talking to.
  2. The version of the secure connection rules in use.
  3. The type of key exchange. In a real file, this line would contain the exact curve or group name.
  4. How the server proved who it is. This check is separate from the key exchange.
  5. The agreed key is worked out on each side. It never travels across the network.

Why this is risky: if line 4 were missing or ignored, you could agree a perfect secret with an impostor.

The safer way: always pair the key exchange with a proper identity check, and keep certificate checks switched on.

How does it work?

  1. Agree the public starting point. Both sides use a well-known starting value, like the shared yellow paint.
  2. Pick private values. Each side picks a random secret and keeps it to itself [1].
  3. Swap public values. Each side sends a value made from its secret. Anyone can see these.
  4. Work out the shared secret. Each side combines its own secret with the other's public value. Both get the same result [1].
  5. Check identity. In TLS 1.3, the server is always authenticated, using a certificate signature or a pre-shared key [4].
  6. Encrypt. The shared secret is turned into keys that protect the rest of the conversation.
Diagram titled The paint-mixing picture of a key exchange. Four boxes in a row joined by arrows. Box 1, Shared start: both agree a common colour in public, yellow. Box 2, Secret colours: each adds a private colour they never send, red and blue. Box 3, Swap mixes: they send their mixes in public, and an eavesdropper sees only the mixes. Box 4, Same result: each adds its own secret colour to the other's mix and gets the same final colour. A note says unmixing paint is hard, as is working back from public values to secrets, and that the mixes prove nothing about who sent them, so real systems such as TLS also check a certificate or signature.
Figure 1. Both sides reach the same secret colour, though only mixes cross the room.

Forward secrecy. If each session uses a fresh key exchange, stealing a key later does not unlock older sessions. NIST describes this as protection against the use of compromised old keys [5]. TLS 1.3 removed the older "static" methods, so every public-key based key exchange in it now gives forward secrecy [4].

Quantum-safe methods. In 2024, NIST published ML-KEM, a method two parties can use to set up a shared secret key over a public channel [6]. NIST says it is believed to be secure even against attackers with a quantum computer [6].

A worked example

This story is made up to show the idea.

Dario Yeboah runs the IT for a small accounting firm. Clients upload tax files through the firm's web portal. A client asks whether the uploads are private.

Dario checks the portal's connection details in his browser. It uses TLS 1.3 with a fresh key exchange for each session. The server proves who it is with a certificate from a trusted authority.

He then looks at an older file-transfer server the firm still uses for a few clients. Its settings allow very old protocol versions, including ones without forward secrecy.

Dario asks a contractor, Kofi Tanaka, to help. Kofi switches off the old versions and tests that every current client can still connect. Two clients with very old software need an update first, so Kofi helps them before the change goes live.

Kofi also finds a script that sends files to a partner with certificate checks turned off. The key exchange still worked, but the script would trust any server. He turns the checks back on.

Dario learned that a key exchange keeps the conversation private. Forward secrecy protects old sessions, and certificate checks make sure you are talking to the right party.

Myths and mistakes

What people thinkWhat is true
The secret key is sent across the network, just encrypted.In Diffie-Hellman, each side works out the key itself. Only public values travel[1].
A key exchange proves who you are talking to.It sets up a secret, not an identity. TLS adds certificates or pre-shared keys for that[4].
Once a server key is stolen, all past traffic is exposed.Not with forward secrecy. Fresh keys for each session protect older sessions[5].
Turning off certificate checks is fine for testing.A test setting left in by mistake lets an impostor sit in the middle unnoticed.
Quantum computers are a problem for later.NIST has already published ML-KEM, a key setup method designed to resist quantum computers[6]. Planning can start now.

What can I do this week?

  • Open a site you use at work and view its connection details in the browser. Note the TLS version.
  • Ask your web team whether old TLS versions are still switched on anywhere.
  • Search scripts and settings for options that skip certificate checks.
  • Read Public Key Cryptography for Beginners to see the public and private key idea in more depth.

Checklist

  • I can explain why two parties need a key exchange before they encrypt.
  • I can describe the paint-mixing picture in my own words.
  • I know the shared key is worked out on each side, not sent.
  • I know forward secrecy protects old sessions if a key is stolen later.
  • I keep certificate checks switched on in scripts and apps.
  • I know which systems still allow old protocol versions.
  • I know quantum-safe key exchange is on the way and why it matters.

Common questions

What is Diffie-Hellman?

It is the best-known key exchange method, published in 1976. Each side picks a secret, sends a public value made from it, and combines its secret with the other's value. Both reach the same key, and an eavesdropper sees only the public values.

What does forward secrecy mean?

It means each session gets its own fresh key, so stealing a long-term key later does not unlock older recorded sessions. TLS 1.3 provides it for all of its public-key based key exchanges.

Can someone in the middle break a key exchange?

They cannot easily work out the secret from the public values. They can try to pretend to be the other side, though. That is why TLS checks the server's identity with a certificate signature or a pre-shared key.

What is ML-KEM?

It is a newer NIST standard, published in 2024, for setting up a shared secret over a public channel. NIST believes it stays secure even against attackers with a quantum computer. Its job is the same as a classic key exchange: give both sides a shared secret to encrypt with.

Do machines and services use key exchange too?

Yes. Services, scripts and AI agents that call APIs over TLS go through the same key exchange as browsers. Their identity checks matter just as much, so keep certificate checks on in their settings too.

Where next?

Ready for the detail? Read the practitioner guide: Key Exchange and Diffie-Hellman Guide. It covers the methods, settings and common mistakes in depth.

Also useful:

Related NHI Mgmt Group resources: Hybrid Post-Quantum TLS Key Exchange Guide, Post Quantum KEMs vs Signatures and Symmetric vs Asymmetric Encryption Guide.

Key terms

Key exchange
A process where two parties swap public values to set up a shared secret for secure communication.
Key agreement
A kind of key exchange where both sides contribute, so neither decides the key alone.
Shared secret
A value known only to the two parties, used to make encryption keys.
Forward secrecy
Using fresh keys for each session so a key stolen later cannot unlock older sessions.
Man in the middle
An attacker who secretly sits between two parties and pretends to be each to the other.
ML-KEM
A NIST standard for setting up a shared secret that is designed to resist quantum computers.

Sources and further reading

  1. Whitfield Diffie and Martin E. Hellman, "New Directions in Cryptography", IEEE Transactions on Information Theory, vol. IT-22, no. 6, November 1976. https://ee.stanford.edu/~hellman/publications/24.pdf (accessed 1 Dec 2026).
  2. NIST Computer Security Resource Center, "Glossary: key exchange" (citing CNSSI 4009-2022). https://csrc.nist.gov/glossary/term/key_exchange (accessed 1 Dec 2026).
  3. NIST Computer Security Resource Center, "Glossary: key agreement". https://csrc.nist.gov/glossary/term/key_agreement (accessed 1 Dec 2026).
  4. IETF, "RFC 8446: The Transport Layer Security (TLS) Protocol Version 1.3", August 2018. https://www.rfc-editor.org/rfc/rfc8446.html (accessed 1 Dec 2026).
  5. NIST Computer Security Resource Center, "Glossary: perfect forward secrecy" (citing SP 800-77 Rev. 1). https://csrc.nist.gov/glossary/term/perfect_forward_secrecy (accessed 1 Dec 2026).
  6. NIST, "FIPS 203: Module-Lattice-Based Key-Encapsulation Mechanism Standard", 13 August 2024. https://csrc.nist.gov/pubs/fips/203/final (accessed 1 Dec 2026).

PDF for subscribers

Take this guide with you

Every section, table and checklist in one PDF, with links to the sources and related guides. Subscribe to our weekly NHI & AI Identity Journal and we’ll email you a download link.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

PDF Downloads for Newsletter Subscribers

Get the guide

Enter your details and we’ll email you a download link.

The full guide is free to read online. The PDF is a thank you for subscribing.

Fill in all fields marked * and tick both boxes to continue.