★ Public-key cryptography and RSA [00IB]
- July 17, 2026
Warm-up [00IC]
- July 17, 2026
Can you decrypt this coded message?
J GPS POF XFMDPNF PVS OFX VOJDPSO PWFSMPSET
Symmetric ciphers [00ID]
- July 17, 2026
The code used in the warm-up is a Caesar cipher—shift every letter by the same amount through the alphabet. It’s not a very secure cipher, but there are (slightly) more secure variants, such as a Vigenère cipher, where instead of a single shift amount we choose a sequence of shift amounts and cycle through them. Typically one would choose a key word and use the letters of the key word to determine the shift amounts.
These are all known as symmetric ciphers: in order to communicate, sender and receiver must have a shared, secret key. The secret key must be known by the people communicating (and no one else), and the same key is used for both encrypting and decrypting. This works fine in many situations, but it has some drawbacks:
- The two parties must somehow agree on a secret key before they can communicate. In order to keep the key a secret, typically this must be done in person.
- Because of this need to agree on a secret key, it is impossible for people who have never met to communicate securely.
Public-key cryptography [00IE]
- July 17, 2026
There is a different idea known as a public-key cryptosystem which does not have these drawbacks. We’ll start by outlining the general properties of such a system.
- Everyone who wants to communicate picks their own personal pair of keys: a private key and a public key.
- The private key should be kept absolutely, 100% secret.
- The public key should be published as widely as possible. You should put your public key on your website, in a public directory of public keys (like a phone book), and so on.
- In order to send a message, the sender looks up the public key of the recipient and uses it to encode the message.
- The recipient decodes the message using their private key.
In order for this to make sense the system should have a few properties:
- A public and private key work together so that messages encrypted using a public key can only be decrypted using the corresponding private key.
- It should be impossible to figure out someone's corresponding private key if you only know their public key.
It is not at all obvious that such a scheme is even possible! But it is possible using some clever mathematics.
Some key facts [00IF]
- July 17, 2026
Here are a few mathematical and computational facts that you'll have to take on faith for now.
- Multiplying two large integers can be done very quickly. By large we have in mind something like 500 digits. This may sound like a lot, but a modern computer can multiply two 500-digit numbers in a miniscule fraction of a second. If you were abducted by a sadistic math nerd, locked in a room, and told you will not be let out until you correctly multiply two 500-digit numbers, it would be tedious but you could do it. You could probably do it in an afternoon if you were focused and determined.
- On the other hand, the opposite operation—factoring a large integer into its prime factors—is believed to be very difficult. Given a 1000-digit number n, there are approximately 8 \times 10^{496} primes less than \sqrt {n}. Even if you could test a trillion trillion trillion trillion trillion primes per second to see whether they are a divisor of n, it would still take many lifetimes of the universe to try them all. We do know of better methods than that—but not anywhere near good enough to make it in any way feasible.
- You might think that this means testing whether a number is prime is difficult too—surely, the only way to test whether a number is prime is to try factoring it? Surprisingly, this is false—it is possible to quickly test whether a number is prime without factoring it! (In fact, most such tests rest fundamentally upon Fermat's Little Theorem, which tells us that if p is prime and p does not divide a, then a^{p-1} \equiv _p 1—so we can take a number n we want to test and some a < n and compute a^{n-1} \mathbin {\mathbf {mod}} n. If we get something other than 1, then n is definitely not prime. This isn’t a great test, but more sophisticated tests are based on similar ideas.)
These ingredients come together to give us RSA.
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.
Decrypting messages with RSA [00IL]
- July 17, 2026
To decrypt the encrypted message C, the recipient simply does the same thing but using their private key d as the exponent instead of e! That is, they compute C^d \mathbin {\mathbf {mod}} n.
Theorem RSA works [00IM]
- July 17, 2026
Encrypting and then decrypting in this way results in recovering the original message, that is, (M^e)^d \equiv _n M.
Proof.
- July 17, 2026
Since de \equiv _{(p-1)(q-1)} 1, there exists some integer k such that de = 1 + k(p-1)(q-1). Also, by Fermat's Little Theorem, if p \nmid M, we have M^{p-1} \equiv _p 1, so (M^e)^d = M^{1 + k(p-1)(q-1)} = M \cdot (M^{p-1})^{k(q-1)} \equiv _p M \cdot 1^{k(q-1)} = M. Likewise, by a similar argument, (M^e)^d \equiv _q M.
Now, M is obviously a solution to the system of equations \begin {align*}x &\equiv _p M \\ x &\equiv _q M \end {align*} But since p and q are primes and hence share no common factors, by the Chinese Remainder Theorem we know that the solution to these equations is unique modulo pq = n. Hence (M^e)^d \equiv _n M.
Digital signatures [00IN]
- July 17, 2026
There is still a problem with this scheme. Under a symmetric cipher, where sender and receiver share a secret key, when the receiver gets a message encrypted using the secret key they can be sure it came from the sender (unless someone has stolen the secret key). However, in a public key system, anyone in the world can encrypt a message to the receiver, so the receiver has no way of knowing who the message is actually from. There is no a priori reason to trust who the message says it is from, because anyone can write a message that says “Hi, this is Alice” whether or not they are actually Alice.
However, this problem can be solved very elegantly by noticing that the operations of encryption and decryption are symmetric, that is, (M^e)^d = (M^d)^e. That is, a private key can actually be used to encrypt a message, and then the corresponding public key will work to decrypt it. So, suppose A wants to send a secure message to B, and they also want B to be able to verify that the message actually came from them. Instead of directly encrypting the message for B, first A encrypts the message using A’s private key. This is known as a digital signature. Then, A takes the resulting signed message and encrypts it using B’s public key as usual. When B receives the encrypted message, they first decrypt it using their private key as usual. The result will not be readable since it has been encrypted using A’s private key. But B can now recover the original message by decrypting using A’s public key, which will undo the effects of encrypting with A’s private key. B can now be sure the message came from A, because the only way to make a message that can be successfully decrypted using A’s public key would be to use A’s private key; and the only person who can do that (assuming A’s private key is secret) is A.