The RSA Cryptosystem
RSA (Rivest, Shamir, Adleman, 1977) was the first practical public-key cryptosystem: anyone can encrypt a message with your public key, but only you can decrypt it. It still secures much of the web, code signing and SSH. Everything it needs is in the last three lessons: primes, modular inverses and Euler's theorem.
Key generation
- Pick two large distinct primes and (today 1024 bits or more each).
- Compute and .
- Pick with (usually ).
- Compute with the extended Euclidean algorithm.
The public key is ; the private key is (and are kept secret too).
To encrypt a message (an integer with ): . To decrypt: .
A toy example
With , : , , , and (check: ).
p, q = 61, 53
n, phi = p * q, (p - 1) * (q - 1)
e = 17
d = pow(e, -1, phi)
print("public key:", (n, e), " private d:", d)
message = "HI!"
ms = [ord(ch) for ch in message] # each character code is < n
cs = [pow(m, e, n) for m in ms]
print("plain codes:", ms)
print("ciphertext: ", cs)
back = [pow(c, d, n) for c in cs]
print("decrypted: ", back, "->", "".join(chr(x) for x in back))public key: (3233, 17) private d: 2753
plain codes: [72, 73, 33]
ciphertext: [3000, 1486, 1853]
decrypted: [72, 73, 33] -> HI!
Why decryption works
We need . Since , write .
Work modulo first. If , Fermat gives , so
If , both sides are . So in all cases; likewise modulo . Since and both divide and are distinct primes, their product divides it (unique factorization).
Why it is believed secure
Computing from is easy if you know , and is easy if you know and . So RSA's security rests on factoring being hard. The best known classical algorithm (the number field sieve) takes time that grows faster than any polynomial in the number of digits; a 2048-bit modulus is far out of reach. A large quantum computer running Shor's algorithm would break RSA, which is why "post-quantum" schemes are being deployed.
For the toy key, factoring is instant, and the attacker recovers the private key:
from sympy import factorint
n, e, c = 3233, 17, 3000
p, q = factorint(n) # trivial for tiny n, infeasible for 2048-bit n
d = pow(e, -1, (p - 1) * (q - 1))
print("factors:", p, q, " recovered d:", d, " plaintext:", pow(c, d, n))factors: 53 61 recovered d: 2753 plaintext: 72
Signatures
RSA also signs: the owner computes for the hash of a document, and anyone checks with the public key. Only the holder of can produce a valid .
Textbook RSA is not safe as is
The scheme above ("textbook RSA") is deterministic: the same message always gives the same ciphertext, so an attacker can test guesses. Small messages with small can be recovered by an ordinary integer root. Real systems add randomized padding (OAEP for encryption, PSS for signatures) and use vetted libraries. Never implement RSA yourself for production use.
Exercises
Exercise 1: Make a key
With , and , compute , and .
Answer
, , .
Solution
, . We need : , so .
Exercise 2: Encrypt and decrypt (code)
With the key of Exercise 1, encrypt and decrypt the result.
Answer
; decrypting gives 88.
Solution
n, e, d = 187, 7, 23
c = pow(88, e, n)
print(c, pow(c, d, n))11 88
Exercise 3: Why must ?
What goes wrong if and ?
Answer
has no inverse modulo , so there is no decryption exponent; and encryption is not injective.
Solution
, so no satisfies . Moreover maps and to the same value (), so two messages share a ciphertext and decryption cannot be unique.
Test yourself
In RSA, which values form the public key?
How is computed from ?
RSA's security relies mainly on the difficulty of…
- computing gcds
- factoring
- fast exponentiation
- testing primality