Crypto Lecture 8 · Public-Key Crypto: RSA, Signatures, Diffie-Hellman

The last part of the cryptography lecture, rebuilt step by step: the key-distribution problem and the three uses of public-key crypto, Euler's totient and Euler's theorem with proofs, RSA key generation, encryption, decryption and signatures computed by hand, why textbook RSA is unsafe and what OAEP and PSS fix, and Diffie-Hellman with forward secrecy, ending with a summary of the whole lecture.

Subject: Computer Security · 75 slides · code lesson

Open the interactive version of this deck

What this lesson covers

The lesson, slide by slide

1. Public-Key Crypto: RSA, Signatures, Diffie-Hellman

Title

Computer Security — Crypto Lecture 8

How two strangers get secrecy, signatures and a shared key without meeting first

2. Part 1 — Strangers on an open wire

Section

Everything so far assumed a shared secret

3. What you will be able to do

Objectives

This is the last part of the cryptography lecture. Every tool so far, from the one-time pad to AES and HMAC, needed a key that both sides already shared. This part removes that assumption, and then builds the scheme that made it practical, RSA, from two small facts you can check by hand.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-div-pk

4. Warm-up: what did every scheme so far need?

Warm-up

Think back over the one-time pad, DES, AES in CBC or CTR mode, and HMAC.

Discussion prompt

What did every one of them require before the first message could be protected, and how was that requirement supposed to be met?

Hint: What has to exist before Alice sends anything?

Answer:

A key shared in advance by both sides. The one-time pad needs a pad as long as all the traffic, AES needs a 128- or 256-bit key, HMAC needs a secret key. None of the lectures said how that key gets there.

That is the chicken-and-egg problem of symmetric cryptography: to talk securely you need a shared secret, and to share a secret you need to talk securely. Couriers and pre-arranged codebooks worked for governments; they do not work for a browser meeting a web server for the first time.

5. The key-distribution problem

Concept

Picture a course with 300 students who each want to send the instructor something private. With symmetric crypto, every student needs a key shared with the instructor, set up in advance over some trusted channel. That is 300 secrets to arrange before anyone can send a word.

who must talk privatelyshared keys neededfor 300 people
each person with one instructorone per person300
every pair of peoplen(n − 1) / 244,850
with public-key cryptoone key pair per person300 pairs, none pre-shared

The pairwise number grows with the square of the group, and every one of those keys has to be delivered secretly. The internet has billions of possible pairs that have never met. Public-key cryptography answers the problem directly: nobody shares anything secret in advance.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-div-pk

6. How many keys for a whole class?

Estimation

Suppose all 300 students want to message each other privately, every pair with its own AES key.

Predict first

Roughly how many secret keys must be set up in advance?

  • About 300
  • About 600
  • About 45,000
  • About 90,000

Correct: About 45,000

Why: Each of 300 students pairs with 299 others, and each pair is counted twice that way, so the count is 300 times 299 divided by 2, which is 44,850. With public-key crypto each student makes one key pair and publishes half of it: 300 public keys, and nothing secret is ever exchanged.

7. The idea: split the key in two

Concept

Bob runs a key-generation algorithm that produces a matched pair. He publishes one half, the public key PK, to anyone who asks. He keeps the other half, the secret key SK, and never sends it anywhere.

Figure (svg): Bob's KeyGen makes PK, given to anyone, and SK, kept by Bob; Alice encrypts with PK and sends c; Mallory on the wire has PK and c; only SK decrypts

The picture to keep is an open padlock. Bob hands out as many open padlocks as people want. Anyone can put a message in a box and snap a padlock shut, which is encryption with PK. Only Bob's private key opens it. Mallory can hold both the padlock and the locked box and still cannot open it.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-pk-idea

8. Mallory has the public key and the ciphertext. Why is that not enough?

Socratic

In symmetric crypto, whoever can encrypt can also decrypt: it is the same key. Here Mallory can encrypt to Bob as easily as Alice can.

Discussion prompt

If Mallory can run Encrypt with PK herself, what exactly stops her from running it backwards on c? What property must the encryption function have?

Hint: Compare multiplying two primes with recovering them from the product.

Answer:

Being able to compute a function forwards does not mean being able to invert it. Public-key encryption needs a function that is easy to compute with PK and infeasible to invert without SK, even for someone who holds PK and c.

Such a function is called a trapdoor function: one-way for everyone, except that a secret, the trapdoor, makes the reverse direction easy. The rest of this lesson builds one, RSA, and the trapdoor turns out to be the factorisation of a large number.

9. Use 1: send the instructor a private message

Concept

The instructor generates a key pair once and posts the public key on the course page. Any student encrypts with it and sends the ciphertext over the open network. Only the instructor, holding SK, can read it, and no student ever arranged anything with the instructor beforehand.

with AESwith public key
setup before the first messagea secret key shared with each studentpost one public key
300 students300 secret keys delivered securelynothing delivered secretly
who can read a student's messagethe instructor, and anyone else holding that keyonly the instructor

AES could protect the same message, but only after a secret key was delivered to each student. Public key removes that setup entirely, which is the whole point.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-pk-app-conf

10. Use 2: a signed announcement

Concept

Now run it the other way. The instructor signs an announcement with the private key, and every student checks the signature with the public key. A valid signature shows the announcement came from the holder of SK and was not changed.

Could a MAC do this? A MAC needs a key shared between the instructor and the students. But then any student who can check a tag can also make one, so a student could post a fake announcement with a valid tag. And no student could ever prove to anyone else who wrote it.

propertyMAC (shared key)signature (key pair)
who can create a valid tageveryone who can verifyonly the private-key holder
who can verifyonly key holdersanyone with the public key
proof to a third partynoyes: non-repudiation

Signatures give public, non-repudiable proof of origin. Software updates, certificates and signed commits all rely on exactly this.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-pk-app-sign

11. Use 3: agree on a key, then switch to AES

Concept

The most common use by far. The student picks a fresh random AES key k, encrypts it with the instructor's public key, and sends it. Eve sees the ciphertext but cannot open it, so only the instructor learns k. Now both sides share k without ever having met, and they switch to AES for the real traffic.

Figure (svg): The student encrypts a random key k with PK and sends it; the instructor decrypts to get k; then both use AES with k in both directions

In practice the AES traffic is also authenticated, with Encrypt-then-MAC or a mode such as AES-GCM. This simple version, called key transport, is how TLS 1.2 with RSA key exchange worked. Modern TLS 1.3 uses Diffie-Hellman instead, which ends this lesson.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-pk-app-kex

12. Which use of public-key crypto is this?

Sorting

Sort each scenario to the use it relies on.

Sort into buckets

Private message, signature, or key agreement?

