Named after Rivest, Shamir, Adleman. A block cipher over Zn used for both encryption and digital signatures. Security rests on the integer factorization problem: given n=pq for 2 large equal-size primes, finding p,q is computationally intractable.
The interactive guide to RSA steps through key generation, encryption, and why factoring n is hard.
Compute n=pq, public, its bit length is the RSA key length.
Compute ϕ(n)=(p−1)(q−1), kept secret.
Choose e with 1<e<ϕ(n) and gcd(e,ϕ(n))=1, public.
Compute d≡e−1(modϕ(n)), secret.
Public key: {e,n}. Private key: {d}. p, q, and ϕ(n) must be destroyed after generation.
Primality Testing
p and q are found by drawing random odd integers of the target bit length and testing each for primality until one passes.
Trial division by the first few hundred small primes rejects most candidates in a few operations.
Survivors go to the Miller-Rabin probabilistic test.
Miller-Rabin tests an odd n>2. Write n−1=2sm with m odd. Pick a random base a with 2≤a≤n−2. Then n passes base aiffam≡1(modn)ora2rm≡−1(modn) for some r with 0≤r<s.
A composite n passes at most 1/4 of the bases. Running k independent random bases bounds the probability of accepting a composite by 4−k. k around 40 is standard.
Each base costs 1 modular exponentiation, O((logn)3) bit operations with schoolbook multiplication. So a candidate is classified in time polynomial in the key length.
Trace for n=221, base a=137:
n−1=220=22⋅55, so s=2, m=55.
ammodn=13755mod221=188, not 1 and not 220.
r=1: 1882mod221=205, not 220.
No residue reached −1, so 137 is a witness and 221 is composite.
Indeed 221=13×17.
Encryption and Decryption
c=memodn,m=cdmodn
Encryption cost is a single exponentiation with a small exponent e. Decryption cost is a single exponentiation with an exponent up to the full length of n, far more expensive.
Hybrid Encryption
RSA is not used to encrypt large data directly, since modular exponentiation on large numbers is slow and the message must be smaller than n. Instead:
The sender picks a random symmetric session key k and encrypts the file with a symmetric cipher such as AES under k.
The sender encrypts k with the receiver’s RSA public key and sends both the encrypted file and the encrypted key.
The receiver recovers k with their RSA private key, then decrypts the file.