0Pricing
Cryptology Academy · Lesson

Diffie-Hellman Key Exchange Math

Step through the DH protocol and the discrete-log hardness assumption.

Diffie-Hellman Key Exchange Math is a free Cryptology Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Cryptology Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

In this lesson we trace the Diffie-Hellman key exchange step by step, from the public parameters through Alice and Bob's private exponents to the shared secret.

Public Parameters

Alice and Bob agree publicly on: - p: a large prime (2048+ bits in production) - g: a generator of Z*p (usually g=2 or g=5) Eve can see p and g. Knowing them does NOT break the protocol.

Alice's Private Key

Alice generates a random secret: a (1 < a < p-1) Alice computes her public value: A = g^a mod p Alice sends A to Bob. Eve sees A. Eve cannot find a from A — discrete logarithm problem.

Bob's Private Key

Bob generates a random secret: b (1 < b < p-1) Bob computes his public value: B = g^b mod p Bob sends B to Alice. Eve sees B. Eve cannot find b from B.

Shared Secret Computation

Alice: S = B^a mod p = (g^b)^a mod p = g^(ab) mod p Bob: S = A^b mod p = (g^a)^b mod p = g^(ab) mod p Both compute the same S = g^(ab) mod p without ever transmitting it.

Small Number Example

p=23, g=5 Alice: a=6, A = 5^6 mod 23 = 8 Bob: b=15, B = 5^15 mod 23 = 19 Alice: S = 19^6 mod 23 = 2 Bob: S = 8^15 mod 23 = 2 ✓ Eve sees: 23, 5, 8, 19 but cannot find 2 easily.

Discrete Logarithm Problem

Given A = g^a mod p, find a. For large p (2048 bits), the best known algorithm (Number Field Sieve) takes sub-exponential but still astronomical time: ~2^100 operations for 2048-bit DH.

Man-in-the-Middle Attack

DH is vulnerable to MITM: Mallory intercepts A and B, substitutes her own values. She shares one secret with Alice, another with Bob. Solution: authenticate the exchange (certificates or pre-shared auth).

Safe Primes

p must be chosen carefully. A safe prime p=2q+1 (where q is prime) ensures the DH group has no small subgroups that could be exploited in small-subgroup attacks. NIST specifies approved DH groups.

DH Parameter Sizes

NIST recommendations: - 2048-bit: minimum for new deployments - 3072-bit: equivalent to RSA-3072 - 4096-bit: high security Never use pre-generated Oakley Group 1/2 (768/1024-bit) — broken by state actors (Logjam attack).

Logjam Attack (2015)

Logjam showed many TLS servers used the same 1024-bit DH prime. State actors could precompute the discrete log for these specific primes, allowing real-time decryption. Solution: use 2048-bit+ unique primes.

Quick Check

In DH with p=23, g=5, a=4, b=3: what is the shared secret?

Recap

DH key exchange math is clear. Next we apply the same concept on elliptic curves to get ECDH — same security, much smaller keys.

Frequently asked questions

Is the “Diffie-Hellman Key Exchange Math” lesson free?

Yes — the full text of “Diffie-Hellman Key Exchange Math” is free to read here on the web, and the Cryptology Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Cryptology Academy course, upgrade to CoddyKit PRO.

What will I learn in “Diffie-Hellman Key Exchange Math”?

Step through the DH protocol and the discrete-log hardness assumption. You practise Cryptology Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Cryptology Academy?

No prior experience is required. Cryptology Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Diffie-Hellman Key Exchange Math” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Cryptology Academy lesson?

Yes. Every Cryptology Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The Key Distribution Problem
  2. Diffie-Hellman Key Exchange Math
  3. ECDH: Elliptic Curves for Key Exchange
  4. Forward Secrecy & Ephemeral Key Exchange
← Back to Cryptology Academy