private message
A whistle-blower sends a document that only one journalist can read
signature
Your laptop checks that an operating-system update really came from the vendor; A Git host shows a commit as verified because of the author's key
key agreement
A browser and a server that have never met set up an AES key for the session; An SSH client and server derive fresh keys at the start of each login
conf
Only the holder of the private key can read it; anyone could have sent it.
sig
Only the private-key holder could have produced it, and anyone can check it: proof of origin, not secrecy.
kex
Public key is used once to set up a shared symmetric key, and AES does the rest.

13. Which job can symmetric crypto never do?

Elimination

Symmetric tools are fast and well understood. Rule out the jobs they can do, given a pre-shared key.

Eliminate the wrong options

Which requirement is impossible with symmetric crypto alone, even if keys could be shared in advance?

  • e1. Keep a message secret between two people
  • e2. Detect any change to a message between two people
  • e3. Let a stranger verify who wrote a message, with no way for verifiers to forge it
  • e4. Encrypt gigabytes of video quickly

Survives elimination: e3

Why: With a shared key, everyone who can verify can also create. Public verifiability without the power to forge needs two different keys, one to sign and one to verify, and that is only possible with public-key crypto.

14. Why not use public key for everything?

Hypothesis

Public-key encryption solves key distribution. Yet TLS, SSH and PGP use it only for a moment at the start.

Predict first

What is the main reason real systems switch to AES as soon as a key is agreed?

  • Public-key encryption is hundreds to thousands of times slower than AES
  • Public-key encryption is less secure than AES
  • Public keys expire after one message
  • AES keys are easier to publish

Correct: Public-key encryption is hundreds to thousands of times slower than AES

Why: An RSA operation works on numbers thousands of bits long with modular exponentiation, while AES runs a few table lookups and XORs per block, often in dedicated CPU instructions. So every real system is hybrid: one public-key step to agree on a key, then fast symmetric crypto for the data.

15. A public-key scheme, precisely

Concept

Formally, a public-key encryption scheme is three algorithms that fit together.

KeyGen — makes a matched pair (PK, SK); PK is published, SK is kept

Encrypt(PK, M) — anyone can run it; it outputs a ciphertext C

Decrypt(SK, C) — only the key owner can run it; it outputs M

requirementwhat it says
correctnessDecrypt(SK, Encrypt(PK, M)) = M for every message
trapdoorEncrypt is easy with PK; inverting it without SK is infeasible
security (CPA)even given PK and C, an attacker learns nothing about which message was sent

The security bar is the chosen-plaintext game from earlier in the lecture, with one change: the attacker gets the public key for free. That change matters a lot. An attacker who holds PK can encrypt any message she likes, at any time, without asking anyone.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-pk-def

16. Match each piece of the definition

Matching

Five pieces of the formal definition, five meanings.

Match the pairs

  • m1. KeyGen
  • m2. PK
  • m3. SK
  • m4. correctness
  • m5. trapdoor
  • r1. produces the matched pair
  • r2. published; used to encrypt or verify
  • r3. never leaves the owner; used to decrypt or sign
  • r4. decrypting an encryption gives the message back
  • r5. easy forwards, infeasible backwards without the secret

Why: The scheme is KeyGen plus two algorithms that undo each other. Correctness says they undo each other; the trapdoor property says only the secret lets you undo Encrypt; CPA security says the ciphertext leaks nothing even to someone holding the public key.

17. Can deterministic public-key encryption be CPA-secure?

Counterexample

Earlier in the lecture, ECB and other deterministic schemes failed CPA security. In public-key crypto the attacker holds PK.

Discussion prompt

Suppose Encrypt(PK, M) always gives the same C for the same M. An attacker knows the message is either yes or no, and sees C. Explain, without any key, why she learns which one it was.

Hint: What can anyone do with a public key?

Answer:

She encrypts yes herself with the public key and compares with C. If they match, the message was yes; otherwise it was no. No secret is needed, because encryption is public.

So any secure public-key encryption must be randomised: the same message must encrypt differently every time. Plain textbook RSA, as we are about to build it, is deterministic, which is one reason real RSA always adds random padding (OAEP).

18. Part 2 — Two facts RSA needs

Section

Euler's totient and Euler's theorem

19. RSA rests on two facts

Concept

RSA, published by Rivest, Shamir and Adleman in 1977, was the first public-key scheme practical enough to run. It needs no tower of number theory. It rests on exactly two facts, and this part proves both, each with an example first and a short proof after.

factstatementwhat RSA uses it for
Fact 1: the totient of pqφ(pq) = (p − 1)(q − 1)the key owner can compute φ, an outsider cannot
Fact 2: Euler's theoremif gcd(a, n) = 1 then a to the power φ(n) is 1 mod ndecryption returns exactly the message

Keep the destination in mind while the maths happens: Fact 1 builds the trapdoor, and Fact 2 is why the trapdoor opens the lock.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-rsa-title

20. Fact 1: what φ(n) counts

Concept

Euler's totient φ(n) is the number of values i from 1 to n that share no factor with n, meaning gcd(n, i) = 1. Before any formula, count a few.

Figure (svg): Rows of numbers 1 to n for n = 7, 8, 9, 10, 12 with the numbers coprime to n highlighted green; the counts are 6, 4, 6, 4, 4

nnumbers coprime to nφ(n)
71 2 3 4 5 66
81 3 5 74
91 2 4 5 7 86
101 3 7 94
121 5 7 114

For a prime p, nothing from 1 to p - 1 shares a factor with it, so φ(p) = p - 1. That one line is worth memorising.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-nt-totient-def

21. Your turn: count three totients

Fill the middle

List the numbers from 1 to n that share no factor with n, then count them.

Fill in the blanks

n = 11 (prime) coprime: 1..10 phi = 10
n = 14 = 2 * 7 coprime: 1 3 5 9 11 13 phi = 6
n = 22 = 2 * 11 coprime: odd, not 11 phi = 10

Why: 11 is prime, so all ten numbers below it count. For 14, drop the even numbers and 7, leaving 1, 3, 5, 9, 11, 13: six. For 22, drop the even numbers and 11, leaving 1, 3, 5, 7, 9, 13, 15, 17, 19, 21: ten. Notice 14 = 2 times 7 gives 1 times 6, and 22 = 2 times 11 gives 1 times 10. A pattern is forming.

22. Is there a shortcut for n = p · q?

Pattern

A real RSA modulus has about 600 digits, so counting from 1 to n is hopeless. But RSA builds n by multiplying two primes it chose itself. Here are some products of two primes, with φ counted by hand.

Step through it

Look at each row as it appears. What formula in p and q gives φ(n)?

  1. n = 6 = 2 · 3, and counting gives φ(6) = 2
  2. n = 10 = 2 · 5, and counting gives φ(10) = 4
  3. n = 15 = 3 · 5, and counting gives φ(15) = 8
  4. n = 21 = 3 · 7, and counting gives φ(21) = 12
  5. n = 35 = 5 · 7, and counting gives φ(35) = 24. Guess the formula before the next step.
  6. The pattern: 2 = 1 · 2, 4 = 1 · 4, 8 = 2 · 4, 12 = 2 · 6, 24 = 4 · 6. So φ(pq) = (p − 1)(q − 1).

