Diffie-Hellman Key Exchange Explained (and Which DH Group to Use)

Diffie-Hellman key exchange explained with a worked example, real OpenSSL output and a table of DH groups. See which group to use for IPsec, SSH and TLS.
16min
Read Time
Wooden letter tiles spelling the word encryption on a desk, the topic of this Diffie-Hellman key exchange guide
Diffie-Hellman key exchange explained with a worked example, real OpenSSL output and a table of DH groups. See which group to use for IPsec, SSH and TLS.

TL;DR – Diffie-Hellman key exchange is a key agreement protocol. Two peers swap public numbers over an open link and each ends up with the same secret. It doesn’t encrypt anything, and it doesn’t prove who’s on the other end. – The ephemeral versions, DHE and ECDHE, give forward secrecy. TLS 1.3 runs one in every full handshake. – Which DH group to use: group 14 (2048-bit) is the floor. Pick group 19 for new IPsec tunnels, group 20 when you run AES-256, and group 31 when both ends support it. Groups 1, 2 and 5 are out, and newer Cisco IOS XE releases reject them by default. – Shor’s algorithm on a large quantum computer breaks both classic DH and ECDH. NIST IR 8547 (final, October 2026) deprecates them after 2030 and disallows them after 2035. Hybrid ML-KEM exchanges are already live in TLS and SSH.

Run openssl s_client -connect smenode-labs.com:443 -brief and read the last line. On 2026-11-18 ours said Negotiated TLS1.3 group: X25519MLKEM768. That line is a Diffie-Hellman key exchange, wearing a post-quantum add-on.

You’ve met Diffie-Hellman before, even if you never clicked on it. It’s the “DH group” drop-down in every IPsec tunnel, the diffie-hellman-group14-sha256 in an SSH error, and the “ECDHE” in a TLS cipher name. People pick the wrong setting because nobody explains what the number means.

This guide explains the Diffie-Hellman key exchange from the ground up. You’ll work through a small example by hand, run the same maths with real OpenSSL keys, see why a man in the middle breaks it, and leave knowing which DH group to use. Every standard and date below was checked against the source in November 2026.

Want to watch it negotiate in a live tunnel? The FortiGate NSE4 Lab Workbook has a 47-page VPN lab, site-to-site and remote access, that you build in EVE-NG.

What is Diffie-Hellman key exchange?

Diffie-Hellman key exchange is a protocol that lets two parties build the same secret key over a link anyone can read. Neither side ever sends the key. They send public values, and each side combines the other’s public value with a private number it keeps to itself. The result then feeds a symmetric cipher such as AES.

Diffie-Hellman explained in one line: it’s a way to agree on a key in public. If you searched “what is Diffie Hellman”, that’s the short answer. It’s the answer to one problem: how do two strangers agree on a secret when every message between them is visible?

Whitfield Diffie and Martin Hellman published the idea in 1976 in their paper “New Directions in Cryptography”. GCHQ later said that Malcolm Williamson had found essentially the same method in 1974. The work stayed secret until December 1997, according to the IEEE ETHW history of public-key cryptography.

Searchers type “Diffie-Hellman encryption” and “Diffie Hellman cryptography” as if it were a cipher. It isn’t. Diffie-Hellman never touches your data. It only produces the key that something else uses to encrypt it.

What is Diffie-Hellman used for?

It sits underneath most of the encrypted traffic you’ll configure:

  • TLS. Every TLS 1.3 handshake runs an (EC)DHE exchange to build the session keys.
  • IPsec VPNs. IKE uses a DH group in phase 1 and, with PFS, again in phase 2.
  • SSH. The client and server run a DH-based key exchange before login. The kex names such as curve25519-sha256 and diffie-hellman-group14-sha256 are those methods.

How does Diffie-Hellman work? A worked example

How does Diffie-Hellman key exchange work? In four steps. The Diffie-Hellman algorithm is short enough to fit on a napkin, so we’ll use tiny numbers and you can check every line. Real exchanges use numbers hundreds of digits long, and the steps are identical.

StepAliceOn the open wireBob
1Both agree on a prime p = 23 and a generator g = 5
2Picks a secret a = 6Picks a secret b = 15
3Computes A = 5^6 mod 23 = 8Alice sends 8, Bob sends 19Computes B = 5^15 mod 23 = 19
4Computes 19^6 mod 23 = 2Computes 8^15 mod 23 = 2

