Skip to content
FoundationSource checked

Diffie-Hellman Key Exchange

Lets two parties derive a shared secret over an insecure channel.

Quick Facts

Compact metadata for implementation and discovery.

Category
Cryptography
Difficulty
Advanced
Family
Key Agreement
Tier
Foundation
Status
Verified
Review
Source Checked
Sources
2
Formulas
1
Relationships
8
Code example
Not ready
Visualization
Ready
Aliases
DH Key Exchange

Overview

Diffie-Hellman key exchange has each party combine a private exponent with public parameters, exchange public values, and compute the same shared secret from the other party's public value.

Core Concepts

shared secretprivate exponentpublic parametersauthentication

Quick facts

Complexity

Runtime and memory behavior at a glance.

Best

O(k^2)

Average

O(k^3)

Worst

O(k^3)

Space

O(k)

When to Use

  • Use it through a vetted protocol or library when two parties need a shared secret.
  • Use authenticated variants to prevent man-in-the-middle attacks.
  • Use standardized groups or elliptic-curve variants rather than custom parameters.

When Not to Use

  • Avoid unauthenticated Diffie-Hellman when peer identity matters.
  • Avoid custom groups or parameters without expert review.
  • Avoid reusing ephemeral private exponents across sessions.

What It Does

  • Establishes a shared secret
  • Keeps private choices off the wire

Use Cases

  • Secure session setup
  • Key agreement
  • Teaching public-key ideas

Inputs

  • Public parameters
  • Each party's private value

Outputs

  • Shared secret

Key Formulas (1)

Shared secret

shared = publicotherprivate mod p
shared = public_other^private mod p

Both parties compute the same shared value from public exchange values and private exponents.

Pseudocode

  1. 1.Agree on public parameters
  2. 2.Each party chooses a private value
  3. 3.Each party computes and sends a public value
  4. 4.Each party combines the received public value with its private value
  5. 5.Both parties get the same shared secret

Canonical Example

Input
Alice's private value, Bob's public value, and shared group parameters
Output
shared secret matching Bob's computed secret
Explanation
Each party combines its private value with the other party's public value to reach the same secret.

Implementation Notes

  • Use established protocol implementations such as TLS rather than raw primitives.
  • Derive final keys with a key-derivation function after computing the shared secret.
  • Prefer ephemeral keys for forward secrecy.

Common Pitfalls

  • Treating key exchange as authentication.
  • Failing to validate peer public values.
  • Using weak or nonstandard groups.

Interactive Mechanism Walkthrough

Graph Frontier & Edge Relaxation

Show two parties exchanging public values and deriving the same secret.

Diffie-Hellman Key Exchange mechanism walkthrough step 1 of 5suvwxt
Step 1 of 5: Agree on public parameters
shared-secret

Notes

Diffie-Hellman needs authentication around it; the key exchange alone does not prove who sent the public value.

Sources (2)

What each reference supports for this entry.

Diffie-Hellman 1976

Paper

Original public-key distribution paper introducing Diffie-Hellman key exchange.

Open source

NIST SP 800-56A Rev. 3

Documentation

NIST recommendation for finite-field and elliptic-curve discrete-logarithm key establishment.

Open source

Explore Next

Good next jumps from the relationship graph, ranked for useful discovery.

ECDH applies Diffie-Hellman key exchange over elliptic curves.

Often paired withHKDF

Shared secrets from key exchange are commonly fed into a key derivation function.

Related Algorithms

Tags

CryptographyKey ExchangePublic KeySecurity