Whoever knows p and q gets φ(n) for free with one multiplication. Whoever knows only n has to factor it first. That asymmetry is the trapdoor RSA is built on.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-nt-totient-q

23. Fact 1 on one example: n = 15

Picture it

Lay the numbers 1 to 15 out in a 3 by 5 grid and light up the ones that share no factor with 15.

Figure (svg): Numbers 1 to 15 in a grid; 1, 2, 4, 7, 8, 11, 13, 14 are green; multiples of 3 and of 5 are marked; 15 is both

Eight survive: 1, 2, 4, 7, 8, 11, 13, 14. The formula predicts (3 - 1)(5 - 1) = 8. The grid also shows how to prove it: the failures are exactly the multiples of 3 and the multiples of 5, and 15 is in both lists.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-nt-totient

24. Why φ(pq) = (p − 1)(q − 1)

Worked example

Count what is not coprime to pq and subtract it. A number from 1 to pq fails only if it is a multiple of p or a multiple of q, so this is one inclusion-exclusion count.

all numbers 1..pq            pq
minus multiples of p         - q    (p, 2p, ..., qp)
minus multiples of q         - p    (q, 2q, ..., pq)
plus pq, subtracted twice    + 1
phi(pq) = pq - q - p + 1 = (p - 1)(q - 1)
stepgeneraln = 15 = 3 · 5
start with all numberspq15
remove multiples of pq of them5: 3, 6, 9, 12, 15
remove multiples of qp of them3: 5, 10, 15
add back pq, removed twice+ 1+ 1
resultpq − q − p + 115 − 5 − 3 + 1 = 8

Figure (svg): The n = 15 grid again: multiples of 3 and of 5 removed, 15 counted in both

Factor the result

Why: pq − q − p + 1 = (p − 1)(q − 1), which you can check by expanding the right-hand side.

Verify: the grid agrees

Why: The grid on the previous slide had exactly 8 green cells, and (3 − 1)(5 − 1) = 8.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-nt-totient-proof

25. The totient recipe

Pattern

Three cases cover every totient RSA will ever need.

n isφ(n)example
a prime pp − 1φ(7) = 6
a product of two different primes p · q(p − 1)(q − 1)φ(15) = 2 · 4 = 8
anything elsecount, or factor it firstφ(12) = 4 by counting

The second row is the one RSA uses, and it only works for someone who knows the two primes. Everyone else is stuck with the third row, which for a 600-digit n is hopeless.

26. Checkpoint: the totient of a real-looking modulus

Check

The classic RSA example uses p = 61 and q = 53.

Check your understanding

What is φ(3233), where 3233 = 61 · 53?

  • A. 3232
  • B. 3120 (correct)
  • C. 3119
  • D. 114

Answer: B

Why: φ(pq) = (p − 1)(q − 1) = 60 · 52 = 3120. No grid of 3233 numbers is needed, but only because we know the two primes.

Why A tempts people
3232 = n − 1 is φ of a prime. 3233 is not prime.
Why C tempts people
This is pq − p − q − 1; the overlap must be added back, not subtracted.
Why D tempts people
60 + 52 + 2 is not a formula for anything here; the factors multiply.

27. Fact 2: Euler's theorem

Concept

The statement: if a shares no factor with n, then a raised to the power φ(n) leaves remainder 1 when divided by n. Watch it happen by multiplying by a again and again and reducing each time.

nφ(n)apowers of a mod nback to 1 at
10433, 9, 7, 1power 4
14633, 9, 13, 11, 5, 1power 6
221077, 5, 13, 3, 21, 15, 17, 9, 19, 1power 10

Each sequence returns to 1 at exactly the power φ(n). For some other choices of a the cycle is shorter, but its length always divides φ(n), so the power φ(n) still lands on 1. This is the engine that will bring an RSA ciphertext back to its message.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-nt-euler

28. What if a shares a factor with n?

Prediction

Euler's theorem has a condition: gcd(a, n) = 1. Test what happens without it, with a = 2 and n = 10.

Predict first

The powers of 2 mod 10 start 2, 4, 8, 6. Does the sequence ever reach 1?

  • Yes, at power 4, since φ(10) = 4
  • Yes, but later, at power 8
  • Never: it cycles 2, 4, 8, 6 forever
  • It reaches 0 and stays there

Correct: Never: it cycles 2, 4, 8, 6 forever

Why: Every power of 2 is even, and 1 more than a multiple of 10 is odd, so no power of 2 is 1 mod 10. The next power after 6 is 12, which is 2 again, so the cycle repeats. The condition gcd(a, n) = 1 is not decoration; the proof uses it.

29. Proof, step 1: multiplying by a only shuffles

Picture it

Take n = 10 and a = 3. The numbers that share no factor with 10 are 1, 3, 7, 9: that is φ(10) = 4 numbers. Multiply each by 3 and reduce mod 10.

Figure (svg): Arrows from 1, 3, 7, 9 to 3, 9, 1, 7: 3 times 1 is 3, 3 times 3 is 9, 3 times 7 is 21 which is 1, 3 times 9 is 27 which is 7

The results are 3, 9, 1, 7: the same four numbers in a different order. Multiplying by 3 created nothing new and lost nothing. That is the whole of step 1. The question is why this always happens.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-nt-euler-proof

30. Why can two arrows never land on the same number?

Socratic

If two different inputs x and y both mapped to the same target, the four results could not be all four numbers again.

Discussion prompt

Suppose 3 · x and 3 · y leave the same remainder mod 10, with x and y different numbers from the set. What would 10 have to divide? Why is that impossible?

Hint: Move everything to one side: 3x − 3y.

Answer:

10 would divide 3x - 3y = 3(x - y). Since 3 shares no factor with 10, all of 10 must divide x - y. But x and y are different numbers between 1 and 10, so x - y is somewhere from -9 to 9 and not zero. No multiple of 10 lives there except 0.

So two arrows can never collide. The next slide writes that argument for any n and any a.

31. No collisions: the proof by contradiction

Concept

Suppose x is not equal to y, both coprime to n, and a times x = a times y (mod n). Follow the consequences one line at a time.

linestatementreason
1n divides a · x − a · y = a(x − y)same remainder mod n
2n divides x − ya shares no factor with n, so every factor of n is in x − y
3x − y is a multiple of nthat is what dividing means
4x − y is not 0we assumed x ≠ y
5−n is less than x − y, which is less than nboth lie between 1 and n
6contradictionthe only multiple of n strictly between −n and n is 0

Figure (svg): A number line with multiples of n marked; a box from just above −n to just below n contains only 0, which is ruled out

One more line finishes step 1: a times x shares no factor with n either, so no arrow leaves the set. Multiplying by a is a shuffle of the φ(n) numbers.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-nt-euler-nocollide