Both sides land on 2, and that’s how Diffie-Hellman key exchange works in full. It isn’t luck. Alice calculates (g^b)^a and Bob calculates (g^a)^b, and those are the same number because exponents multiply in either order.

Diffie-Hellman key exchange diagram: two laptops swap public values over an open wire and each builds the same shared key

A Diffie-Hellman key exchange diagram in one picture: only the public values cross the wire, and the two matching keys are built locally.

What does an eavesdropper see? She sees p = 23, g = 5, 8 and 19. To get the key she needs a or b.

With a prime this small she tries every exponent and finds a = 6 in at most 22 guesses. We checked that with a loop.

That attack is the discrete logarithm problem, and it’s the reason real groups use primes of 2048 bits or more. At that size nobody knows a way to reverse the exponent on a classical computer.

Want to check the maths yourself? Any online Diffie-Hellman calculator does the same three pow() calls. This one runs on any machine with Python:

python3 -c "
p,g,a,b=23,5,6,15
A=pow(g,a,p); B=pow(g,b,p)
print(A, B, pow(B,a,p), pow(A,b,p))"
# 8 19 2 2

A Diffie-Hellman example with real keys

Small numbers teach the idea. Real keys show the sizes. Here’s the same exchange with OpenSSL 3.5.5, first with a finite-field group (ffdhe2048 from RFC 7919) and then with X25519. We ran this on 2026-11-18.

# Finite-field Diffie-Hellman, 2048-bit group
openssl genpkey -genparam -algorithm DH -pkeyopt group:ffdhe2048 -out params.pem
openssl genpkey -paramfile params.pem -out alice.pem
openssl genpkey -paramfile params.pem -out bob.pem
openssl pkey -in alice.pem -pubout -out alice.pub
openssl pkey -in bob.pem   -pubout -out bob.pub
openssl pkeyutl -derive -inkey alice.pem -peerkey bob.pub   -out a.secret
openssl pkeyutl -derive -inkey bob.pem   -peerkey alice.pub -out b.secret
sha256sum a.secret b.secret

Both hashes matched, and the shared secret was 256 bytes. The same steps with -algorithm X25519 also matched, and that secret was 32 bytes. The hashes change on every run. The match doesn’t.

Same job, one eighth of the size. Keep that number in mind, because it explains why elliptic curves took over.

Is Diffie-Hellman symmetric or asymmetric?

Strictly, neither. Diffie-Hellman is a key agreement protocol. It uses key pairs, a private number and a public value, so textbooks file it under asymmetric (public-key) cryptography. What comes out is a symmetric key.

That answers “is Diffie-Hellman asymmetric?” with a qualified yes. It can’t encrypt a message, and it can’t sign one. Those jobs belong to other algorithms.

Diffie-Hellman vs RSA

Diffie Hellman vs RSA isn’t a contest. They do different jobs, and a TLS handshake often uses both.

Diffie-HellmanRSA
Main jobAgree on a shared secretSign data, or encrypt a small value such as a key
Encrypts messagesNoYes, small payloads only
Signs dataNoYes
Forward secrecyYes, when the exchange is ephemeralNo, when RSA transports a reused key
In TLS 1.3Runs in every handshakeSignatures only. Static RSA key exchange was removed

TLS 1.3 says it plainly in RFC 8446: “Static RSA and Diffie-Hellman cipher suites have been removed; all public-key based key exchange mechanisms now provide forward secrecy.” The certificate on this site is ECDSA, which we saw in the same OpenSSL output. DH builds the key. The certificate signature proves who you’re talking to.

Diffie-Hellman man in the middle attack: why the exchange alone isn’t enough

The Diffie-Hellman protocol has no idea who it’s talking to. A Diffie Hellman man in the middle attack exploits that. The attacker sits between the two peers, runs one exchange with each, and ends up holding two different keys.

Diffie-Hellman man in the middle attack diagram with an attacker holding a different key on each link

The attacker runs a separate exchange with each laptop. Each side believes it shares a key with the other, and the attacker reads and forwards everything.

The fix is authentication, and every protocol bolts it on. TLS signs the handshake with the server’s certificate. SSH checks the host key. IKE authenticates with a pre-shared key or certificates before it trusts the exchange.

