1
CS 161, Summer 2026 @ UC Berkeley
Slides credit: Nick Weaver, Nicholas Ngai, Peyrin Kao, Henry Corrigan-Gibbs, Jonah Bedouch
Diffie-Hellman Key Exchange and Public-Key Cryptography
Lecture 15 (Transport 3)
Diffie-Hellman Key Exchange
Lecture 15, CS 161, Summer 2026
Diffie-Hellman Key Exchange
Public-Key Encryption
Secure Secret Sharing
Symmetric-key cryptography relies on Alice and Bob sharing a secret key.
As an analogy, consider Alice and Bob sharing a secret color over an insecure channel.
Analogy: Secure Color Sharing
Alice generates a secret color amber (A).�Bob generates a secret color blue (B).
Alice mixes G+A, and Bob mixes G+B.
Alice and Bob exchange the mixed G+A and G+B.
Alice mixes G+B with A to get G+A+B.�Bob mixes G+A with B to get G+B+A.
Alice, Bob agree on a public color grey (G).
B
+
+
=
=
+
=
+
=
A
G+A
B
A
G
G
G+B
G+B
G+A
G+B+A
G+B+A
Bob
Eve
Alice
Analogy: Secure Color Sharing
If Eve can see the insecure channel, there are two things she might try.
B
+
+
=
=
+
=
+
=
A
G+A
B
A
G
G
G+B
G+B
G+A
G+B+A
G+B+A
Bob
Eve
Alice
Discrete Log Problem: Math Version of Paint Mixing
The discrete logarithm problem:
The discrete log problem is a mathematical version of "paint mixing."
a
ga mod p
Easy
Hard
a can't be 0 or p–1 because g0 = gp–1 = 1 mod p, making it easy to recover a.
g must be carefully selected to be a "generator," and p must be prime.�Won't discuss further.
Why Discrete Log is Hard
Given y = gx mod p, no "log" operation exists for recovering x.
All known algorithms to recover x are exponential-time.
Plot of y = gx mod p
Plot of y = gx
In mod space, there's no pattern: Given y, it's hard to find x.
With real numbers, there's a pattern: �Larger y corresponds to larger x.
Diffie-Hellman Key Exchange
Alice
Eve
Bob
Randomly generate a
Calculate ga mod p
Randomly generate b
Calculate gb mod p
Public values: g, p
Calculate shared symmetric key:
(gb)a mod p
Calculate shared symmetric key:
(ga)b mod p
ga mod p
gb mod p
Given ga mod p and gb mod p, Eve cannot compute gab mod p.
She could try ga × gb mod p, but that is ga+b mod p, not (ga)b mod p.
Properties of Diffie-Hellman
For correctness, both users should end up with the same key.
(ga)b mod p = (gb)a mod p
For security, we define the Computational Diffie-Hellman (CDH) assumption:�Given g, p, ga mod p, and gb mod p, it is hard to compute gab mod p.
Bob receives ga and�raises it to the bth power.
Alice receives gb and�raises it to the ath power.
Using Diffie-Hellman
There are two ways of using Diffie-Hellman Key Exchange:
1. Generate a key the first time you talk with someone, and use it every time.
2. Generate a new key every time you talk with someone.
Problem: Mallory
Alice
Mallory
Bob
Randomly generate a
Calculate ga mod p
Public values: g, p
(gm)a mod p
(gm)b mod p
ga mod p
gb mod p
Randomly generate m
Calculate gm mod p
gm mod p
gm mod p
(ga)m mod p
(gb)m mod p
Randomly generate b
Calculate gb mod p
Mallory and Alice have derived the shared secret gma mod p.
Mallory and Bob have derived the shared secret gmb mod p.
Diffie-Hellman: Issues
Diffie-Hellman is not secure against a MITM adversary.
Diffie-Hellman does not provide authentication.
DHE is an active protocol: Alice and Bob need to be online at the same time.
Elliptic-Curve Diffie-Hellman (ECDH)
The math of Diffie-Hellman can be generalized to:
Elliptic-curve Diffie-Hellman (ECDHE) is a variant that uses different math:
The underlying problem in ECDHE is harder to solve, so we can use smaller keys for equal security. (DHE with 3027-bit keys is about as secure as ECDHE with 256-bit keys.)
Public-Key Cryptography
Lecture 15, CS 161, Summer 2026
Diffie-Hellman Key Exchange
Public-Key Encryption
Public-Key Cryptography: A Brief History
New Directions in Cryptography by Whitfield Diffie and Martin Hellman (1976) is the foundational paper in modern cryptography.
Their big idea: Who said you have to encrypt and decrypt with the same key?
Public-Key Cryptography: Definition
A public-key encryption scheme consists of three efficient algorithms:
Message�(Plaintext)
PKB
Encryption
Algorithm
Ciphertext
SKB
Decryption
Algorithm
Alice
Bob
Insecure Channel
Message�(Plaintext)
ElGamal Encryption
Lecture 15, CS 161, Summer 2026
Diffie-Hellman Key Exchange
Public-Key Encryption
ElGamal Encryption: Intuition
Diffie-Hellman is great: It lets Alice and Bob share a secret over an insecure channel.
Using a Diffie-Hellman secret gab mod p as the key, how to encrypt and decrypt?
Dec(gab, Enc(gab, M)) = M × gab × (gab)–1 = M mod p
In modular arithmetic, "division" is multiplying by the multiplicative inverse.
gab × (gab)–1 = 1 mod p.
ElGamal Encryption
Decrypt: Bob derives the inverse key and uses it to decrypt the message.
Encrypt: Alice completes her half of DHE, and encrypts the message with the key.
Alice
Bob
Pick random r, and compute gr mod p
Pick random b, and publish gb mod p
Public values: g, p
Compute inverse key: ((gr)b)–1 mod p
Compute key: (gb)r mod p
M × (gb)r mod p
gb mod p
Encrypt message with key: M × (gb)r mod p
Decrypt ciphertext with inverse key:
M × (gb)r × ((gr)b)–1 = M mod p
KeyGen: Bob completes his half of DHE.
gr mod p
ElGamal Definition
Like DHE, ElGamal security uses the Computational Diffie-Hellman (CDH) assumption:�Given g, p, gr mod p, and gb mod p, it is hard to compute grb mod p.
ElGamal Encryption | |
KeyGen() | SK ← randomly-selected b�PK ← gb mod p |
Enc(PK, M) | C1 ← gr mod p C2 ← M × PKr |
Dec(SK, C) | M ← C2 × (C1SK)–1 |
Public parameters.
Part of ciphertext.
Bob's public key.
Secret used to encrypt message.
Sometimes written as B.
Sometimes written as R.
ElGamal is Malleable
Problem: Mallory can tamper with the ciphertext in predictable ways,�even if she doesn't know the plaintext.�
Schemes vulnerable to this type of attack are called malleable.
RSA Encryption
Lecture 15, CS 161, Summer 2026
Diffie-Hellman Key Exchange
Public-Key Encryption
Recall: Trapdoor One-Way Permutation
A trapdoor function consists of three efficient algorithms:
x
f(x)
Easy
Hard
x
f(x)
Easy
Hard
Easy with trapdoor
Public-Key Encryption from Trapdoor Functions
Building blocks:
From these, we can build an encryption scheme:
x
f(x)
Easy
Hard
Easy with trapdoor
Public-Key Encryption with Trapdoors | |
KeyGen() | (PK, SK) ← T.KeyGen() |
Encrypt(PK, M) | C ← T.Eval(PK, M) |
Decrypt(SK, C) | M ← T.Invert(SK, C) |
Use the invert direction to decrypt.
Use the eval direction to encrypt.
Recall: RSA Trapdoor Function
Forward direction: Raise to e (public key value) power.
Invert direction: Raise to d (secret key value) power.
RSA Trapdoor Function | |
KeyGen() |
|
Eval(VK, x) | Output xe mod N. |
Invert(SK, y) | Output yd mod N. |
p and q are usually 1024–2048 bits long.
Other e values work as long as they're relatively prime to (p – 1)(q – 1).
–1 is modular multiplicative inverse:�ed = 1 mod (p – 1)(q – 1).
Public-Key Encryption with the RSA Trapdoor Function
RSA Trapdoor Function | |
KeyGen() |
|
Eval(VK, x) | Output xe mod N. |
Invert(SK, y) | Output yd mod N. |
RSA Encryption Scheme | |
KeyGen() | PK ← (N, e) and SK ← d. |
Encrypt(PK, M) | C ← Me mod N. |
Decrypt(SK, C) | M ← Cd mod N. |
Public-key encryption schemes can be created using a trapdoor function.
RSA is a trapdoor function.
Public-Key Encryption with Trapdoors | |
KeyGen() | (PK, SK) ← T.KeyGen() |
Encrypt(PK, M) | C ← T.Eval(PK, M) |
Decrypt(SK, C) | M ← T.Invert(SK, C) |
RSA Encryption Requires "Padding"
As presented, "plain" RSA encryption is not IND-CPA secure.
In order for plain RSA to be IND-CPA secure, a padding scheme is necessary.
Hybrid Encryption
Lecture 15, CS 161, Summer 2026
Diffie-Hellman Key Exchange
Public-Key Encryption
Issues with Public-Key Encryption
Public-key encryption is often expensive and slow.
Message length is often limited.
Best of Both Worlds: Hybrid Encryption
Hybrid encryption has the benefits of both symmetric-key and public-key encryption:
Message
K
Symmetric-Key
Encryption
Ciphertext
Alice
Bob
Insecure Channel
Public-Key
Encryption
PKB
K
Symmetric-Key
Decryption
Public-Key
Decryption
SKB
Message
Encrypted Key