32. Put the contradiction in order

Ranking

Rebuild the argument from memory.

Put in order

  1. Assume x ≠ y and a · x ≡ a · y (mod n)
  2. So n divides a(x − y)
  3. a shares no factor with n, so n divides x − y
  4. x − y lies strictly between −n and n and is not 0
  5. No multiple of n fits there, so the assumption was false

Why: Assume a collision, turn it into a divisibility statement, use gcd(a, n) = 1 to strip off a, then trap x − y in a window that contains no non-zero multiple of n.

33. Proof, step 2: multiply both rows, then cancel

Worked example

Because multiplying by 3 only shuffles 1, 3, 7, 9, the product of the four results equals the product of the four originals, mod 10.

(3*1) * (3*3) * (3*7) * (3*9)  =  3 * 9 * 1 * 7      (mod 10)
3^4 * (1*3*7*9)                =  1*3*7*9            (mod 10)
1*3*7*9 = 189 = 9 (mod 10), and 9 * 9 = 81 = 1, so multiply both sides by 9
3^4                            =  1                  (mod 10)
stepwhat happenswhy
multiply both rowssame four numbers, so same productstep 1: it is a shuffle
collect the 3s3 to the power 4 times 1 · 3 · 7 · 9one 3 for each of the φ(10) = 4 numbers
cancel the productmultiply both sides by 9, the inverse of 9 mod 10the product is coprime to 10, so it has an inverse
result3 to the power 4 ≡ 1 (mod 10)Euler's theorem for this case

Figure (svg): The shuffle of 1, 3, 7, 9 under multiplication by 3 mod 10, which is why both rows have the same product

Verify: by direct computation

Why: 3, 9, 27, 81: and 81 = 8 · 10 + 1, so 3 to the power 4 is 1 mod 10.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-nt-euler-proof2

34. Where did the exponent come from?

Explain it to yourself

Nothing in step 2 was special to 10 and 3.

Discussion prompt

In the general proof, why is the exponent on a exactly φ(n), and where was the condition gcd(a, n) = 1 used?

Hint: Count the copies of a on the left-hand side.

Answer:

The exponent counts how many numbers were multiplied by a: one copy of a for each of the φ(n) numbers coprime to n. So the left side is a to the power φ(n) times their product.

The condition was used twice: to show no two arrows collide (stripping a off a(x - y)), and to show a times x stays coprime to n. Without it, the shuffle breaks, as the powers of 2 mod 10 showed.

35. Part 3 — RSA

Section

Rivest, Shamir, Adleman, 1977

36. RSA: build the key pair

Concept

RSA is a key factory, shown here on the classic tiny example so that every number can be checked by hand. In reality each prime is about 1000 bits long; the steps are identical.

Figure (svg): p = 61 and q = 53 multiply to n = 3233; phi = 60 times 52 = 3120; e = 17 is chosen; d = 2753 solves e times d = 1 mod phi; public key (17, 3233), private key 2753

stepon the examplepublic or secret
pick two random primes p, q61 and 53secret
multiply: n = p · q3233public
Fact 1: φ = (p − 1)(q − 1)60 · 52 = 3120secret
pick e sharing no factor with φ17public
solve e · d ≡ 1 (mod φ)d = 2753, since 17 · 2753 = 15 · 3120 + 1secret

The public key is (e, n) = (17, 3233); the private key is d = 2753. Then p, q and φ are thrown away. Real systems almost always use e = 65537.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-rsa

37. Why must e share no factor with φ?

Prediction

Key generation insists that gcd(e, φ) = 1. Try breaking that rule with e = 2 and φ = 3120.

Predict first

What goes wrong when you try to find d with 2 · d ≡ 1 (mod 3120)?

  • Nothing: d is just larger
  • No such d exists, because 2 · d is always even and 1 more than a multiple of 3120 is odd
  • d exists but decryption becomes slow
  • d equals 1560

Correct: No such d exists, because 2 · d is always even and 1 more than a multiple of 3120 is odd

Why: e · d ≡ 1 (mod φ) has a solution exactly when e and φ share no factor. With e = 2, every 2d is even, but every number of the form 3120k + 1 is odd. So without the gcd condition there is no private exponent at all, and the key is useless.

38. Finding d: the extended Euclidean algorithm

Worked example

Solving e times d = 1 (mod φ) by trying every d would be hopeless at real sizes. The extended Euclidean algorithm finds d in a handful of division steps: run Euclid's gcd algorithm, then walk the remainders backwards to write 1 as a combination of 3120 and 17.

find d with 17 * d = 1 (mod 3120)

3120 = 183 * 17 + 9
  17 =   1 *  9 + 8
   9 =   1 *  8 + 1        # remainder 1: an inverse exists
1 = 9 - 8 = 9 - (17 - 9) = 2*9 - 17
  = 2*(3120 - 183*17) - 17 = 2*3120 - 367*17
d = -367 mod 3120 = 2753
abquotientremainder
3120171839
17918
9811
8180

Figure (svg): The key factory again, now with d = 2753 found by the algorithm

Back-substitute

Why: Each remainder is written in terms of the two numbers above it, until 1 = 2 · 3120 − 367 · 17. Reading that mod 3120 says −367 · 17 ≡ 1, so d ≡ −367 ≡ 2753.

Verify: multiply it out

Why: 17 · 2753 = 46801 = 15 · 3120 + 1, so 17 · 2753 leaves remainder 1 mod 3120.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-rsa

39. The RSA symbols, and who may see them

Concept

A cheat sheet, so the letters stop blurring together. Everything splits into two piles.

symbolwhat it isrelationwho sees it
p, qthe two prime factorschosen at randomsecret
nthe modulusn = p · qpublic
φ(n)Euler's totient of n(p − 1)(q − 1)secret
eencryption exponentshares no factor with φ(n)public
ddecryption exponente · d ≡ 1 (mod φ)secret
(e, n)public keyencrypt and verifypublic
(d, n)private keydecrypt and signsecret

The memory hook: the secret pile is everything you need to build the key, p, q and φ, plus the trapdoor d. Once d exists, p, q and φ are deleted. If anyone ever learns φ, p or q, they can recompute d and the key is dead.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-rsa-symbols

40. Public or secret?

Sorting

Sort every RSA value by who is allowed to know it.

Sort into buckets

Which pile does each value belong to?

public
n = 3233; e = 17; a ciphertext c = 2790 sent over the network
secret
p = 61; φ = 3120; d = 2753
pub
The modulus and the encryption exponent form the public key, and a ciphertext travels in the open by design.
sec
Any one of p, φ or d lets an attacker compute the private key, so all three must stay with the owner (and p, q, φ are best deleted).

41. RSA: encrypt and decrypt

Worked example

