← Cryptography guide

K038 / Asymmetric encryption, signatures and key exchange

Diffie-Hellman

Diffie Hellman key exchange · Survey date 2026-12-05

Diffie-Hellman (DH) is an asymmetric key-agreement method for establishing a shared secret between two parties.

CRYPTOGRAPHY LAB / K038

Cryptography Lab · Diffie-Hellman

Run real computations and inspect intermediate states step by step.

Educational only. Never enter real passwords or secret keys. Computation stays in your browser; input and trace values are not sent to a server. Do not use this implementation in production.

Scope: Toy educational example

DH (p=23, g=5)

See the scope above for fixed examples, reduced parameters and supported variants. Standard operation names and formulas are retained.

Open lab →

Definition and overview
Diffie-Hellman (DH) is an asymmetric key-agreement method for establishing a shared secret between two parties. Source document ↗
Classification and reversibility
Key agreement; use the shared secret for symmetric-key derivation rather than directly. Source document ↗
Key or secret requirements
Each party keeps a secret integer below p; p, g and public values A/B are public. Source document ↗
Operating principle and structure
Agree on p,g; calculate/exchange A,B with secrets x,y; B^x and A^y yield the same shared key k. Source document ↗
Key size, block size and parameters
Finite-field DH requires prime p and other parameters; p should be at least 2048 bits. Source document ↗
Security strength and recommendation
NIST plans to deprecate quantum-vulnerable public-key algorithms after 2030 and disallow after 2035; DH needs PQ transition. Source document ↗
Known attacks and vulnerabilities
Raw unauthenticated DH is vulnerable to man-in-the-middle attacks. Source document ↗
Standards and certification
NIST SP 800-56A Rev.3 specifies finite-field/elliptic-curve DH and MQV key establishment. Source document ↗
Typical uses
Establish session keys via ephemeral DH/ECDH in TLS and IKE. Source document ↗
Implementations, libraries and tools
Windows CNG supports DH key pair generation through BCryptOpenAlgorithmProvider and related APIs. Source document ↗
Decryption or decoding
DH itself has no decryption or decoding; derive symmetric keys for subsequent ciphertext decryption/authentication. Source document ↗
Advantages
Computes a shared secret over an untrusted channel without transmitting any of the secret itself. Source document ↗
Limitations and cautions
Static key exchange lacks forward secrecy and is considered poor practice. Source document ↗
Distinguishing encryption, encoding and hashing
DH is key agreement, not encryption/encoding/hashing; derive keys with a KDF rather than using raw shared secret. Source document ↗

Practical examples

Examples are illustrative; they are not execution-tested implementations or operational recommendations.

Example

Alice and Bob exchange public parameters and values, compute the same shared secret using their private values, derive a symmetric key via KDF for later encryption/decryption and authentication.

Procedure

Choose standard finite-field parameters or curve; generate ephemeral key pairs and exchange only public keys; compute shared secret from peer public and own private key; derive session key via KDF; encrypt/decrypt with that symmetric key. DH itself has no decryption.

Scenario

Use ephemeral ECDH/DH in TLS/IKE handshakes for session keys, then symmetric encryption of application data. Authenticate public-key exchange with certificates or authenticated channel to prevent MITM.

Pitfalls and alternatives

Authenticate peers with certificates, signatures or PSK. Static DH/ECDH lacks forward secrecy; use KDF, not raw shared secret as key. Use standard parameters, safe sizes/curves and consider post-quantum migration.

Diagram source ↗

References