Picture a VPN configured with a weak, widely shared pre-shared key. The DH maths is perfect and the tunnel is still open to a middleman, because the authentication check is the weak link. Diffie-Hellman gives you a key. Authentication tells you whose key it is.

Diffie-Hellman ephemeral: DHE, ECDHE and forward secrecy

Diffie-Hellman ephemeral means both sides create fresh private numbers for every session and throw them away afterwards. You’ll see it written DHE (finite-field) or ECDHE (elliptic curve). The opposite, a static key, reuses the same private number for many sessions.

Static versus ephemeral Diffie-Hellman keys: one static key opens three locked folders, three ephemeral keys open one folder each

A static key that leaks opens every recorded session. With ephemeral keys, one leaked key opens one session.

That property is forward secrecy. Suppose someone records your traffic today and steals a server key next year. With static keys they decrypt everything. With ephemeral keys the old sessions stay shut, because the per-session secrets no longer exist.

TLS 1.3 builds this in. RFC 8446 lists three key exchange modes: (EC)DHE, PSK-only, and PSK with (EC)DHE. It also notes that PSK-only mode loses forward secrecy, and that 0-RTT data isn’t forward secret either.

In IPsec you switch it on with PFS, which runs a fresh DH exchange when the tunnel rekeys. Fortinet’s own guide says a DH group is used in IKE phase 1 and optionally for PFS in phase 2.

Elliptic curve Diffie-Hellman (ECDH) key exchange

Elliptic curve Diffie-Hellman is the same idea with different maths. Instead of raising a number to a power, each side multiplies a fixed point on a curve by its secret. Alice and Bob swap the resulting points, multiply again, and reach the same point.

The payoff is size. A 256-bit curve gives about the same protection as a 3072-bit prime, which is why the X25519 secret above was a fraction of the ffdhe2048 one. Smaller keys mean smaller packets and faster handshakes.

You meet the elliptic curve Diffie-Hellman key exchange under different names:

  • TLS: the groups x25519 and secp256r1.
  • SSH: curve25519-sha256 and ecdh-sha2-nistp256.
  • IPsec: DH groups 19, 20 and 21 (the NIST P-256, P-384 and P-521 curves) and groups 31 and 32 (Curve25519 and Curve448).

Diffie-Hellman groups: what the numbers mean

A DH group is a fixed set of public parameters that both peers must agree on. For the older MODP groups it’s a prime and a generator. For the elliptic groups it’s a named curve. The group number comes from the IANA registry, and a bigger number doesn’t always mean stronger.

GroupTypeSizeVerdict
1MODP768-bitBroken. RFC 8247 says MUST NOT
2MODP1024-bitAvoid. RFC 8247 says SHOULD NOT
5MODP1536-bitAvoid. RFC 8247 says SHOULD NOT
14MODP2048-bitThe floor. RFC 8247 says MUST
15MODP3072-bitStronger and heavier. Fine when elliptic curves aren’t available
19ECP (P-256)256-bitModern default. About 128-bit strength. RFC 8247 says SHOULD
20ECP (P-384)384-bitAbout 192-bit strength
21ECP (P-521)521-bitAbout 256-bit strength
31Curve25519256-bitCisco recommends it where supported
32Curve448448-bitCisco recommends it where supported

RFC 8247 only ranks groups 14, 19 and the weak ones. It doesn’t mention 15, 16, 20 or 21, so the strength labels for those come from Cisco’s and Fortinet’s own guides. RFC 3526 puts group 14 at roughly 110 bits of strength on its more conservative estimate.

MODP versus elliptic curve Diffie-Hellman: a very large key and a small key that fit the same padlock

Group 14 uses a 2048-bit prime for roughly 110 bits of strength. Group 19 reaches about 128 bits with a 256-bit curve.

Ready to see a group mismatch for yourself? Build the lab, change the group on one side, and watch the tunnel fail. The FortiGate NSE4 Lab Workbook walks you through the site-to-site VPN build first, and the CCIE Security Workbook goes deeper on IKEv2.

Which DH group should you use?

Use the strongest group both peers support, and don’t go below 14. Here’s the decision in one table.