Anyone with the public key (17, 3233) can encrypt. The message must be a number smaller than n; take m = 65. Encryption is c = m to the power e mod n, computed by repeated squaring, so even a 2048-bit exponent needs only a few thousand multiplications.

c = 65^17 mod 3233,   17 = 16 + 1 = binary 10001

65^1  = 65
65^2  = 65 * 65     mod 3233 = 992
65^4  = 992 * 992   mod 3233 = 1232
65^8  = 1232 * 1232 mod 3233 = 1547
65^16 = 1547 * 1547 mod 3233 = 789
65^17 = 789 * 65    mod 3233 = 2790
power of 65howvalue mod 3233
1start65
2square992
4square1232
8square1547
16square789
17 = 16 + 1multiply by 652790

Figure (svg): Anyone encrypts 65 with e = 17 to get 2790; only Bob decrypts 2790 with d = 2753 to get 65

Decrypt with the private key

Why: Only Bob knows d = 2753. He computes 2790 to the power 2753 mod 3233 by the same square-and-multiply method and gets 65.

Verify: in Python

Why: pow(65, 17, 3233) returns 2790, and pow(2790, 2753, 3233) returns 65.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-rsa-use

42. Your turn: a whole RSA key on a small example

Faded example

Use p = 11, q = 13 and e = 7. Some lines are done for you.

Fill in the blanks

n = 11 * 13 = 143
phi = 10 * 12 = 120
d = 7^-1 mod phi = 103 # check: 7 * d = 6 * phi + 1
c = 9^7 mod n = 48
m = 48^d mod n = 9

Why: n = 143 and φ = 120. The inverse of 7 mod 120 is 103, because 7 · 103 = 721 = 6 · 120 + 1. Encrypting m = 9 gives 9 to the power 7 mod 143 = 48, and decrypting with d = 103 brings back 9, exactly as Fact 2 promises.

43. Why decryption returns the message

Concept

Decryption is not a separate trick: it is Euler's theorem. Four lines, symbols in the middle column and the example on the right.

linein symbolson the example
1. how d was chosene · d = 1 + k · φ17 · 2753 = 46801 = 1 + 15 · 3120
2. split the exponentm to the (1 + kφ) = m · (m to the φ) to the k65 to the 46801 = 65 · (65 to the 3120) to the 15
3. Fact 2, if gcd(m, n) = 1m to the φ ≡ 1 (mod n)65 to the 3120 ≡ 1 (mod 3233)
4. what is leftm · 1 to the k = m65 · 1 = 65

Line 3 is the only real step, and the condition holds here: 65 = 5 times 13 shares no factor with 61 times 53. Encryption raises to e, decryption raises to d, and together they raise to e times d, which is one more than a multiple of φ, so the message comes back.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-rsa-works

44. Fill in the decryption argument

Fill the middle

Complete the chain for the key with e = 7, d = 103, φ = 120, n = 143 and message m = 9.

Fill in the blanks

e * d = 7 * 103 = 721 = 1 + 6 * 120
c^d = m^(e*d) = 9^721
= 9 * (9^120)^1
9^120 = ___ (mod 143) # Euler: gcd(9, 143) = 1
so c^d = 9

Why: 7 · 103 = 721 = 1 + 6 · 120, so 9 to the 721 is 9 times (9 to the 120) to the 6. Euler's theorem makes 9 to the 120 equal to 1 mod 143, because 9 = 3 · 3 shares no factor with 11 · 13. What remains is 9.

45. What if the message shares a factor with n?

Edge cases

Line 3 needed gcd(m, n) = 1. On the example key, take m = 122 = 2 times 61, which shares the factor 61 with n.

Discussion prompt

Encrypting 122 gives c = 1830. Decryption still returns 122 in this case, but something far worse has happened. Compute gcd(1830, 3233) and explain why the key is now broken.

Hint: What divides both m and n, and therefore also c?

Answer:

gcd(1830, 3233) = 61. Since m is a multiple of 61, so is every power of m, and so is c. Anyone who computes the gcd of the ciphertext and the public modulus learns p = 61, then q = 3233 / 61 = 53, then φ and d.

With real 1024-bit primes, the chance that a random message shares a factor with n is negligible, roughly 1 in 2 to the 1000. Padding schemes such as OAEP make the encrypted number random-looking anyway. But it shows how tightly RSA's security is tied to one secret: the factorisation of n.

46. If the factors leak, everything follows

Faded example

The lecture's CTF 6 makes one point: p and q are the whole secret. Here the modulus n = 2773 is small enough that its factors are known, p = 47 and q = 59, with public exponent 17 and a ciphertext 1647. Rebuild the private key exactly as its owner did.

Fill in the blanks

phi = 46 * 58 = 2668
d = 17^-1 mod phi = 157 # 17 * d = phi + 1
m = 1647^d mod 2773 = 2024

Why: φ = 46 · 58 = 2668. Then d = 157, because 17 · 157 = 2669 = 2668 + 1. Decrypting gives 1647 to the 157 mod 2773 = 2024. This is ordinary key generation and decryption; it was only possible because n was small enough to factor. At real sizes, factoring n is the wall, which is why p, q, φ and d must never leak.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-rsa-ctf

47. Why RSA is believed secure

Concept

Sit in the attacker's chair. She sees the public key e = 17, n = 3233 and a ciphertext c = 2790. Ask what she needs, one step at a time.

Figure (svg): To get m she needs d; to get d she needs phi; to get phi she needs p and q; to get p and q she must factor n

key size ncomparable symmetric keystatus
1024 bitsabout 80 bitsno longer acceptable
2048 bitsabout 112 bitstoday's minimum
3072 bitsabout 128 bitsrecommended for long-term use

Two honest caveats. Factoring is believed hard, not proven hard: no one has shown there is no fast classical algorithm. And a large quantum computer running Shor's algorithm would factor quickly, which is why post-quantum schemes are being standardised now.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-rsa-secure

NIST SP 800-57 Part 1 Rev. 5 — Recommendation for Key Management (comparable security strengths) table 2, comparable security strengths

48. Rebuild the attacker's chain

Ranking

Order the obstacles from the attacker's goal back to the hard problem underneath.

Put in order

  1. To read m, she needs the private exponent d
  2. To compute d, she needs φ(n)
  3. To compute φ(n), she needs p and q
  4. To get p and q, she must factor n

Why: Each secret value is computed from the next one down, and the bottom of the chain is factoring n. That is why RSA's security is usually stated as: breaking it is believed to be as hard as factoring the modulus.

49. Two truths and a myth about RSA's security

Two truths and a lie

Two of these statements are true. Find the myth.

Eliminate the wrong options

Which statement is the myth?

  • t1. A large enough quantum computer running Shor's algorithm would factor RSA moduli quickly
  • t2. Factoring a 2048-bit modulus is estimated to take work comparable to brute-forcing a 112-bit key
  • t3. It has been mathematically proven that factoring large numbers is hard

Survives elimination: t3

