RSA [00IG]
- July 17, 2026
RSA was discovered in 1977 by Ron Rivest, Adi Shamir, and Leonard Adleman (the name "RSA" just comes from the initials of their last names). It was actually independently discovered four years earlier, by Clifford Cocks, while he was working for the UK’s GCHQ—basically the equivalent of the NSA—but this wasn’t known until 1997 when it became declassified.
Generating public/private key pairs with RSA [00IH]
- July 17, 2026
Here’s how generating a public/private key pair works in RSA:
- Pick two large (say, 500-digit) primes p and q. This can be done efficiently: just pick a random 500-digit number and test it to see if it is prime (which can be done efficiently), if not, pick another one and repeat. For 500-digit numbers about one in every 2300 integers is prime, so you might have to repeat this procedure a few thousand times before finding a prime, but that’s not a big deal. Even if this process were to take a few seconds or even minutes, you only have to do it once when creating a public and private key.
- Compute n = pq. As mentioned previously, this can be done very quickly.
- Choose some e such that 1 < e < (p-1)(q-1) and e shares no common factors with (p-1)(q-1). For example, we could randomly pick an e and compute the \gcd (which is very efficient using the Euclidean Algorithm) to make sure it is 1. If not, pick a different e and repeat. In practice, it can be beneficial to use specific values of e to make encoding more efficient, so one could instead fix the value of e and keep picking p and q until \gcd (e, (p-1)(q-1)) = 1.
- Find d such that de \equiv _{(p-1)(q-1)} 1, that is, the modular inverse of e modulo (p-1)(q-1). Since e shares no common factors with (p-1)(q-1), a modular inverse is guaranteed to exist, and we can find it easily with the extended Euclidean algorithm.
- p, q, and d are the private key and should be kept absolutely secret.
- n and e are the public key and should be published widely.
Remark [00II]
- July 17, 2026
As far as we know, of course, it is very difficult to find p and q by factoring n. In turn this means it is difficult to find d from e. We can easily compute d from e if we know (p-1)(q-1), but there’s no way to find (p-1)(q-1) from n without first factoring n to find out what p and q are. This means that no one can figure out d, p, or q (the private key) if they only know n and e (the public key).
Encrypting messages with RSA [00IJ]
- July 17, 2026
To encrypt a message:
- Find or look up the public key values n and e for the desired recipient.
- Turn the message into an integer M < n. It does not matter how, as long as the sender and receiver agree on how this will be done. The method can even be public—knowing how the message is encoded as an integer will not help anyone read it once it is encrypted. In practice, most information that people want to encrypt is already stored on a computer as a sequence of bits, which can readily be interpreted as an integer. If the message is too long to fit into an integer which is less than n, it can simply be broken into multiple parts, and each part encoded separately.
- Compute C = M^e \mathbin {\mathbf {mod}} n. This can be done efficiently due to two key observations:
- It is not necessary to first compute M^e (which might be truly enormous) and then reduce modulo n at the end. Because (a\cdot b) \mathbin {\mathbf {mod}} n = ((a \mathbin {\mathbf {mod}} n) \cdot (b \mathbin {\mathbf {mod}} n)) \mathbin {\mathbf {mod}} n, we can keep reducing modulo n along the way, after every multiplication. That way, we never have to deal with numbers bigger than n^2.
- We don’t have to simply multiply M by itself e times. Instead we can use something called repeated squaring. For example, if we want to compute M^{16}, we can first compute M \cdot M = M^2; then we can square that to get (M^2) \cdot (M^2) = M^4; squaring that yields (M^4) \cdot (M^4) = M^8; and one final squaring yields M^{16}. If we want an exponent which is not a power of two we can simply throw in some extra multiplications by M (for example, if we want M^9 we can get M^8 as described above and then multiply by M). In this way we can compute M raised to very large powers with only a few multiplications (it takes about 2 \log _2 e multiplications in the worst case for exponent e).
- Send C.
Remark [00IK]
- July 17, 2026
It turns out that given only C, e, and n, it is (as far as we know) very difficult to find an M such that M^e \equiv _n C. So no one can read the encrypted message C even if they know the public key (e,n) that was used to encrypt it.