RSA
Idea of the algorithm
Multiplying two large, randomly chosen primes p and q is easy. Finding them from their product n is hard. RSA makes n public and uses the secret primes to calculate a private exponent d.
Anyone can raise a message number m to the public exponent e to get a ciphertext c. Raising c to d recovers the message. Both calculations keep the remainder after division by n.
- Message
- Ciphertext
- Recovered
Mathematical property
- Number theory
- Number theory studies whole numbers and their factors. A prime is a whole number greater than 1 whose only positive divisors are 1 and itself. Two numbers are coprime if their only shared positive divisor is 1.
Euler’s theorem
If m and n share no factor greater than 1, raising m to the power φ(n) leaves a remainder of 1. For a product of two distinct primes, φ(n) = (p − 1)(q − 1).
RSA chooses e and d so that ed = 1 + kφ(n). For a message coprime to n, this leaves one factor of m and k factors that each equal 1 modulo n. The remainder is the original message.
See the proof for every message
When the message and modulus are coprime
Write n as the product of two distinct primes, p and q. Then φ(n) = (p − 1)(q − 1). It counts the numbers from 1 to n − 1 that are coprime to n. Choosing e coprime to φ(n) guarantees a private inverse d.
When the message shares a prime factor
Check each prime separately. If the prime divides m, the original and the decrypted number both have remainder zero. Otherwise, Fermat’s little theorem gives the same remainder because ed − 1 is a multiple of that prime minus one.
The Chinese remainder theorem says the two remainders determine one number from 0 to n − 1. This covers every message in that range, including zero.
This lab uses small primes and textbook RSA so you can inspect the arithmetic. Real RSA encryption uses much larger keys and randomized padding.