Why: No such proof exists. Factoring is believed hard because decades of effort have produced no fast classical algorithm, not because hardness has been proven. It is not even known to be NP-complete. RSA rests on an assumption, as most of public-key cryptography does.

50. Toolkit: big-integer arithmetic in Python

Concept

Python integers have arbitrary precision, so a 2048-bit number is an ordinary int and needs no special library. Three built-ins cover everything in this lesson.

from math import gcd

p, q, e = 61, 53, 17
n   = p * q                  # 3233
phi = (p - 1) * (q - 1)      # 3120
assert gcd(e, phi) == 1      # otherwise no d exists
d   = pow(e, -1, phi)        # modular inverse (Python 3.8+): 2753

c = pow(65, e, n)            # encrypt: 2790
m = pow(c, d, n)             # decrypt: 65
callcomputesused for
pow(m, e, n)m to the e, mod n, by square-and-multiplyencrypt, decrypt, sign, verify
pow(e, -1, phi)the modular inverse of ecomputing d at key generation
math.gcd(a, b)greatest common divisorchecking that an inverse exists

Use this for learning and for checking hand calculations only. Real code never does raw RSA arithmetic: it calls a vetted library such as Python's cryptography package, which adds the padding the next part explains.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-rsa-bigint

Python documentation — built-in pow() and math.gcd pow with three arguments and with exponent -1

51. Part 4 — Signatures

Section

Public-key crypto, run in reverse

52. Signatures: public-key crypto in reverse

Concept

Bob signs an announcement with his private key and posts it once. Anyone, a student or a total stranger, checks it with his public key and learns two things: it really came from the holder of Bob's private key, and it has not been changed.

Figure (svg): Encryption: anyone locks with PK, only Bob reads with SK. Signature: only Bob signs with SK, anyone checks with PK

Suppose Mallory intercepts the post and changes Friday to Monday. She has no private key, so she cannot produce a new signature. A verifier hashes the edited text, compares it with what the signature yields, sees they differ, and rejects it. Same key pair as encryption, opposite direction, and this is precisely what a MAC cannot give.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-sig

53. Encryption versus signature

Comparison

Fill in which key each party uses. Everything follows from who holds the private key.

Comparison matrix

encryptionsignature
who acts firstanyone (the sender)only the key owner (the signer)
key used firstpublic keyprivate key
who acts secondonly the key owneranyone (the verifier)
key used secondprivate keypublic key
what it givessecrecyintegrity, origin, non-repudiation

Encryption: many senders, one reader. Signature: one signer, many readers. The private key is always the capability only one person should have: reading, or signing.

54. RSA signatures: sign with d, verify with e

Worked example

On the running key (e = 17, d = 2753, n = 3233). Real signatures always hash the message first, which turns a message of any length into one number below n. Here we pretend the hash is 123.

sign   (only Bob, private d = 2753):
  h = hash(message)           # pretend the hash is 123
  s = h^d mod n = 123^2753 mod 3233 = 2746

verify (anyone, public e = 17):
  s^e mod n = 2746^17 mod 3233 = 123
  hash(message received)      = 123   -> equal: accept
stepwhocomputationresult
hash the messagesignerhash(message)123
signsigner, with d123 to the 2753 mod 3233s = 2746
recoveranyone, with e2746 to the 17 mod 3233123
re-hash and compareanyonehash(message received)123: accept

Figure (svg): Signing lane: hash 123, raise to d, signature 2746. Verifying lane: raise 2746 to e, get 123, compare with a fresh hash

Why it works

Why: (h to the d) to the e = h to the (e · d) = h mod n: exactly the RSA decryption argument with the two exponents used in the other order.

Verify: in Python

Why: pow(123, 2753, 3233) returns 2746 and pow(2746, 17, 3233) returns 123.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-rsa-sig

55. Real signatures with OpenSSL

Concept

Here is the same flow at real size: a 2048-bit key, SHA-256 as the hash, and PSS padding. Every command was run to produce the output shown.

$ openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out priv.pem
$ openssl pkey -in priv.pem -pubout -out pub.pem
$ openssl dgst -sha256 -sign priv.pem -sigopt rsa_padding_mode:pss -out msg.sig msg.txt
$ openssl dgst -sha256 -verify pub.pem -sigopt rsa_padding_mode:pss -signature msg.sig msg.txt
Verified OK
$ openssl dgst -sha256 -verify pub.pem -sigopt rsa_padding_mode:pss -signature msg.sig edited.txt
Verification failure
commandwhat it does
genpkey ... rsa_keygen_bits:2048makes the key pair (public exponent 65537)
pkey -puboutextracts the public key to publish
dgst -sign ... psshashes the message with SHA-256 and signs it with randomised PSS padding
dgst -verifychecks the signature with the public key: Verified OK
verify an edited filethe hash no longer matches: Verification failure

The signature is 256 bytes, the size of the modulus, whatever the length of the message. The library does the hashing and the padding; you never call pow yourself.

RFC 8017 — PKCS #1: RSA Cryptography Specifications Version 2.2 (OAEP and PSS) section 8.1, RSASSA-PSS

56. Textbook RSA has structure an attacker can use

Concept

Raw RSA signing is just raising to the power d. Powers multiply, so signatures multiply too: the product of the signatures of a and b is the signature of a times b. No key is involved in combining them.

valuecomputation on the example keyresult
signature of 22 to the 2753 mod 32331027
signature of 33 to the 2753 mod 32331857
their product1027 · 1857 mod 32332902
signature of 66 to the 2753 mod 32332902

This property is called malleability. It means old signatures can be combined into valid signatures on messages the key owner never signed. The lecture's CTF 7 builds a server that signs any number except one forbidden target, and shows that refusing one value is no protection at all, because of this multiplicative structure.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-rsa-ctf-hint1

57. A server signs anything except one message. Is that safe?

Socratic

CTF 7 in the lecture: a server signs any number you send with raw RSA, except one target M that it refuses to sign. The rule sounds strict.

Discussion prompt

Using only the fact from the last slide, explain in one or two sentences why a blocklist of forbidden messages cannot protect a raw-RSA signing service. Then say what the server should sign instead.

Hint: Can M be reached from numbers the server is willing to sign?

Answer:

Because signatures multiply, a signature on M is determined by signatures on other numbers whose product is M, and the server will happily sign those. Blocking one input does nothing when the scheme lets valid signatures be combined into new ones.

The fix is to never sign raw numbers. Sign a hash of the message with a padding scheme designed for signatures, RSASSA-PSS. Hashing destroys the multiplicative relationship between messages, and padding makes the signed number unpredictable, so combining old signatures no longer produces anything valid.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-rsa-ctf-sign

58. Trap: big keys make textbook RSA safe

Trap

The trap

A common belief: as long as n is 2048 bits or more, encrypting with pow(m, e, n) and signing with pow(m, d, n) is secure.

