Public-key cryptography and RSA
Public-key cryptography uses pairs of mathematically related keys so anyone can encrypt (or verify) with a public key while only the holder of the private key can decrypt (or sign); RSA is the classic scheme based on modular arithmetic and integer factoring.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Public-key cryptography (also called asymmetric cryptography) is used by engineers to enable secure key exchange, digital signatures, and secure channels (HTTPS, SSH, email security). RSA is the standard example: it lets one publish a public key so anyone can send confidential messages while only the private-key holder can decrypt. In practice, RSA is used in a hybrid scheme to encrypt a random symmetric session key (not large messages directly).
Key ideas
Private key: the secret value used for decryption or signing.
One-way function: a function f that is easy to compute for any input but hard to invert without extra information.
Trapdoor one-way function (trapdoor): a one-way function f with a special secret (the trapdoor) that makes inversion feasible. RSA uses modular exponentiation as a trapdoor function: computing m^e mod n is easy; computing m from c = m^e mod n is believed to be hard without the trapdoor (the private key). Factoring n (or knowing φ(n)) suffices to invert RSA, but in general breaking RSA is not proven equivalent to factoring.
Prime number: an integer > 1 with no positive divisors other than 1 and itself. RSA chooses two large primes p and q.
Greatest common divisor (gcd): gcd(a,b) is the largest positive integer dividing both a and b.
Euler's totient function φ(n): number of integers in {1,2,...,n} that are coprime to n. For n = p·q with distinct primes p and q, φ(n) = (p−1)(q−1).
Modular inverse: For integers a and m with gcd(a,m)=1, the modular inverse of a modulo m is an integer x such that a·x ≡ 1 (mod m).
Euler's theorem (exact statement): If gcd(a,n)=1, then a^{φ(n)} ≡ 1 (mod n). Assumptions: n≥1 integer, a integer with gcd(a,n)=1.
Fermat's little theorem (special case): If p is prime and p ∤ a, then a^{p−1} ≡ 1 (mod p).
Chinese Remainder Theorem (CRT) (statement for two moduli): If p and q are coprime, the system x ≡ a (mod p) and x ≡ b (mod q) has a unique solution modulo pq. In RSA proofs CRT is used to combine congruences modulo p and q into a congruence modulo n = p·q.
RSA scheme (step-by-step, as universities expect):
Key generation:
- Choose two distinct large primes p and q (keep them secret).
- Compute n = p·q.
- Compute φ(n) = (p−1)(q−1).
- Choose an integer e with 1 < e < φ(n) and gcd(e, φ(n)) = 1 (e is the public exponent). In practice, e is almost always chosen as 65537 for efficiency and security trade-offs.
- Compute d such that e·d ≡ 1 (mod φ(n)). d is the modular inverse of e modulo φ(n); d is the private exponent. (Equivalently, many texts use λ(n) = lcm(p−1, q−1) and set d ≡ e^{-1} (mod λ(n)).)
- Public key = (n, e). Private key = (n, d) and keep p, q secret.
Encryption (sender): to send message m (0 ≤ m < n), compute ciphertext c = m^{e} mod n.
Decryption (receiver): compute m' = c^{d} mod n. Correctness: m' = m (explain below). In implementations, decryption is sped up using CRT to work modulo p and q and then recombine.
Why decryption works (derivation universities expect):
Case 1: gcd(m,n) = 1. Then Euler's theorem gives m^{φ(n)} ≡ 1 (mod n). Since e·d ≡ 1 (mod φ(n)), there exists k with e·d = 1 + k·φ(n). Then m^{e·d} = m^{1 + k·φ(n)} = m·(m^{φ(n)})^{k} ≡ m·1^{k} = m (mod n). Thus c^{d} = m^{e·d} ≡ m (mod n).
Case 2: gcd(m,n) ≠ 1 (i.e., m divisible by p or q). Use CRT: show congruences modulo p and q separately. For each prime r ∈ {p,q}: if m ≡ 0 (mod r), then m^{e·d} ≡ 0 ≡ m (mod r); otherwise gcd(m,r)=1 and by Fermat's little theorem m^{r−1} ≡ 1 (mod r), so with e·d ≡ 1 (mod r−1) we get m^{e·d} ≡ m (mod r). By CRT, m^{e·d} ≡ m (mod n).
Security assumption: the difficulty of RSA inversion (finding m from c without the private key) is related to, but not proven equivalent to, the hardness of factoring n into p and q; factoring n (or knowing φ(n)) lets you invert RSA, but the converse is unknown. The best known classical algorithms (General Number Field Sieve) are sub-exponential, so keys must be large (recommendations evolve; verify current standards). Quantum algorithms (Shor) break RSA efficiently if a large-scale quantum computer exists.
Diagram to draw for exam answers: draw two boxes labelled Sender and Receiver. From Receiver draw an arrow labelled "public key (n,e)" pointing to Sender. From Sender draw arrow labelled "ciphertext c = m^e mod n" pointing to Receiver. Also show a small lock icon near public key and a key icon inside Receiver to represent private key (n,d). Label each arrow and box clearly.
Formulas
n = p · q — n is modulus, dimensionless integer, applies when p and q are primes.
φ(n) = (p − 1)(q − 1) — φ(n) is Euler's totient for n = p·q with distinct primes p and q, dimensionless.
gcd(a, b) — greatest common divisor of integers a and b, dimensionless.
e · d ≡ 1 (mod φ(n)) — e is public exponent, d is private exponent; units: dimensionless integers; applies in RSA key generation.
c = m^e mod n — ciphertext formula, m (plaintext) and c are integers 0 ≤ value < n; dimensionless.
m = c^d mod n — decryption formula, dimensionless.
If gcd(a,n)=1 then a^{φ(n)} ≡ 1 (mod n) — Euler's theorem, dimensionless; applies when a and n are coprime.
Note: crypto key lengths are measured in bits (unit: bits). All arithmetic values above are integers (dimensionless).
Worked examples
Example 1 — RSA key generation, encryption and decryption (classic small numbers):
- Choose primes p = 61 and q = 53.
- Compute n = p·q = 61 · 53 = 3233.
- Compute φ(n) = (61 − 1)(53 − 1) = 60 · 52 = 3120.
- Choose e = 17 (check gcd(17, 3120) = 1).
- Compute d such that 17·d ≡ 1 (mod 3120). Using extended Euclidean algorithm (see Example 2) we get d = 2753.
- Public key: (n = 3233, e = 17). Private key: d = 2753.
- Encrypt message m = 65: compute c = 65^{17} mod 3233.
- Using fast modular exponentiation gives c = 2790. (Compute by repeated squaring with reductions.)
- Decrypt: compute m' = 2790^{2753} mod 3233 = 65.
- Final answer: decrypted message = 65.
Example 2 — find modular inverse (extended Euclidean algorithm) of 17 modulo 3120: We solve 17·d + 3120·y = 1.
- 3120 = 17·183 + 9 (since 17·183 = 3111)
- 17 = 9·1 + 8
- 9 = 8·1 + 1
- 8 = 1·8 + 0 Back-substitute:
- 1 = 9 − 8·1
- 8 = 17 − 9·1 ⇒ 1 = 9 − (17 − 9) = 2·9 − 17
- 9 = 3120 − 17·183 ⇒ 1 = 2·(3120 − 17·183) − 17 = 2·3120 − 366·17 − 17 = 2·3120 − 367·17 So −367·17 + 2·3120 = 1 ⇒ d ≡ −367 ≡ 3120 − 367 = 2753 (mod 3120). Final answer: modular inverse is 2753.
Example 3 — message not coprime to n still decrypts (small test):
- Let p = 5, q = 11 → n = 55, φ = 40. Choose e = 3, find d = 27 because 3·27 = 81 ≡ 1 (mod 40).
- Let m = 10 (gcd(10,55)=5 ≠1). Encrypt: c = 10^3 mod 55 = 1000 mod 55 = 10.
- Decrypt: c^{27} mod 55 = 10^{27} mod 55 = 10 (computation using exponent rules). So RSA returns 10. This matches the CRT-based correctness proof.
Common mistakes
- Using very small primes or small key sizes (e.g., 512-bit prime factors) — insecure because factoring is feasible.
- Choosing an unusually small private exponent d — vulnerable to Wiener's attack; avoid implementations that try to minimize d.
- Forgetting to check message range: messages must be represented as integers 0 ≤ m < n (use padding schemes in practice).
- Using textbook RSA without probabilistic padding (e.g., RSAES-OAEP for encryption, RSASSA-PSS for signatures) — insecure because textbook RSA is deterministic and malleable.
- Incorrectly proving correctness by applying Euler's theorem without handling the case gcd(m,n) ≠ 1; exam answers must mention CRT or handle both cases.
- Confusing φ(n) with (p−1)(q−1) — state assumption n = p·q with p,q prime.
For your exam
Question types you will get:
- Definitions: define public-key cryptography, Euler's theorem, φ(n), trapdoor function.
- Short notes: steps of RSA key generation, why RSA is secure (factorization assumption), role of padding.
- Derivations/proofs: prove that decryption recovers plaintext (show case gcd(m,n)=1 and explain CRT case), derive d from e.
- Numericals: generate small RSA keys and encrypt/decrypt a small message; compute modular inverse using extended Euclid.
- Diagrams: draw sender/receiver flow with public key, ciphertext arrow, private key kept secret.
What a full-marks answer includes:
- Precise definitions (bold or explicit terms), all assumptions (p and q prime), correct formulas, step-by-step derivation of correctness including Euler's theorem and handling gcd(m,n) ≠ 1 via CRT, and a correct numerical example (show intermediate steps like modular reductions and extended Euclid work). Mention practical considerations (padding, key sizes) briefly if asked about security.
Quick check
- In RSA, how is φ(n) computed for n = p·q?
- State Euler's theorem.
- If e·d ≡ 1 (mod φ(n)), what is the operation to compute d from e?
Answers: 1) φ(n) = (p − 1)(q − 1). 2) If gcd(a,n)=1 then a^{φ(n)} ≡ 1 (mod n). 3) Compute modular inverse of e modulo φ(n) (use extended Euclidean algorithm).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?