Security

What is key exchange?

Key exchange is how two parties agree on a shared secret key over an untrusted network, usually with Diffie-Hellman, before symmetric encryption begins.

What is key exchange?

Symmetric ciphers such as AES are fast, but both sides need the same secret key before they can use one. Key exchange is the step that puts that key in place. When a browser connects to a site it has never visited, or a VPN client reaches a gateway, the two machines share nothing yet, and every packet crosses networks neither of them controls. Key exchange lets them finish with a shared key that an eavesdropper watching every packet cannot work out.

Keys can also be exchanged out of band, through a separate channel such as a phone call or a drive handed over in person. That works for a Wi-Fi passphrase or a site-to-site VPN pre-shared key, but it cannot scale to millions of web sessions. In-band key exchange, over the same network the data will use, relies on asymmetric cryptography.

How key exchange works

  • Key agreement: both sides contribute values and each computes the same secret, which never crosses the network. Diffie-Hellman (DH), published in 1976, and its elliptic curve form ECDH work this way.
  • Key transport: one side generates a random symmetric key and encrypts it with the other side's public key, usually RSA, so only the matching private key can unwrap it. S/MIME and PGP email commonly work this way. TLS 1.2 offered it as RSA key exchange, and TLS 1.3 removed it.

Either way the result is hybrid encryption: asymmetric math sets up the key, and a symmetric cipher such as AES-GCM or ChaCha20-Poly1305 protects the data itself.

A worked Diffie-Hellman example

With numbers small enough to check by hand, Alice and Bob publicly agree on a prime p = 17 and a base g = 3. Alice picks the private value 4 and sends 3^4 mod 17 = 13. Bob picks 7 and sends 3^7 mod 17 = 11. Alice computes 11^4 mod 17 = 4, and Bob computes 13^7 mod 17 = 4. Both now hold 4, yet an observer saw only 17, 3, 13 and 11. Reaching 4 requires one of the private values, which means solving a discrete logarithm: trivial here, infeasible with a 2048-bit prime or a curve such as X25519 or P-256.

In a TLS 1.3 handshake, the ClientHello carries the client's key share, the ServerHello returns the server's, and both sides derive the session keys from the shared secret. SSH and the IKE phase of IPsec use the same idea.

Securing the exchange

Plain Diffie-Hellman does not prove who is on the other end. An on-path attacker can run one exchange with each side and relay everything between them. The fix is authentication: in TLS the server signs the handshake with the private key behind its certificate, and the client checks that signature. The choice of method also decides what a stolen key exposes later.

  • Ephemeral DH (DHE, ECDHE): a fresh key pair for every session, discarded afterward, which gives forward secrecy.
  • Static RSA key transport: a stolen server private key decrypts every recorded session, one reason TLS 1.3 dropped it.
  • Group size: use 2048-bit DH groups or larger, or modern curves. The 2015 Logjam research showed that 512-bit export-grade groups could be broken quickly.
  • Post-quantum: NIST standardized ML-KEM in 2024 (FIPS 203), and major browsers now negotiate hybrid exchanges that pair X25519 with ML-KEM.

Key exchange vs. encryption vs. key management

Key exchange establishes a key; it encrypts nothing by itself. Encryption is what the symmetric cipher does with that key afterward. Key management is the wider life cycle of generating, storing, rotating and destroying keys, and exchange is one step in it. Diffie-Hellman is a key exchange algorithm, not an encryption algorithm and not an authentication method, and mixing those up is a classic wrong answer.

Key exchange on the CompTIA Security+ exam

Key exchange falls under SY0-801 objective 1.3, which covers choosing a suitable cryptographic solution and explaining why it fits. Scenario wording such as "agree on a session key over an untrusted network", "without ever sending the key" or "establish a shared secret" points to Diffie-Hellman or ECDHE. If the question adds that past sessions must stay private after a server key is stolen, the answer is ephemeral key exchange with forward secrecy. Common distractors are AES (it needs a key rather than delivering one), hashing, digital signatures (they authenticate the exchange but do not create the key) and RSA key transport when the question stresses forward secrecy.

Studying for CompTIA (Security)?

ExamWizardz turns the official objectives into a guided study plan — with practice tests, realistic PBQs, and a readiness score. Start free.

Start studying free

Related terms