textbook RSA propertyconsequence
deterministicthe same message always gives the same ciphertext, so anyone can test guesses with the public key
small messages stay small0 and 1 encrypt to themselves; tiny messages can leak
multiplicativeciphertexts and signatures can be combined into new valid ones

The fix

Key size defends against factoring only. These weaknesses are in the structure of raw RSA, and they hold at any size. Real RSA always adds padding: OAEP for encryption, which adds randomness so the same message encrypts differently every time, and PSS for signatures.

You can see the randomness with OpenSSL: encrypting the same one-byte message twice with rsa_padding_mode:oaep gives two different 256-byte ciphertexts, and both decrypt to the same message.

RFC 8017 — PKCS #1: RSA Cryptography Specifications Version 2.2 (OAEP and PSS) sections 7.1 (RSAES-OAEP) and 8.1 (RSASSA-PSS)

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-rsa-ctf-hint2

59. Code review: what is wrong with this RSA module?

Error analysis

A teammate wrote this for a payments service. It works, in the sense that decrypting an encryption gives the message back. Open each note to find the problems.

Annotate

  • encrypt is deterministic: the same m always gives the same ciphertext, so anyone holding the public key can confirm a guessed message by encrypting it.
  • encrypt has no padding: small values leak (0 and 1 encrypt to themselves) and ciphertexts can be multiplied into related ones.
  • sign works on the raw number m with no hash: any length limit is n, and signatures multiply, so new valid signatures can be made from old ones.
  • verify compares against the raw m, so it accepts whatever the multiplicative structure produces; it should verify a PSS signature over a hash.
  • The fix for all of it: delete this module and call a vetted library with RSA-OAEP for encryption and RSA-PSS for signatures.

None of these flaws depends on key size. A 4096-bit key with this code is still broken in every way listed.

RFC 8017 — PKCS #1: RSA Cryptography Specifications Version 2.2 (OAEP and PSS) section 1, why raw RSA is never used directly

60. Safe RSA, or textbook RSA?

Discrimination

Sort each design by whether it is acceptable in new code.

Sort into buckets

Acceptable or broken?

acceptable
Encrypt a session key with RSA-OAEP through a vetted library; Sign SHA-256 of the message with RSA-PSS
broken
Encrypt a password with pow(m, e, n); Sign an order number by computing pow(order, d, n); Use a 512-bit modulus because it is faster
ok
Hash where needed, pad with the scheme made for the job (OAEP or PSS), and let a vetted library do it.
bad
Raw pow is deterministic and malleable, and a 512-bit modulus can be factored with modest computing power. All three are textbook RSA mistakes.

61. Part 5 — Diffie-Hellman and the hybrid pattern

Section

A shared key from public values

62. Diffie-Hellman: agree on a key over an open wire

Concept

Diffie and Hellman's 1976 idea: two strangers each mix a public value with a private secret, swap the mixes, and each mixes in their own secret again. Both end up with the same key. An eavesdropper who sees only the swapped mixes cannot rebuild it.

Figure (svg): Public g and p; Alice mixes in secret a to get A, Bob mixes in secret b to get B; they swap A and B; each computes the same key; Eve sees g, p, A, B but not a or b

The paint analogy: mixing colours is easy, separating a mix back into its colours is hard. In the real protocol, mixing is modular exponentiation and un-mixing is the discrete logarithm problem, believed hard for well-chosen groups.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-dh

Diffie, Hellman — New Directions in Cryptography (IEEE Trans. Information Theory, 1976) section III, public key distribution

63. Diffie-Hellman on small numbers

Worked example

The classic toy example, with p = 23 and g = 5. Real deployments use 2048-bit groups or elliptic curves such as X25519.

public:   p = 23, g = 5
Alice:    secret a = 6     A = 5^6  mod 23 = 8
Bob:      secret b = 15    B = 5^15 mod 23 = 19
swap A and B in the open
Alice:    B^a = 19^6  mod 23 = 2
Bob:      A^b = 8^15  mod 23 = 2      # the same shared key
partysecretsendscomputes
Alicea = 6A = 819 to the 6 mod 23 = 2
Bobb = 15B = 198 to the 15 mod 23 = 2
Evenonenothingsees 5, 23, 8, 19 only

Figure (svg): Alice: secret 6, sends 8, computes 2. Bob: secret 15, sends 19, computes 2

Why both get the same key

Why: Alice computes (g to the b) to the a and Bob computes (g to the a) to the b. Both equal g to the (a · b) mod p.

Verify: in Python

Why: pow(5, 6, 23) is 8, pow(5, 15, 23) is 19, and pow(19, 6, 23) and pow(8, 15, 23) are both 2.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-dh

64. What does Eve need to get the key?

Prediction

Eve recorded everything on the wire: g = 5, p = 23, A = 8 and B = 19.

Predict first

What would she have to compute to learn the shared key?

  • A times B mod p
  • a or b, by solving for the exponent: which power of 5 gives 8 mod 23
  • The factors of 23
  • Nothing: the key is A plus B

Correct: a or b, by solving for the exponent: which power of 5 gives 8 mod 23

Why: A times B is g to the (a + b), not g to the (a · b), so it is useless. Eve needs one of the secret exponents, which means solving a discrete logarithm. For p = 23 she can try all 22 exponents, but for a 2048-bit p, or an elliptic curve, no feasible method is known.

65. RSA or Diffie-Hellman?

Trade off

Fill in the blanks to compare the two public-key workhorses.

Comparison matrix

RSADiffie-Hellman
what it doesencrypt and signkey agreement only
keysone long-term reusable pairfresh secret per session (ephemeral)
forward secrecyno: a leaked key exposes recorded trafficyes: a new key every session
main weaknesstrusting that a public key is really Bob'sno authentication: a man in the middle unless the values are signed

Forward secrecy is the big difference. With RSA key transport, anyone who records traffic today and steals the server's private key next year can decrypt all of it. With ephemeral Diffie-Hellman the session secrets are deleted after use, so a later key theft reveals nothing about past sessions. That is why TLS 1.3 removed RSA key transport.

66. Plain Diffie-Hellman with a stranger: what is missing?

Missing information

Alice runs Diffie-Hellman with someone who says they are her bank. The math works perfectly and both sides get a shared key.

Discussion prompt

What has Alice not established, and what extra piece fixes it?

Hint: Does the math tell Alice whose value she received?

Answer:

She has not established who is on the other end. An active attacker in the middle can run one Diffie-Hellman exchange with Alice and another with the bank, and relay everything while reading it. Plain Diffie-Hellman protects against eavesdroppers, not against impersonators.

The fix is authentication: the bank signs its Diffie-Hellman value, and Alice verifies that signature with the bank's public key. That only moves the question: how does Alice know the public key is really the bank's? Answering that is the job of certificates and PKI, the next lecture.

67. Every real protocol is hybrid

Real world

TLS, SSH and Signal all combine the pieces of this lecture in the same way.

