RSA & public-key cryptography
◈ 6 cardsKey generation from two primes, encryption/decryption by modular exponentiation, why correctness follows from Euler/Fermat, and a runnable toy RSA round-trip and signature.
Public-key cryptography: the asymmetry
In a public-key system each party has two keys: a public key anyone may use, and a secret key kept private. They are inverse transformations — and for every message — but knowing does not reveal . RSA realizes this with modular exponentiation: the public and secret operations are and , where and are exponents that undo each other modulo .
The number theory behind it
RSA depends on Euler's phi function — the count of integers in that are relatively prime to — and on Euler's theorem:
When is a product of two distinct primes, . Fermat's little theorem is the special case for prime — it patches the correctness proof when happens to share a factor with .
Key generation
- Choose two large random primes (1024 bits each in practice).
- Compute the modulus and .
- Pick a small odd with (often ).
- Compute with EXTENDED-EUCLID (lesson 8.1).
- Publish ; keep secret. Discard , , .
Encryption of a message (an integer with ): . Decryption: . Both are single modular exponentiations — the algorithm from 8.1.
Why decryption inverts encryption
By construction , so for some integer . Then
using Euler's theorem (and Fermat + the Chinese Remainder Theorem to cover the rare case). The round-trip is exact, not approximate.
Worked example — the classic small instance
Take , . Then and . Choose (it is coprime to ); EXTENDED-EUCLID gives . To send :
- Encrypt: .
- Decrypt: . ✓
Signatures, and why it is secure
Running the keys in the other order gives a digital signature: the owner signs with the secret key, , and anyone verifies with the public key by checking . Only the secret-key holder could have produced .
Security rests on the hardness of factoring: recovering requires , which requires the factorization . No polynomial-time factoring algorithm is known — the best (general number field sieve) is sub-exponential — so for 2048-bit this is infeasible. (Note: finding the primes in step 1 is easy — primality testing, e.g. Miller–Rabin, is fast; it is un-multiplying that is hard. That gap is the whole game.)