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
Title
Computer Security — Crypto Lecture 8
How two strangers get secrecy, signatures and a shared key without meeting first
Section
Everything so far assumed a shared secret
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
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.
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 privately | shared keys needed | for 300 people |
|---|---|---|
| each person with one instructor | one per person | 300 |
| every pair of people | n(n − 1) / 2 | 44,850 |
| with public-key crypto | one key pair per person | 300 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
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?
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.
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
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.
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 AES | with public key | |
|---|---|---|
| setup before the first message | a secret key shared with each student | post one public key |
| 300 students | 300 secret keys delivered securely | nothing delivered secretly |
| who can read a student's message | the instructor, and anyone else holding that key | only 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
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.
| property | MAC (shared key) | signature (key pair) |
|---|---|---|
| who can create a valid tag | everyone who can verify | only the private-key holder |
| who can verify | only key holders | anyone with the public key |
| proof to a third party | no | yes: 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
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
Sorting
Sort each scenario to the use it relies on.
Sort into buckets
Private message, signature, or key agreement?
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?
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.
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?
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.
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
| requirement | what it says |
|---|---|
| correctness | Decrypt(SK, Encrypt(PK, M)) = M for every message |
| trapdoor | Encrypt 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
Matching
Five pieces of the formal definition, five meanings.
Match the pairs
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.
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).
Section
Euler's totient and Euler's theorem
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.
| fact | statement | what 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 theorem | if gcd(a, n) = 1 then a to the power φ(n) is 1 mod n | decryption 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
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
| n | numbers coprime to n | φ(n) |
|---|---|---|
| 7 | 1 2 3 4 5 6 | 6 |
| 8 | 1 3 5 7 | 4 |
| 9 | 1 2 4 5 7 8 | 6 |
| 10 | 1 3 7 9 | 4 |
| 12 | 1 5 7 11 | 4 |
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
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.
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)?
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
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
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)| step | general | n = 15 = 3 · 5 |
|---|---|---|
| start with all numbers | pq | 15 |
| remove multiples of p | q of them | 5: 3, 6, 9, 12, 15 |
| remove multiples of q | p of them | 3: 5, 10, 15 |
| add back pq, removed twice | + 1 | + 1 |
| result | pq − q − p + 1 | 15 − 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
Pattern
Three cases cover every totient RSA will ever need.
| n is | φ(n) | example |
|---|---|---|
| a prime p | p − 1 | φ(7) = 6 |
| a product of two different primes p · q | (p − 1)(q − 1) | φ(15) = 2 · 4 = 8 |
| anything else | count, 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.
Check
The classic RSA example uses p = 61 and q = 53.
Check your understanding
What is φ(3233), where 3233 = 61 · 53?
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.
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) | a | powers of a mod n | back to 1 at |
|---|---|---|---|---|
| 10 | 4 | 3 | 3, 9, 7, 1 | power 4 |
| 14 | 6 | 3 | 3, 9, 13, 11, 5, 1 | power 6 |
| 22 | 10 | 7 | 7, 5, 13, 3, 21, 15, 17, 9, 19, 1 | power 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
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?
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.
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
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.
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.
| line | statement | reason |
|---|---|---|
| 1 | n divides a · x − a · y = a(x − y) | same remainder mod n |
| 2 | n divides x − y | a shares no factor with n, so every factor of n is in x − y |
| 3 | x − y is a multiple of n | that is what dividing means |
| 4 | x − y is not 0 | we assumed x ≠ y |
| 5 | −n is less than x − y, which is less than n | both lie between 1 and n |
| 6 | contradiction | the 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
Ranking
Rebuild the argument from memory.
Put in order
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.
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)| step | what happens | why |
|---|---|---|
| multiply both rows | same four numbers, so same product | step 1: it is a shuffle |
| collect the 3s | 3 to the power 4 times 1 · 3 · 7 · 9 | one 3 for each of the φ(10) = 4 numbers |
| cancel the product | multiply both sides by 9, the inverse of 9 mod 10 | the product is coprime to 10, so it has an inverse |
| result | 3 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
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.
Section
Rivest, Shamir, Adleman, 1977
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
| step | on the example | public or secret |
|---|---|---|
| pick two random primes p, q | 61 and 53 | secret |
| multiply: n = p · q | 3233 | public |
| Fact 1: φ = (p − 1)(q − 1) | 60 · 52 = 3120 | secret |
| pick e sharing no factor with φ | 17 | public |
| solve e · d ≡ 1 (mod φ) | d = 2753, since 17 · 2753 = 15 · 3120 + 1 | secret |
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
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)?
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.
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| a | b | quotient | remainder |
|---|---|---|---|
| 3120 | 17 | 183 | 9 |
| 17 | 9 | 1 | 8 |
| 9 | 8 | 1 | 1 |
| 8 | 1 | 8 | 0 |
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
Concept
A cheat sheet, so the letters stop blurring together. Everything splits into two piles.
| symbol | what it is | relation | who sees it |
|---|---|---|---|
| p, q | the two prime factors | chosen at random | secret |
| n | the modulus | n = p · q | public |
| φ(n) | Euler's totient of n | (p − 1)(q − 1) | secret |
| e | encryption exponent | shares no factor with φ(n) | public |
| d | decryption exponent | e · d ≡ 1 (mod φ) | secret |
| (e, n) | public key | encrypt and verify | public |
| (d, n) | private key | decrypt and sign | secret |
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
Sorting
Sort every RSA value by who is allowed to know it.
Sort into buckets
Which pile does each value belong to?
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 65 | how | value mod 3233 |
|---|---|---|
| 1 | start | 65 |
| 2 | square | 992 |
| 4 | square | 1232 |
| 8 | square | 1547 |
| 16 | square | 789 |
| 17 = 16 + 1 | multiply by 65 | 2790 |
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
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.
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.
| line | in symbols | on the example |
|---|---|---|
| 1. how d was chosen | e · d = 1 + k · φ | 17 · 2753 = 46801 = 1 + 15 · 3120 |
| 2. split the exponent | m to the (1 + kφ) = m · (m to the φ) to the k | 65 to the 46801 = 65 · (65 to the 3120) to the 15 |
| 3. Fact 2, if gcd(m, n) = 1 | m to the φ ≡ 1 (mod n) | 65 to the 3120 ≡ 1 (mod 3233) |
| 4. what is left | m · 1 to the k = m | 65 · 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
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.
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.
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
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 n | comparable symmetric key | status |
|---|---|---|
| 1024 bits | about 80 bits | no longer acceptable |
| 2048 bits | about 112 bits | today's minimum |
| 3072 bits | about 128 bits | recommended 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
Ranking
Order the obstacles from the attacker's goal back to the hard problem underneath.
Put in order
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.
Two truths and a lie
Two of these statements are true. Find the myth.
Eliminate the wrong options
Which statement is the myth?
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.
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| call | computes | used for |
|---|---|---|
| pow(m, e, n) | m to the e, mod n, by square-and-multiply | encrypt, decrypt, sign, verify |
| pow(e, -1, phi) | the modular inverse of e | computing d at key generation |
| math.gcd(a, b) | greatest common divisor | checking 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
Section
Public-key crypto, run 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
Comparison
Fill in which key each party uses. Everything follows from who holds the private key.
Comparison matrix
| encryption | signature | |
|---|---|---|
| who acts first | anyone (the sender) | only the key owner (the signer) |
| key used first | public key | private key |
| who acts second | only the key owner | anyone (the verifier) |
| key used second | private key | public key |
| what it gives | secrecy | integrity, 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.
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| step | who | computation | result |
|---|---|---|---|
| hash the message | signer | hash(message) | 123 |
| sign | signer, with d | 123 to the 2753 mod 3233 | s = 2746 |
| recover | anyone, with e | 2746 to the 17 mod 3233 | 123 |
| re-hash and compare | anyone | hash(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
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| command | what it does |
|---|---|
| genpkey ... rsa_keygen_bits:2048 | makes the key pair (public exponent 65537) |
| pkey -pubout | extracts the public key to publish |
| dgst -sign ... pss | hashes the message with SHA-256 and signs it with randomised PSS padding |
| dgst -verify | checks the signature with the public key: Verified OK |
| verify an edited file | the 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
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.
| value | computation on the example key | result |
|---|---|---|
| signature of 2 | 2 to the 2753 mod 3233 | 1027 |
| signature of 3 | 3 to the 2753 mod 3233 | 1857 |
| their product | 1027 · 1857 mod 3233 | 2902 |
| signature of 6 | 6 to the 2753 mod 3233 | 2902 |
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
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
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 property | consequence |
|---|---|
| deterministic | the same message always gives the same ciphertext, so anyone can test guesses with the public key |
| small messages stay small | 0 and 1 encrypt to themselves; tiny messages can leak |
| multiplicative | ciphertexts and signatures can be combined into new valid ones |
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
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
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
Discrimination
Sort each design by whether it is acceptable in new code.
Sort into buckets
Acceptable or broken?
Section
A shared key from public values
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
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| party | secret | sends | computes |
|---|---|---|---|
| Alice | a = 6 | A = 8 | 19 to the 6 mod 23 = 2 |
| Bob | b = 15 | B = 19 | 8 to the 15 mod 23 = 2 |
| Eve | none | nothing | sees 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
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?
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.
Trade off
Fill in the blanks to compare the two public-key workhorses.
Comparison matrix
| RSA | Diffie-Hellman | |
|---|---|---|
| what it does | encrypt and sign | key agreement only |
| keys | one long-term reusable pair | fresh secret per session (ephemeral) |
| forward secrecy | no: a leaked key exposes recorded traffic | yes: a new key every session |
| main weakness | trusting that a public key is really Bob's | no 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.
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.
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.
Pattern
Whenever two parties who have never met need to communicate securely, the pattern is the same.
Each line exists because of a failure studied in this lecture: no shared key, no authentication, slowness, and the structure of textbook RSA.
Section
Each primitive solves exactly one problem
Concept
This lecture began with hash functions and ends with public keys. Five lines summarise all of it.
| takeaway | where 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
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.
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?
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.
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?
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.
Exit ticket
One question to check the central idea.
Predict first
Why can a signature be verified by strangers while a MAC cannot?
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.
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 need | use |
|---|---|
| to agree on a key with a stranger | ephemeral Diffie-Hellman (X25519) |
| anyone to verify who sent something | a signature: RSA-PSS or ECDSA |
| to send a secret to a key owner | RSA-OAEP or a hybrid scheme |
| to protect the data itself | authenticated 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
Want this taught 1-on-1? Alexander tutors Computer Security — $55/session, free consultation.