Your situationGroup to pick
New IPsec tunnel between current Cisco, FortiGate or similar gear19 with IKEv2
Same tunnel, but you run AES-25620
Both ends support Curve2551931
Older gear or a third-party peer that only speaks MODP14
Anything on groups 1, 2 or 5Change it

Fortinet’s guide says the DH group should match the strength of the symmetric key, so AES-256 gains little when it’s paired with group 19. It also says to use IKEv2 for all new deployments. Both sides need at least one group in common, or the tunnel never comes up.

Diffie-Hellman group 14: the floor

Group 14 is the 2048-bit MODP group, and RFC 8247 lists it as MUST for IKEv2. That makes it the safe common ground between vendors. It’s also slower than the elliptic groups, so treat it as the compatibility choice rather than the first choice.

You’ll also see it in SSH as diffie-hellman-group14-sha256. More on that below.

Diffie-Hellman group 20 and group 21

Group 20 is the 384-bit curve and group 21 is the 521-bit curve. Pair group 20 with AES-256 and you’ve matched the strengths. Diffie-Hellman group 21 reaches the 256-bit level, and most teams keep it for peers that insist on the top tier.

Groups 1, 2 and 5: leave them behind

Cisco has been pulling these out. Its field notice, last updated 2026-01-20, says newer IOS XE releases reject DH groups 1, 2 and 5 in IKEv1 policies by default, and groups 1, 2, 5 and 24 in IKEv2 proposals. On routers that starts at 17.11.1a, and on Catalyst 9200 to 9500 switches at 17.15.1. The only workaround is crypto engine compliance shield disable, and Cisco calls it a last resort.

Picture a branch router that’s run for years with group 2. Someone upgrades it to a current IOS XE release over a weekend. On Monday the tunnel is down, and nothing in the VPN config changed. The new release simply refuses the old group.

We covered the same pattern on the ASA side in our L2TP over IPsec guide, where ASA 9.15(1) removed groups 1, 2 and 5 from IKEv1 policies.

Four mistakes that break or weaken a tunnel

  • Assuming a bigger number is stronger. Group 19 is far stronger than group 5.
  • Pairing AES-256 with group 14 or 19. You paid for 256-bit encryption and get less from the key exchange.
  • Leaving group 2 on for “compatibility”. The peer you’re accommodating needs an upgrade.
  • Forgetting that phase 2 has its own PFS group. Phase 1 matching doesn’t help if phase 2 doesn’t match.

How to set the Diffie-Hellman group on Cisco and FortiGate

These snippets show only the DH-related lines, not a full tunnel. They follow Cisco’s and Fortinet’s documented syntax, and nobody ran them in a lab for this article, so test them in EVE-NG first.

On Cisco IOS XE with IKEv2:

crypto ikev2 proposal DH-LAB
 encryption aes-cbc-256
 integrity sha256
 group 20
!
crypto ipsec profile DH-LAB-PROFILE
 set pfs group20

On a FortiGate:

config vpn ipsec phase1-interface
    edit "to-branch"
        set ike-version 2
        set proposal aes256-sha256
        set dhgrp 20
    next
end
config vpn ipsec phase2-interface
    edit "to-branch-p2"
        set phase1name "to-branch"
        set proposal aes256-sha256
        set pfs enable
        set dhgrp 20
    next
end

If the two ends share no group, IKEv2 answers with the notify NO_PROPOSAL_CHOSEN and the tunnel stays down. On Cisco, show crypto ikev2 sa detailed prints the group that the finished exchange used, in the DH Grp field.

Picture a head office FortiGate and a branch router. One side offers group 20 only, and the other accepts groups 14 and 19. Nothing’s broken and no password is wrong, because the two lists simply don’t overlap. Add group 19 to the first side and the tunnel comes up.

Diffie-Hellman in SSH: group1-sha1, group14-sha1 and what the names mean

SSH names its key exchange methods after the group and the hash. diffie-hellman-group14-sha256 means the 2048-bit MODP group 14 with SHA-256. The names look like the IKE group numbers, but there’s a trap.

In SSH, diffie-hellman-group1-sha1 doesn’t use IKE group 1. RFC 4253 says the method is “named using the phrase group1, even though it specifies the use of Oakley Group 2”, which is the 1024-bit group. And diffie-hellman-group14-sha1 uses the 2048-bit Oakley Group 14.