Discussion prompt

For a TLS 1.3 connection to a website, say which primitive does each job: agreeing on the key, proving the server's identity, and protecting the data.

Hint: Three jobs, three different tools.

Answer:

Key agreement: ephemeral Diffie-Hellman, usually on the X25519 curve, giving forward secrecy. Server identity: a signature by the server's long-term key (RSA-PSS or ECDSA) over the handshake, with a certificate vouching for that key. Data: an authenticated cipher such as AES-GCM or ChaCha20-Poly1305 with keys derived using HKDF, which is built from HMAC.

Public key handles the moment of meeting; symmetric crypto, which is fast, carries everything after it. Every primitive in the lecture has a job in that one connection.

68. The public-key recipe

Pattern

Whenever two parties who have never met need to communicate securely, the pattern is the same.

  1. Agree on a fresh key with ephemeral Diffie-Hellman (or, in older systems, RSA-OAEP key transport)
  2. Authenticate the exchange with a signature (RSA-PSS or ECDSA) from a key whose ownership is vouched for
  3. Switch to authenticated symmetric encryption (AES-GCM) for all the data
  4. Never use raw RSA: always a vetted library, always OAEP or PSS

Each line exists because of a failure studied in this lecture: no shared key, no authentication, slowness, and the structure of textbook RSA.

69. Part 6 — The whole lecture

Section

Each primitive solves exactly one problem

70. Takeaways from the whole cryptography lecture

Concept

This lecture began with hash functions and ends with public keys. Five lines summarise all of it.

takeawaywhere you saw it
Each primitive solves exactly one problem. Hashes give integrity against noise, MACs against people, signatures against people and to third parties.hash functions, HMAC, signatures
Determinism is the recurring sin.ECB, textbook RSA, encrypt-and-MAC all leak or fail the same way
Confidentiality is not integrity.the one-time pad, CBC and CTR all encrypt perfectly well and let an attacker edit the plaintext
The mode and the randomness break before the cipher does.AES is fine; reused IVs and nonces are not
Public key fixes key distribution, not speed.RSA or Diffie-Hellman agree on a key, then AES carries the data; RSA also signs, DH gives forward secrecy

What is left is authenticity of keys themselves: when you hold a public key, whose key is it really? That is the subject of the next lecture, certificates and PKI.

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-takeaways

71. Draw the whole lecture

Connect it up

Put every primitive from the lecture on one map.

Draw it

Draw boxes for: hash function, MAC (HMAC), block cipher (AES) with a mode (CTR or CBC), authenticated encryption, RSA encryption, RSA signature, Diffie-Hellman. Label each with the one property it gives (integrity against noise, integrity against people, confidentiality, both, public proof of origin, key agreement) and whether it needs a pre-shared key. Then draw the arrows for one TLS connection.

A good map has exactly one property per box, a clear line between shared-key and public-key tools, and a TLS path that uses Diffie-Hellman, a signature, then authenticated encryption.

72. Checkpoint: RSA by the numbers

Check

Solve it on paper before you click.

Check your understanding

An RSA key has p = 5, q = 11 and e = 3. What is d?

  • A. 7
  • B. 27 (correct)
  • C. 37
  • D. 18

Answer: B

Why: n = 55 and φ = 4 · 10 = 40. d must satisfy 3 · d ≡ 1 (mod 40): 3 · 27 = 81 = 2 · 40 + 1, so d = 27.

Why A tempts people
3 · 7 = 21, which is not 1 mod 40.
Why C tempts people
3 · 37 = 111 = 2 · 55 + 1 is 1 mod n, but d must invert e mod φ, not mod n.
Why D tempts people
3 · 18 = 54 = 40 + 14: not 1 mod 40.

73. Checkpoint: choosing the right tool

Check

Solve it on paper before you click.

Check your understanding

A vendor wants every customer's computer to confirm that an update file really came from the vendor and was not modified. Which design is correct?

  • A. Publish the SHA-256 of the file on the same download page as the file
  • B. Ship every customer the same HMAC key and attach an HMAC tag
  • C. Sign a hash of the file with the vendor's private key using RSA-PSS; customers verify with the vendor's public key (correct)
  • D. Encrypt the file with the vendor's public key

Answer: C

Why: Only a signature lets everyone verify while nobody but the vendor can create. The public key is built into the update client, so customers can check every update.

Why A tempts people
Whoever can change the file on that page can change the digest next to it: same channel, no protection.
Why B tempts people
Every customer holds the key, so any customer could tag a malicious update.
Why D tempts people
Encryption with the public key gives secrecy to the vendor, and anyone can do it; it proves nothing about origin.

74. Exit ticket

Exit ticket

One question to check the central idea.

Predict first

Why can a signature be verified by strangers while a MAC cannot?

  • Signatures use longer keys than MACs
  • A signature is checked with a public key that cannot create signatures, while checking a MAC needs the same secret that creates tags
  • MACs are broken and signatures are not
  • Signatures encrypt the message and MACs do not

Correct: A signature is checked with a public key that cannot create signatures, while checking a MAC needs the same secret that creates tags

Why: The split key is the whole difference. With a MAC, verifying and creating need the same key, so verifiers could forge and strangers cannot be given the key. With a signature, the public verification key is useless for signing, so it can be handed to the whole world.

75. What you can do now

Recap

The single thread: split the key into a public and a private half, build the split from the hardness of factoring, use it in reverse for signatures, and in practice use public key only to meet, then hand over to fast symmetric crypto.

you needuse
to agree on a key with a strangerephemeral Diffie-Hellman (X25519)
anyone to verify who sent somethinga signature: RSA-PSS or ECDSA
to send a secret to a key ownerRSA-OAEP or a hybrid scheme
to protect the data itselfauthenticated encryption such as AES-GCM

Security I, Lecture 2: Cryptography, part five (public-key cryptography) slide s-ack — Lecture materials by Suman Jana (Columbia University), with slides originally from Vitaly Shmatikov; re-typeset and extended in the Security I course

Rivest, Shamir, Adleman — A Method for Obtaining Digital Signatures and Public-Key Cryptosystems (CACM, 1978) the original RSA paper

Sources

  1. Security I, Lecture 2: Cryptography, part five (public-key cryptography)
  2. Rivest, Shamir, Adleman — A Method for Obtaining Digital Signatures and Public-Key Cryptosystems (CACM, 1978)
  3. Diffie, Hellman — New Directions in Cryptography (IEEE Trans. Information Theory, 1976)
  4. RFC 8017 — PKCS #1: RSA Cryptography Specifications Version 2.2 (OAEP and PSS)
  5. NIST SP 800-57 Part 1 Rev. 5 — Recommendation for Key Management (comparable security strengths)
  6. Python documentation — built-in pow() and math.gcd

Want this taught 1-on-1? Alexander tutors Computer Security — $55/session, free consultation.

Book on Wyzant · Text (657) 465-8108