RSA
RSA key generation, encryption and decryption, worked in full for both of the lecture's own examples, and why the scheme is fast to use but hard to break.
- Generate an RSA key pair by hand: choose p and q, compute n and phi(n), pick e, and derive d.
- Encrypt and decrypt a message with RSA, reproducing every intermediate value.
- Explain why computing C = M^e mod n is fast while recovering M without d is believed infeasible.
- State the message-size constraint M < n and what happens if it is violated.
22 min read
Intuition
The previous page defined a trapdoor one-way function in the abstract. RSA is the concrete instance built from the RSA problem: multiply two primes to get a modulus , and factoring back into those primes is believed infeasible once they are large enough. Everything below is one algorithm, worked twice, with every intermediate value shown so you can check your own working against it.
Mechanism
Key generation. Four steps, in order:
- Choose two distinct primes and . In practice these are very large; the examples below use small ones so the arithmetic stays checkable by hand.
- Compute the modulus .
- Compute — Euler’s totient of , using the shortcut that applies when is a product of two distinct primes (see primes, Fermat, Euler and fast exponentiation for the general definition).
- Choose a public exponent with and , then compute the private exponent as the modular inverse of modulo : . The method is the extended Euclidean algorithm — see extended Euclid and modular inverses.
The public key is ; the private key is . , and can be discarded once is computed — they are never needed again, and keeping them around only gives an attacker more to steal.
Encryption, by anyone holding the public key: , where the plaintext satisfies .
Decryption, only by the holder of the private key: .
RSA is a function from to , and that choice is deliberate. Exponentiation in is fast — repeated squaring computes in about multiplications, never of them — so encryption and decryption are both cheap once you know the exponent you are raising to. Recovering from without knowing means finding an -th root modulo instead: the RSA problem, believed infeasible for large enough . The entire scheme rests on that gap between “fast if you know the exponent” and “hard if you don’t.”
The public key {e, n} and private key{d, n} both use the same modulus n, butd depends on φ(n), which depends on the two prime factors of n — recovering d from the public key alone means factoring n, believed infeasible for large enough primes. Anyone with the public key can encrypt; only the holder of d can decrypt. Numbers throughout are the lecture's own worked example, not illustrative round figures.
Worked example
Answerp=17, q=11, e=7 gives n=187, phi(n)=160, d=23. M=88 encrypts to C=11 and decrypts back to 88.
This is the lecture’s main worked example.
- Choose , .
- .
- .
- Choose . Check — 7 is prime and does not divide 160, so this holds.
Find by the extended Euclidean algorithm on and :
— remainder 0, so , the last non-zero remainder.Back-substitute to write as a combination of and :
substitute :So , which the lecture confirms directly: . Since , .
- , .
Encrypt : compute by repeated squaring.
, so : , then .
.Decrypt : compute by repeated squaring.
, , , , .
, so : , then , then .
— recovered exactly.
Worked example
Answerp=7, q=11, e=13 gives n=77, phi(n)=60, d=37. M=3 encrypts to C=38 and decrypts back to 3.
This is the lecture’s toy example, from the slides.
- Choose , .
- .
- .
- Choose . It is less than 60 and coprime to it.
Find by the extended Euclidean algorithm on and :
— remainder 0, so .Back-substitute from the bottom up:
So , and the least non-negative residue of modulo is . , matching the lecture’s stated value directly.
- , .
Encrypt : compute .
, , , .
, so , and .
.Decrypt : compute .
, , , , , .
, so : , then .
— recovered exactly.
Exam detail
Non-numerical messages have to be mapped to numbers before RSA can touch them — the algorithm only encrypts integers in . A common convention maps letters to numbers (, ), but any fixed, invertible mapping works; the lecture does not test a specific one.
Pitfall
is the modular inverse of modulo , not modulo . It is easy to mix these up because both moduli appear on the same page — only exists during key generation and is never used again once is derived, while appears in both the public and private key and is used in every encryption and decryption.
Recall
For p=7, q=11, phi(n)=60. Why must e=13 be checked against phi(n), and not against n=77?
Because is defined as the inverse of modulo , not modulo — a modular inverse only exists when . Checking against instead would test the wrong condition entirely; a modulus of is what matters here, not .
Source
Week 5 notes PDF