MethodRFC 9142 status (January 2022)Before
diffie-hellman-group1-sha1SHOULD NOTMUST
diffie-hellman-group14-sha1MAYMUST
diffie-hellman-group14-sha256MUSTnone
diffie-hellman-group16-sha512SHOULDnone
ecdh-sha2-nistp256SHOULDMUST

OpenSSH has moved on. Release 10.0 (2025-04-09) made the hybrid mlkem768x25519-sha256 the default in the ssh client. It also removed the diffie-hellman-group* and group-exchange methods from the default list in the sshd server.

On OpenSSH 10.2, the version on our server, the client’s default list starts with mlkem768x25519-sha256 and ends at diffie-hellman-group14-sha256. group1-sha1 is still compiled in, but only if you ask for it.

Picture an engineer who runs a 2012 switch and gets this error after a laptop upgrade:

Unable to negotiate with 192.0.2.10 port 22: no matching key exchange method found.
Their offer: diffie-hellman-group1-sha1

Nothing’s wrong with the laptop. The switch only offers the old 1024-bit method, and the new client refuses it by default. Re-enable it for that one host, and plan the firmware upgrade:

Host old-switch
    HostName 192.0.2.10
    KexAlgorithms +diffie-hellman-group1-sha1

We confirmed that ssh -G accepts the + form and appends diffie-hellman-group1-sha1 to the end of the list. Never set this for Host *.

Does TLS use Diffie-Hellman? Is Diffie-Hellman still used?

Yes to both. Does TLS use Diffie-Hellman? Every TLS 1.3 handshake that doesn’t resume a session runs an (EC)DHE exchange.

Is Diffie-Hellman still used? It’s everywhere, mostly as ECDHE with X25519. Finite-field DH hangs on in IPsec and older SSH.

We tested our own site with OpenSSL 3.5.5 on 2026-11-18:

  • A default TLS 1.3 handshake negotiated X25519MLKEM768.
  • Offering only X25519 or only secp256r1 also worked.
  • Offering only ffdhe2048 got a handshake_failure alert.
  • Forcing TLS 1.2 chose ECDHE-ECDSA-CHACHA20-POLY1305.

So the server accepts elliptic-curve exchanges and refuses a finite-field-only client. That’s modern practice. The weak spot the industry cleaned up came from the finite-field side.

In 2015 researchers published Logjam. A man in the middle could downgrade a TLS connection to 512-bit export-grade DH and read the traffic, and 8.4% of the top 1M domains were vulnerable at first.

The authors also estimated that a nation-state could break one common 1024-bit prime, which would expose passive eavesdropping on about 18% of those domains. Their advice was a 2048-bit group, and RFC 7919 later standardized named finite-field groups for TLS (ffdhe2048 up to ffdhe8192) so peers stopped inventing their own.

Is Diffie-Hellman safe against quantum computers?

Not against a large enough quantum computer. Shor’s algorithm solves the discrete logarithm problem, which breaks classic DH and ECDH alike. Attackers can record traffic now and decrypt it later, which is the “store now, decrypt later” risk Fortinet’s DH guide warns about for site-to-site tunnels.

The fix isn’t to drop Diffie-Hellman. It’s to run it alongside a post-quantum exchange, so you’re safe if either one holds.

Hybrid key exchange: an ECDH key and an ML-KEM key combine into one shared key

A hybrid exchange runs ECDH and ML-KEM together and feeds both results into one key.

Here’s where that stands in November 2026:

  • TLS. RFC 10024 (August 2026, Proposed Standard) defines X25519MLKEM768, SecP256r1MLKEM768 and SecP384r1MLKEM1024. Our own site negotiated the first one.
  • SSH. OpenSSH 10.0 made mlkem768x25519-sha256 the default.
  • IPsec. RFC 9370 (May 2023) lets IKEv2 run multiple key exchanges in one negotiation, which is how hybrid tunnels work. Fortinet recommends post-quantum key exchange on site-to-site tunnels, and the remote peer has to support it as well.
  • Policy. NIST IR 8547, final in October 2026, deprecates finite-field DH and ECDH after 2030 and disallows them after 2035.

What should you do this quarter? Move tunnels to groups 19, 20 or 31, retire anything below group 14, and ask your vendor when hybrid ML-KEM reaches your platform.

Frequently asked questions

What is Diffie-Hellman?

Diffie-Hellman is a key agreement protocol from 1976. Two parties exchange public values over an open link and each computes the same shared secret. The secret then keys a symmetric cipher such as AES. It doesn’t encrypt data itself.

How does Diffie-Hellman key exchange work?

Both sides agree on public parameters, pick a private number, and send a public value built from it. Each side combines the other’s public value with its own private number. Both arrive at the same secret, and an eavesdropper who sees only the public values can’t compute it.

What is Diffie-Hellman used for?

It creates the session keys in TLS, IPsec (through IKE) and SSH. It’s the step that lets two peers who’ve never met agree on an encryption key. Authentication, such as certificates, is added separately.

Is Diffie-Hellman symmetric or asymmetric?

It’s a key agreement protocol that uses asymmetric-style key pairs and produces a symmetric key. Most references file it under asymmetric cryptography. It can’t encrypt or sign on its own.

What’s the difference between Diffie-Hellman and RSA?

Diffie-Hellman agrees on a shared secret. RSA signs data or encrypts small values. TLS 1.3 removed static RSA key exchange, so DH (usually ECDHE) builds the key and a signature, from RSA or ECDSA, proves identity.

Does TLS use Diffie-Hellman?

Yes. TLS 1.3 uses (EC)DHE in every full handshake, and static RSA and static DH suites were removed. Most connections today use ECDHE with X25519, and some now use the hybrid X25519MLKEM768.

Is Diffie-Hellman still used?

Yes. It’s the standard way to agree on keys in TLS, SSH and IPsec. What changed is the flavour: elliptic curves replaced most finite-field groups, and hybrid post-quantum versions are rolling out.

Which Diffie-Hellman group should I use for IPsec?

Use group 19 for new IKEv2 tunnels, group 20 with AES-256, and group 14 only for compatibility. Use group 31 where both peers support it. Avoid groups 1, 2 and 5.

What’s the difference between DHE and ECDHE?

Both are ephemeral, so each session gets fresh private numbers and forward secrecy. DHE does the maths with a large prime. ECDHE does it on an elliptic curve, with much smaller keys and faster handshakes.

Can a quantum computer break Diffie-Hellman?

A large enough quantum computer running Shor’s algorithm can break classic DH and ECDH. Hybrid exchanges that add ML-KEM, such as X25519MLKEM768, protect against that. NIST IR 8547 sets 2030 for deprecation and 2035 for disallowing the classic versions.

Key takeaways

  • Diffie-Hellman agrees on a key. It doesn’t encrypt data, and it doesn’t authenticate the peer.
  • Without authentication, a man in the middle holds two keys. Certificates, signatures or a strong pre-shared key close that gap.
  • Ephemeral exchanges (DHE and ECDHE) give forward secrecy, and TLS 1.3 runs one in every full handshake.
  • Group 14 is the floor. Pick group 19, 20 or 31 for new tunnels, and retire groups 1, 2 and 5.
  • In SSH, group1 means the 1024-bit Oakley Group 2, not IKE group 1.
  • Shor’s algorithm breaks classic DH and ECDH. Hybrid ML-KEM exchanges are already shipping, and NIST sets 2030 and 2035 for the classic versions.

Next step: build a two-site VPN in EVE-NG, change the DH group on one side, read the failure, then fix it. The FortiGate NSE4 Lab Workbook has the site-to-site build, and Install EVE-NG gets the lab running. For the VPN family around this topic, read DMVPN Explained, and for exam-style questions on key exchange try the Security+ Practice Test.

Sources

Keep Reading

Related Articles

Ethernet ports and cables on the back of a router, with SMEnode Labs cyan fading in from the bottom-left corner.

EVE-NG Download Guide 2026: Community, Freemium and Pro ISO Explained

EVE-NG v7.2.0-4 is one 10 GB ISO. The free Freemium mode caps labs at 7 nodes, Pro costs 150 EUR, and Community is EOL. Here's which file to download now.
Blurred green code scrolling down a dark screen with a sky blue gradient in the lower left corner

OSCP Study Guide 2027: Exam Format, Cost, and a 6-Month Plan

OSCP+ costs $1,749 for the 90-day course and one exam attempt. Here's the exam format, the scoring change on 2026-12-01, what it all costs, and a realistic 6-month study plan.

Share Your Valuable Opinions