Let $p$ and $q$ be distinct primes, $n = pq$, and $ed \equiv 1$ modulo $p - 1$ and modulo $q - 1$, as when $ed \equiv 1 \pmod{(p - 1)(q - 1)}$. Then $M^{ed} \equiv M \pmod n$ for every integer $M$, multiples of $p$ or $q$ included, so a message $0 \le M < n$ encrypted as $C = M^e \bmod n$ is recovered as $C^d \bmod n$. The proof applies Fermat's theorem modulo each prime; with a repeated prime ($n = 25$) decryption fails for the multiples of 5. The worked key is $n = 3053 = 43 \cdot 71$, $e = 13$, $d = 1357$, which encrypts 2026 to 1280. Factoring $n$ yields $d$, and $\varphi(n)$ or $d$ yields the factors. Whether decrypting without $d$ requires factoring is open, and the hardness of factoring is believed, not proved.
Suppose the numbers $n = 3053$ and $e = 13$ are published, with an invitation to send messages to their owner. To send the message $M = 2026$, anyone can compute the remainder of $2026^{13}$ on division by 3053, which is $C = 1280$, and transmit that. Recovering 2026 from 1280 looks hard: it means finding a number whose thirteenth power is 1280 in $\mathbb{Z}_{3053}$. Yet the owner recovers it with a single computation, raising 1280 to the power 1357, because the owner knows something not published: $3053 = 43 \cdot 71$. This article proves that the power 1357 undoes the power 13 for every message, and explains how the rule for encrypting can be public while the rule for decrypting stays secret.
This is the RSA cryptosystem, published by Rivest, Shamir and Adleman (1978) and named from their initials. The encryption rule is public, the decryption rule is private, and knowing the first does not, as far as anyone knows, reveal the second. Every step uses the arithmetic of $\mathbb{Z}_n$ developed earlier in the course.
- Repeated squaring makes the powers cheap.
- The inverse of a unit, found by the extended Euclidean algorithm, is the private key.
- Fermat's little theorem proves that decryption undoes encryption.
First we see the problem RSA solves and the proposal of Diffie and Hellman that framed it. Then we build the key above, encrypt 2026, and decrypt it twice, once directly and once prime by prime. Next we prove that decryption recovers every message, including the awkward ones, and see what each hypothesis does. Finally we turn to signatures, practical key sizes, and what is and is not known about why RSA is secure.
This is the second article of Public-Key Cryptography, and it assumes the first, Fermat's Little Theorem. It covers Chapter 13 of the course text (Hungerford, 2014), with Judson (2026, §7.2) as a free companion. The small numbers here are chosen for hand computation; real keys use primes of hundreds of digits.
The problem RSA solves
In a classical cipher the same secret governs both directions. A shift cipher on the alphabet, read as $\mathbb{Z}_{26}$, encrypts a letter $x$ as $x + 3$ and decrypts by subtracting 3. Anyone who knows how to encrypt knows how to decrypt. Modern symmetric ciphers are vastly stronger, but they share the structure: sender and receiver must hold the same key. So before two parties can communicate privately, they must already have exchanged a secret through some other secure channel. For a network in which strangers need to communicate, this key-distribution problem is a serious barrier.
Diffie and Hellman (1976) proposed a way around it. In a public-key cryptosystem, encryption and decryption are governed by different keys, and computing the decryption key from the encryption key is computationally infeasible. Each user can then publish an encryption key in a directory, and anyone can send that user a message that only the user can read. They also observed that such a system would give digital signatures. And they described the needed object, a trap-door one-way function: a function easy to compute and hard to invert, but easy to invert for whoever holds some secret information. They did not have one. Rivest, Shamir and Adleman (1978, §I) say that Diffie and Hellman's article "motivated our research, since they presented the concept but not any practical implementation", and RSA supplied one.
An aside: agreeing on a key in public
Diffie and Hellman did solve a related problem, letting two parties agree on a shared secret key over an open channel. Fix a prime $q$ and a primitive root $\alpha$ modulo $q$. Each party picks a secret exponent, $X_A$ and $X_B$, and publishes $Y_A = \alpha^{X_A}$ and $Y_B = \alpha^{X_B}$ in $\mathbb{Z}_q$. Then each can compute
$$K = Y_B^{X_A} = \alpha^{X_A X_B} = Y_A^{X_B},$$
and an eavesdropper who sees only $Y_A$ and $Y_B$ apparently cannot. With $q = 101$ and $\alpha = 2$ (the script checks that 2 has order 100 modulo 101), secret exponents 13 and 21 give $Y_A = 2^{13} = 11$ and $Y_B = 2^{21} = 89$. Both parties arrive at $K = 48$. An eavesdropper who could solve $2^X = 11$ in $\mathbb{Z}_{101}$ for $X$, a discrete logarithm, would break the scheme. For this toy prime a search over 100 exponents finds $X = 13$ at once. Diffie and Hellman (1976, §III) note that the security rests on the apparent difficulty of discrete logarithms for large $q$, and that they had no proof that breaking the scheme requires computing them. The scheme is a key exchange, not an encryption system with a published key, and Rivest, Shamir and Adleman (1978, §V) distinguish their method from it.
So the missing piece was still a trap-door function that anyone can apply and only the owner can undo. Here is the one RSA found.
The construction
Key generation. The owner of a key does the following.
- Choose two distinct primes $p$ and $q$ and compute $n = pq$.
- Compute $\varphi(n) = (p - 1)(q - 1)$, which is Euler's function of $n$, the number of units of $\mathbb{Z}_n$.
- Choose an integer $e$ with $1 < e < \varphi(n)$ and $\gcd(e, \varphi(n)) = 1$.
- Compute $d$ with $ed \equiv 1 \pmod{\varphi(n)}$ and $0 < d < \varphi(n)$. It exists and is unique because $e$ is a unit of $\mathbb{Z}_{\varphi(n)}$ and a unit has exactly one inverse. The inverse is not the class of 0: since $\varphi(n) \ge 2$, the classes 0 and 1 of $\mathbb{Z}_{\varphi(n)}$ are different, and $e \cdot 0 = 0 \neq 1$. So its representative lies strictly between 0 and $\varphi(n)$. The extended Euclidean algorithm finds it.
Step 3 needs $\varphi(n) > 2$. That excludes only $n = 2 \cdot 3 = 6$, where $\varphi(6) = 2$. For every other pair of distinct primes the larger prime is at least 5, so $\varphi(n) \ge 4$, and $e = \varphi(n) - 1$ is one valid choice, because consecutive integers are relatively prime.
The public key is the pair $(n, e)$. The private key is $d$, and $p$, $q$ and $\varphi(n)$ are also kept secret (or destroyed).
Encryption and decryption. A message is an integer $M$ with $0 \le M < n$; text is first converted to numbers by any agreed scheme and cut into blocks smaller than $n$. The ciphertext is
$$C = M^e \bmod n,$$
the remainder of $M^e$ on division by $n$, and the owner decrypts by computing $C^d \bmod n$. In the language of $\mathbb{Z}_n$, encryption is the function $x \mapsto x^e$ and decryption is $x \mapsto x^d$.
The original paper fixed $d$ first and computed $e$ as its inverse (Rivest, Shamir and Adleman, 1978, §V), as the course text does; the roles are symmetric. Current practice chooses $e$ first: the U.S. federal digital signature standard requires an odd $e$ with $2^{16} < e < 2^{256}$, chosen before the primes, and computes $d$ modulo $\operatorname{lcm}(p - 1, q - 1)$, which the proof below shows is enough (NIST, 2023, Appendix A.1.1).
A small key
Take $p = 43$ and $q = 71$, both prime by trial division. Then
$$n = 43 \cdot 71 = 3053, \qquad \varphi(n) = 42 \cdot 70 = 2940 = 2^2 \cdot 3 \cdot 5 \cdot 7^2.$$
Any $e$ without the prime factors 2, 3, 5 and 7 is allowed; take $e = 13$. To invert 13 modulo 2940, run the extended Euclidean algorithm, keeping each remainder as a combination $r_i = 2940\, s_i + 13\, t_i$:
| $i$ | $r_i$ | $s_i$ | $t_i$ | $q_i$ |
|---|---|---|---|---|
| 0 | 2940 | 1 | 0 | |
| 1 | 13 | 0 | 1 | 226 |
| 2 | 2 | 1 | $-226$ | 6 |
| 3 | 1 | $-6$ | 1357 | 2 |
| 4 | 0 | 13 | $-2940$ |
Row 3 reads $1 = 2940 \cdot (-6) + 13 \cdot 1357$, so $13 \cdot 1357 \equiv 1 \pmod{2940}$ and $d = 1357$. As a check, $13 \cdot 1357 = 17{,}641 = 6 \cdot 2940 + 1$. The public key is $(3053, 13)$ and the private key is 1357.
Example (which exponents are allowed). With the same primes, the owner could have chosen $e = 11$ but not $e = 14$ or $e = 15$. The script finds $\gcd(11, 2940) = 1$, while $\gcd(14, 2940) = 14$ and $\gcd(15, 2940) = 15$, so 14 and 15 have no inverse modulo 2940 and no private exponent exists for them. In fact no even $e$ ever works. Of two distinct primes at least one is odd, so $\varphi(n) = (p - 1)(q - 1)$ is even, and an even $e$ shares the factor 2 with it. That parity fact returns below.
Encrypting
To encrypt $M = 2026$, write $13 = 8 + 4 + 1$ and square repeatedly, reducing modulo 3053 at each step:
| Power | Computation | Value in $\mathbb{Z}_{3053}$ |
|---|---|---|
| $M$ | 2026 | |
| $M^2$ | $2026^2 = 4{,}104{,}676$ | 1444 |
| $M^4$ | $1444^2 = 2{,}085{,}136$ | 2990 |
| $M^8$ | $2990^2 = 8{,}940{,}100$ | 916 |
Then $M^{13} = M^8 \cdot M^4 \cdot M$: first $916 \cdot 2990 = 2{,}738{,}840 \equiv 299$, then $299 \cdot 2026 = 605{,}774 \equiv 1280$. The ciphertext is $C = 1280$, from three squarings and two multiplications.
Decrypting
The owner computes $1280^{1357}$ in $\mathbb{Z}_{3053}$. In binary, $1357 = 1024 + 256 + 64 + 8 + 4 + 1$, so ten squarings produce
$$C^{1}, C^{2}, \ldots, C^{1024} = 1280, \; 1992, \; 2217, \; 2812, \; 74, \; 2423, \; 10, \; 100, \; 841, \; 2038, \; 1364,$$
and five more multiplications combine $C^{1024} \cdot C^{256} \cdot C^{64} \cdot C^{8} \cdot C^{4} \cdot C = 1364 \cdot 841 \cdot 10 \cdot 2812 \cdot 2217 \cdot 1280$. Multiplying from the left and reducing modulo 3053 after each step, the partial products are
$$1364 \cdot 841 \equiv 2249, \quad 2249 \cdot 10 \equiv 1119, \quad 1119 \cdot 2812 \equiv 2038, \quad 2038 \cdot 2217 \equiv 2859, \quad 2859 \cdot 1280 \equiv 2026.$$
The message is back. The script checks each of the ten squares and five partial products.
The owner can do less work by decrypting modulo each prime separately. Modulo 43, the ciphertext is $1280 = 29 \cdot 43 + 33 \equiv 33$. Since 43 does not divide 1280, exponents may be reduced modulo $p - 1$, so the exponent 1357 may be replaced by $1357 \bmod 42 = 13 = 8 + 4 + 1$. The squares are $33^2 = 1089 \equiv 14$, $33^4 \equiv 14^2 = 196 \equiv 24$ and $33^8 \equiv 24^2 = 576 \equiv 17 \pmod{43}$. Then $33^{13} \equiv 17 \cdot 24 \cdot 33$, with $17 \cdot 24 = 408 \equiv 21$ and $21 \cdot 33 = 693 \equiv 5 \pmod{43}$.
Modulo 71, $1280 = 18 \cdot 71 + 2 \equiv 2$. Since 71 does not divide 1280, the exponent may again be reduced: $1357 \bmod 70 = 27 = 16 + 8 + 2 + 1$. The squares are $2^2 = 4$, $2^4 = 16$, $2^8 = 256 \equiv 43$ and $2^{16} \equiv 43^2 = 1849 \equiv 3 \pmod{71}$. Then $2^{27} \equiv 3 \cdot 43 \cdot 4 \cdot 2$, with $3 \cdot 43 = 129 \equiv 58$, $58 \cdot 4 = 232 \equiv 19$ and $19 \cdot 2 = 38$. So the message is congruent to 5 modulo 43 and to 38 modulo 71.
Only one number from 0 to 3052 has both remainders, and it is 2026: $2026 = 47 \cdot 43 + 5 = 28 \cdot 71 + 38$. Two such numbers would differ by a multiple of both 43 and 71, hence, by the lemma on relatively prime moduli, by a multiple of 3053. Two numbers from 0 to 3052 that differ by a multiple of 3053 are equal. Finding that number from its two remainders in general is the job of the Chinese remainder theorem. The standard specification of RSA allows the private key to be stored in this form: $p$ and $q$, the reduced exponents (here 13 and 27), and a coefficient for the recombination step (Moriarty et al., 2016, §3.2).
One message came back. The next section proves that every message comes back, including those that are multiples of 43 or 71.
Why decryption works
Decryption raises $C \equiv M^e$ to the power $d$, giving $M^{ed}$, so what has to be shown is that $M^{ed} \equiv M \pmod n$.
Here is the plan of the proof. Modulo the prime $p$, the exponent $ed$ is 1 more than a multiple of $p - 1$, so Fermat's theorem strips away everything but one factor of $M$; if $p$ divides $M$, both sides are 0 anyway. The same holds modulo $q$, and the lemma on relatively prime moduli combines the two congruences into one modulo $n$.
The proof uses four earlier results:
- Fermat's little theorem: if $p$ is prime and $p \nmid a$, then $a^{p-1} \equiv 1 \pmod p$;
- the lemma on relatively prime moduli: if $m \mid c$, $k \mid c$ and $\gcd(m, k) = 1$, then $mk \mid c$;
- the rule for powers of congruences: if $a \equiv b \pmod n$, then $a^k \equiv b^k \pmod n$;
- the corollary on remainders in the same article: every integer is congruent modulo $n$ to its remainder on division by $n$, and to exactly one of $0, 1, \ldots, n - 1$.
Theorem. Let $p$ and $q$ be distinct primes and $n = pq$, and let $e$ and $d$ be positive integers such that $ed \equiv 1 \pmod{p - 1}$ and $ed \equiv 1 \pmod{q - 1}$. Then
$$M^{ed} \equiv M \pmod n \quad \text{for every integer } M.$$
In particular this holds when $ed \equiv 1 \pmod{(p - 1)(q - 1)}$, since $p - 1$ and $q - 1$ both divide $(p - 1)(q - 1)$. Consequently, if $0 \le M < n$ and $C = M^e \bmod n$, then $C^d \bmod n = M$.
Proof. Modulo $p$. Since $ed \equiv 1 \pmod{p - 1}$, the positive integer $p - 1$ divides $ed - 1$, and $ed - 1 \ge 0$. So we can write $ed = 1 + (p - 1)s$ for an integer $s \ge 0$. If $p \nmid M$, then by Fermat's little theorem
$$M^{ed} = M \cdot \bigl(M^{p-1}\bigr)^{s} \equiv M \cdot 1^{s} = M \pmod p.$$
If $p \mid M$, then $M \equiv 0 \pmod p$, so $M^{ed} \equiv 0 \equiv M \pmod p$, since $ed \ge 1$. In both cases $M^{ed} \equiv M \pmod p$.
Modulo $q$. Since $ed \equiv 1 \pmod{q - 1}$ and $ed - 1 \ge 0$, we can write $ed = 1 + (q - 1)s'$ for an integer $s' \ge 0$. If $q \nmid M$, then by Fermat's little theorem for the prime $q$
$$M^{ed} = M \cdot \bigl(M^{q-1}\bigr)^{s'} \equiv M \cdot 1^{s'} = M \pmod q.$$
If $q \mid M$, then $M \equiv 0 \pmod q$, so $M^{ed} \equiv 0 \equiv M \pmod q$, since $ed \ge 1$. In both cases $M^{ed} \equiv M \pmod q$.
Modulo $n$. So $p$ and $q$ both divide $M^{ed} - M$. Distinct primes are relatively prime, as shown after the lemma on relatively prime moduli. By that lemma, $pq = n$ divides $M^{ed} - M$, which is the congruence claimed.
Decryption. Let $0 \le M < n$ and $C = M^e \bmod n$. By the corollary on remainders, $C \equiv M^e \pmod n$. By the rule for powers, $C^d \equiv (M^e)^d = M^{ed}$, and $M^{ed} \equiv M \pmod n$ was just proved. By the corollary on remainders again, $C^d \bmod n \equiv C^d \equiv M \pmod n$. The integers $C^d \bmod n$ and $M$ both lie between 0 and $n - 1$ and are congruent modulo $n$. They are equal, since every integer is congruent to exactly one of $0, \ldots, n - 1$. $\blacksquare$
The proof makes no exception. A message $M$ with $0 \le M < n$ is relatively prime to $n$, or a nonzero multiple of exactly one of the primes, or 0. The case $p \nmid M$ or the case $p \mid M$ applies at the first prime, and likewise at the second, whichever kind $M$ is.
The script confirms the theorem for every message modulo 3053, and for every pair of distinct primes below 60 with several exponents $e$ and every message.
Messages that share a factor with $n$
A shorter argument is often given, using Euler's theorem in place of Fermat's: if $\gcd(M, n) = 1$, then $M^{\varphi(n)} \equiv 1 \pmod n$, so $M^{ed} = M \cdot (M^{\varphi(n)})^t \equiv M$. That argument covers only messages relatively prime to $n$. For the others it fails at its first step.
Example (a message that shares a factor with $n$). RSA with the article's key decrypts the message $M = 2130 = 30 \cdot 71$ correctly, although Euler's argument does not apply to it. The message is a multiple of 71, and the script finds $2130^{2940} \equiv 1420 \pmod{3053}$, not 1.
To encrypt, the squares are $2130^2 \equiv 142$, $2130^4 \equiv 142^2 \equiv 1846$ and $2130^8 \equiv 1846^2 \equiv 568$. Then $2130^{13} \equiv 568 \cdot 1846 \cdot 2130$, with $568 \cdot 1846 \equiv 1349$ and $1349 \cdot 2130 \equiv 497$. The ciphertext $497 = 7 \cdot 71$ is again a multiple of 71. To decrypt, ten squarings of 497 produce
$$497, \; 2769, \; 1278, \; 2982, \; 1988, \; 1562, \; 497, \; 2769, \; 1278, \; 2982, \; 1988,$$
and the product $C^{1024} \cdot C^{256} \cdot C^{64} \cdot C^{8} \cdot C^{4} \cdot C = 1988 \cdot 1278 \cdot 497 \cdot 2982 \cdot 1278 \cdot 497$ has the partial products
$$1988 \cdot 1278 \equiv 568, \quad 568 \cdot 497 \equiv 1420, \quad 1420 \cdot 2982 \equiv 2982, \quad 2982 \cdot 1278 \equiv 852, \quad 852 \cdot 497 \equiv 2130.$$
So $497^{1357} \equiv 2130 \pmod{3053}$. The Euler argument failed, not the system. The proof above covers such messages through the case $p \mid M$, where both sides vanish modulo that prime. The original paper gave this prime-by-prime proof, crediting Richard Schroeppel with the improved argument, and observed that it shows the identity holds for all $M$ (Rivest, Shamir and Adleman, 1978, §VI). Judson (2026, §7.2) treats the second case separately with Euler's theorem modulo the other prime.
The messages in question are rare. By the count behind $\varphi(pq)$, exactly $p + q - 1$ of the numbers $0, \ldots, n - 1$ share a factor with $n$. (The count there ran over $1, \ldots, pq$. Replacing $pq$ by 0 does not change it, since both are multiples of $p$ and of $q$.) Here that is 113 of 3053, a fraction $\frac{1}{p} + \frac{1}{q} - \frac{1}{n} \approx 3.7\%$. With primes of hundreds of digits the fraction is so small that a message chosen without knowledge of $p$ or $q$ essentially never lands on one. The theorem guarantees that decryption works regardless.
What the hypotheses do
Two hypotheses of the theorem can be tested directly: that the primes are distinct, and the condition on the exponents.
The two primes must be distinct. Take $n = 25 = 5^2$ and exponents satisfying $ed \equiv 1 \pmod{\varphi(25)}$, for instance $e = 3$ and $d = 7$. The message 5 encrypts to $5^3 = 125 \equiv 0$, and then $0^7 = 0 \neq 5$. The script finds that this choice fails exactly for the four nonzero multiples of 5. The proof needed $n$ to be a product of distinct primes at the step where Fermat's theorem modulo $p$ was turned into a congruence modulo $n$. Modulo $p^2$, the case $p \mid M$ no longer makes both sides agree.
The exponent condition can be weakened, as the theorem's statement allows. For the key above, $\operatorname{lcm}(42, 70) = 210$, and $d' = 1357 \bmod 210 = 97$ satisfies $13 \cdot 97 = 1261 = 6 \cdot 210 + 1$. The script confirms that $C^{97}$ decrypts every message as well as $C^{1357}$ does. The original paper remarks that $\varphi(n)$ may be replaced by $\operatorname{lcm}(p - 1, q - 1)$ (Rivest, Shamir and Adleman, 1978, §IX-C), and the federal standard defines $d$ that way (NIST, 2023, Appendix A.1.1).
The theorem also shows that encryption is a permutation of $\{0, 1, \ldots, n - 1\}$, a one-to-one function from the set onto itself. It is one-to-one because decryption recovers the message: two messages with the same ciphertext $C$ would both equal $C^d \bmod n$. It is onto because a one-to-one function from a set of $n$ elements to itself takes $n$ different values, which must be all of them. So no two messages share a ciphertext and every number is the ciphertext of some message. The figure shows the permutation for the article's key.
Example (messages that encrypt to themselves). Under the article's key the messages 0, 1 and 3052 each encrypt to themselves, and no power needs to be computed to see it. $0^{13} = 0$ and $1^{13} = 1$. And $3052 = -1$ in $\mathbb{Z}_{3053}$, while $e = 13$ is odd, so $(-1)^{13} = -1 = 3052$. Every RSA key leaves these three alone, since $e$ is always odd: it is relatively prime to the even number $\varphi(n)$. For this key the script finds 21 messages left unchanged in all.
Signatures
Decryption undoes encryption, and encryption also undoes decryption. Because $M^{de} = M^{ed}$, the two exponents can be applied in either order: encrypting a decrypted number also returns it. This gives signatures. To sign a message $M$, the owner of the key computes
$$S = M^d \bmod n$$
with the private exponent and sends $S$ with $M$. Anyone can check the signature with the public key, by verifying that $S^e \bmod n = M$. Producing a valid $S$ for a given $M$ without $d$ means computing an $e$-th root of $M$ modulo $n$. That is believed to be infeasible for a large modulus, but it is not proved; the section on security below says what is known.
With the article's key, the signature on the message 1729 is $S = 1729^{1357} \bmod 3053 = 1477$. Ten squarings of 1729 produce
$$1729, \; 554, \; 1616, \; 1141, \; 1303, \; 341, \; 267, \; 1070, \; 25, \; 625, \; 2894,$$
and the product $2894 \cdot 25 \cdot 267 \cdot 1141 \cdot 1616 \cdot 1729$ for $1357 = 1024 + 256 + 64 + 8 + 4 + 1$ has the partial products 2131, 1119, 625, 2510 and 1477. Anyone holding the public key computes $1477^{13} \bmod 3053 = 1729$: the squares are $1477^2 \equiv 1687$, $1477^4 \equiv 573$ and $1477^8 \equiv 1658$, and then $1658 \cdot 573 \equiv 551$ and $551 \cdot 1477 \equiv 1729$. The signature depends on the message: the signature on 1730 is 1887. The two uses of a public key, privacy for messages sent to its owner and signatures by its owner, are both due to Diffie and Hellman (1976). Rivest, Shamir and Adleman (1978, §§III–IV) show how their permutation provides both, and that a function usable for signatures must be a permutation, which RSA encryption is. In practice the number signed is not the message itself but a fixed-length hash of it, formatted by a padding scheme (Moriarty et al., 2016).
Making it practical
The mathematics works for any two distinct primes. A system with public encryption is only useful if the honest parties' work is small while the attacker's is enormous. With primes of hundreds of digits, each of the owner's steps is still efficient.
- Powers. Repeated squaring computes $M^e \bmod n$ with at most about $2 \log_2 e$ multiplications and reductions (Rivest, Shamir and Adleman, 1978, §VII-A); for an exponent of 2048 bits this is a few thousand operations on numbers of a few hundred digits.
- The private exponent. The extended Euclidean algorithm finds $d$ in a number of steps proportional to the number of digits, by Lamé's bound.
- Primes. The owner generates random odd numbers of the desired size and tests them. Trial division is hopeless at this size, so the test is the strong probabilistic test built on Fermat's theorem. By the prime number theorem, about one number in $\ln x$ near $x$ is prime, so about one odd number in $\frac{1}{2}\ln x$. For 1024-bit primes, $\ln 2^{1024} \approx 709.8$, so on average about 355 odd candidates are tested before a prime is found. The original paper made the same estimate for its recommended 100-digit primes, about 115 candidates (§VII-B).
The current U.S. federal digital signature standard requires a modulus of at least 2048 bits, with $p$ and $q$ of equal length, and prescribes the Miller–Rabin test for generating them (NIST, 2023, §5.1 and Appendix B.3). The paper of 1978 recommended a modulus of about 200 decimal digits, roughly 664 bits.
Why it is believed secure
An eavesdropper sees $n$, $e$ and the ciphertext $C$. Breaking RSA has not been proved as hard as factoring $n$, and nothing in the mathematics proves that recovering $M$ is hard at all. What can be said is which attacks are known to work, which are known to be equivalent to each other, and which questions remain open.
What is proved and what is believed. Three statements in this article are theorems. Decryption recovers every message (the theorem above). Whoever knows the factors of $n$ can compute the private key. Whoever knows $\varphi(n)$ can compute the factors. A fourth, that $d$ yields the factors, is a published result that this article cites and illustrates on its own key without proving it. Two statements are beliefs, supported by decades of failed attacks and by no proof: that factoring a large $n$ is infeasible with the computers and methods now available, and that recovering messages without the private key is infeasible. Whether the second follows from the first is also unproved.
Factoring breaks RSA. Whoever factors $n$ can compute $\varphi(n) = (p - 1)(q - 1)$ and then $d$ by the extended Euclidean algorithm, exactly as the owner did. For $n = 3053$, trial division finds the factor 43 after a handful of divisions; for a modulus of 600 digits, no published method is known to finish in a feasible time.
Knowing $\varphi(n)$ is as good as factoring. Since $\varphi(n) = pq - p - q + 1 = n - (p + q) + 1$, the sum $p + q = n - \varphi(n) + 1$ follows from $n$ and $\varphi(n)$. Then $p$ and $q$ are the two roots of $x^2 - (p + q)x + n = 0$. For the article's key, $p + q = 3053 - 2940 + 1 = 114$, the discriminant is $114^2 - 4 \cdot 3053 = 12{,}996 - 12{,}212 = 784 = 28^2$, and the roots are $(114 \pm 28)/2 = 71$ and $43$. So computing $\varphi(n)$ is no easier than factoring (Rivest, Shamir and Adleman, 1978, §IX-B).
Knowing $d$ is as good as factoring. The number $ed - 1$ is a multiple of $\varphi(n)$. Rivest, Shamir and Adleman (1978, §IX-C) cite Miller for a method that factors $n$ given any such multiple. Miller's method is deterministic but assumes the unproved extended Riemann hypothesis (Miller, 1976). A randomized version needs no unproved hypothesis: given $d$, it factors $n$ efficiently with probability as close to 1 as desired (Boneh, 1999, Fact 1). Its core is the same square-root idea as the strong primality test, which split 1729 in the previous article. For the article's key, $ed - 1 = 17{,}640 = 2^3 \cdot 2205$, and the script finds $2^{2205} \equiv 214$ and $214^2 \equiv 1 \pmod{3053}$. So 214 is a square root of 1 other than $\pm 1$, and $\gcd(213, 3053) = 71$ is a factor.
Decrypting without $d$ is not known to require factoring. An attacker need not find $d$; it is enough to compute $e$-th roots modulo $n$ by some other route. Rivest, Shamir and Adleman (1978, §IX-D) conjectured that any general method of breaking their scheme would yield an efficient factoring algorithm, and said they could not prove it. The question is still open: in the words of Boudot et al. (2022), factoring and breaking RSA "are not known to be equivalent". Nor is factoring itself known to be hard; the evidence is that a great deal of effort has not produced a fast method.
How hard factoring is in practice. The original paper estimated that a 200-digit modulus would take billions of years with the best algorithm then known (§IX-A); algorithms and hardware have improved enormously since. At the time of the survey by Boudot et al. (2022), the record factorization of a modulus of RSA type was RSA-250, of 250 decimal digits (829 bits), in February 2020, using about 2,700 core-years of computation, and Boudot et al. (2022) estimate a 1024-bit modulus at roughly 200 times that effort. NIST rates a 2048-bit modulus at 112 bits of security strength and a 3072-bit modulus at 128 bits, a measure of the work needed to break it on the same scale as the key length of a symmetric cipher (Barker, 2020, §5.6.1.1, Table 2).
Quantum computers. Shor (1997) gave an algorithm that factors integers, and computes discrete logarithms, in polynomial time on a quantum computer. No quantum computer of the necessary size exists, and whether one can be built is an open question (Boudot et al., 2022). Because of the possibility, standards bodies are preparing replacements based on different mathematics, and NIST notes that schemes such as RSA may need to be replaced by quantum-resistant counterparts (Barker, 2020, §5.6.1).
The textbook scheme is not used as it stands. The construction above, often called textbook RSA, is deterministic: the same message always produces the same ciphertext.
Example (guessing the message). An eavesdropper who intercepts $C = 1280$ and suspects that the message is 2025, 2026 or 2027 can decide which, without the private key and without factoring. The public key is public, so the eavesdropper encrypts all three guesses and compares. The results are 2462, 1280 and 264, and only 2026 matches. For 2025 the squares are $2025^2 \equiv 446$, $2025^4 \equiv 471$ and $2025^8 \equiv 2025$, and then $2025 \cdot 471 \equiv 1239$ and $1239 \cdot 2025 \equiv 2462$. For 2027 they are $2027^2 \equiv 2444$, $2027^4 \equiv 1468$ and $2027^8 \equiv 2659$, and then $2659 \cdot 1468 \equiv 1678$ and $1678 \cdot 2027 \equiv 264$. The encryption of 2026 is the table above. No inverse was computed; a short list of plausible messages was enough.
Very small messages are also a problem, since if $M^e < n$ no reduction happens and an ordinary $e$-th root recovers $M$. The original paper already asked for exponents large enough that every message other than 0 and 1 is reduced modulo $n$ at least once (§VII-D). Practical RSA encryption therefore pads each message with random bits in a prescribed format before exponentiating. The standard scheme, RSAES-OAEP, is proved secure against strong attacks provided that computing $e$-th roots modulo $n$ is infeasible and under an idealized model of its hash functions (Moriarty et al., 2016, §7.1).
Worked problems
These problems are from the course's assignment on §13.1. Each is solved in full from the results of this unit. The assignment's RSA computation is worked here; its other problems are in Fermat's Little Theorem.
Problem 1. An RSA key is built from the primes $p = 13$ and $q = 17$, with public exponent $e = 19$.
(a) Find $n$. (b) Find $k = (p - 1)(q - 1)$. (c) Use the Euclidean algorithm to find $\gcd(e, k)$ and to write it as a linear combination of $e$ and $k$. (d) Determine the private exponent $d$, and say why it is the right choice. (e) Encrypt the message $M = 2$. (f) Decrypt the ciphertext $C = 76$.
(a) $n = 13 \cdot 17 = 221$. Both numbers are prime, since neither is divisible by 2 or 3, the only primes whose squares are at most 17 (trial division). They are distinct, as the construction requires.
(b) $k = 12 \cdot 16 = 192 = 2^6 \cdot 3$. By the proposition on $\varphi(pq)$, $k = \varphi(221)$, the number of units of $\mathbb{Z}_{221}$.
(c) The divisions are
$$\begin{aligned} 192 &= 19 \cdot 10 + 2, \\ 19 &= 2 \cdot 9 + 1, \\ 2 &= 1 \cdot 2 + 0. \end{aligned}$$
The last nonzero remainder is 1, so $\gcd(19, 192) = 1$. The extended table keeps each remainder as a combination $r_i = 192\, s_i + 19\, t_i$:
| $i$ | $r_i$ | $s_i$ | $t_i$ | $q_i$ |
|---|---|---|---|---|
| 0 | 192 | 1 | 0 | |
| 1 | 19 | 0 | 1 | 10 |
| 2 | 2 | 1 | $-10$ | 9 |
| 3 | 1 | $-9$ | 91 | 2 |
| 4 | 0 | 19 | $-192$ |
Row 3 reads
$$1 = 192 \cdot (-9) + 19 \cdot 91.$$
The check: $19 \cdot 91 = 1729$ and $192 \cdot 9 = 1728$. So 19 is an allowed exponent: $1 < 19 < 192$ and $\gcd(19, 192) = 1$.
(d) $d = 91$. By part (c), $19 \cdot 91 = 1 + 9 \cdot 192$, so $ed \equiv 1 \pmod{192}$, and $0 < 91 < 192$. It is the only such number in that range, because the unit 19 of $\mathbb{Z}_{192}$ has exactly one inverse. It is the right choice because of the theorem of this article. Since $ed \equiv 1 \pmod{(p - 1)(q - 1)}$, we have $M^{19 \cdot 91} \equiv M \pmod{221}$ for every integer $M$, so raising a ciphertext to the power 91 recovers every message $0 \le M < 221$, including the 29 messages that share a factor with 221.
The full answer to "which exponents decrypt" is larger. A positive integer $d'$ satisfies $M^{19d'} \equiv M \pmod{221}$ for every $M$ if and only if $19d' \equiv 1 \pmod{48}$, that is, $d' \equiv 43 \pmod{48}$. Here $48 = \operatorname{lcm}(12, 16)$.
Proof. The congruence $19x \equiv 1 \pmod{48}$ has the solution $x = 43$, since $19 \cdot 43 = 817 = 17 \cdot 48 + 1$. Its solutions are the integers congruent to 43 modulo 48, because the unit 19 of $\mathbb{Z}_{48}$ has exactly one inverse. If $19d' \equiv 1 \pmod{48}$, then $19d' \equiv 1$ modulo 12 and modulo 16, which divide 48, and the theorem gives $M^{19d'} \equiv M \pmod{221}$ for every $M$.
Conversely, suppose $M^{19d'} \equiv M \pmod{221}$ for every $M$. Take $M = 2$. Since 13 divides 221, $2^{19d'} \equiv 2 \pmod{13}$, and canceling the unit 2 of $\mathbb{Z}_{13}$ gives $2^{19d' - 1} \equiv 1 \pmod{13}$. The order of 2 modulo 13 is 12 (the table of orders), so $12 \mid 19d' - 1$ by the proposition on orders. Take $M = 3$. Since 17 divides 221, $3^{19d'} \equiv 3 \pmod{17}$, and canceling the unit 3 of $\mathbb{Z}_{17}$ gives $3^{19d' - 1} \equiv 1 \pmod{17}$. The order of 3 modulo 17 is 16 (Problem 1 of the previous article), so $16 \mid 19d' - 1$. Since 3 divides 12, both 3 and 16 divide $19d' - 1$, and they are relatively prime, so 48 divides it by the lemma on relatively prime moduli. $\blacksquare$
So the decrypting exponents are $43, 91, 139, 187, \ldots$, and $d = 91 = 43 + 48$ is the one among them that is the inverse of 19 modulo 192.
(e) $C = 2^{19} \bmod 221$. Write $19 = 16 + 2 + 1$ and square four times.
| Power | Computation | Value in $\mathbb{Z}_{221}$ |
|---|---|---|
| $2^2$ | $4$ | 4 |
| $2^4$ | $4^2 = 16$ | 16 |
| $2^8$ | $16^2 = 256 = 221 + 35$ | 35 |
| $2^{16}$ | $35^2 = 1225 = 5 \cdot 221 + 120$ | 120 |
Then $2^{19} = 2^{16} \cdot 2^2 \cdot 2$: first $120 \cdot 4 = 480 = 2 \cdot 221 + 38$, then $38 \cdot 2 = 76$. The ciphertext is $C = 76$.
(f) $M = 76^{91} \bmod 221$. Write $91 = 64 + 16 + 8 + 2 + 1$ and square six times.
| Power | Computation | Value in $\mathbb{Z}_{221}$ |
|---|---|---|
| $C^2$ | $76^2 = 5776 = 26 \cdot 221 + 30$ | 30 |
| $C^4$ | $30^2 = 900 = 4 \cdot 221 + 16$ | 16 |
| $C^8$ | $16^2 = 256 = 221 + 35$ | 35 |
| $C^{16}$ | $35^2 = 1225 = 5 \cdot 221 + 120$ | 120 |
| $C^{32}$ | $120^2 = 14400 = 65 \cdot 221 + 35$ | 35 |
| $C^{64}$ | $35^2 = 1225 = 5 \cdot 221 + 120$ | 120 |
Then $C^{91} = C^{64} \cdot C^{16} \cdot C^{8} \cdot C^{2} \cdot C = 120 \cdot 120 \cdot 35 \cdot 30 \cdot 76$. Multiplying from the left:
$$\begin{aligned} 120 \cdot 120 &= 14400 \equiv 35, \\ 35 \cdot 35 &= 1225 \equiv 120, \\ 120 \cdot 30 &= 3600 = 16 \cdot 221 + 64 \equiv 64, \\ 64 \cdot 76 &= 4864 = 22 \cdot 221 + 2 \equiv 2. \end{aligned}$$
The message is $M = 2$, the number encrypted in part (e).
As an independent check, decrypt prime by prime. Modulo 13, $76 = 5 \cdot 13 + 11$, and since $13 \nmid 76$ the exponent reduces to $91 \bmod 12 = 7 = 4 + 2 + 1$. The squares are $11^2 = 121 \equiv 4$ and $11^4 \equiv 4^2 = 16 \equiv 3$, so $11^7 \equiv 3 \cdot 4 \cdot 11 = 132 = 10 \cdot 13 + 2 \equiv 2$. Modulo 17, $76 = 4 \cdot 17 + 8$, and since $17 \nmid 76$ the exponent reduces to $91 \bmod 16 = 11 = 8 + 2 + 1$. The squares are $8^2 = 64 \equiv 13$, $8^4 \equiv 13^2 = 169 \equiv 16$ and $8^8 \equiv 16^2 = 256 \equiv 1$, so $8^{11} \equiv 1 \cdot 13 \cdot 8 = 104 = 6 \cdot 17 + 2 \equiv 2$. So $M \equiv 2$ modulo 13 and modulo 17. By the lemma on relatively prime moduli $M \equiv 2 \pmod{221}$, and since $0 \le M < 221$, $M = 2$.
Where it leads
- Decryption prime by prime, then recombination, is an instance of the Chinese remainder theorem, which identifies $\mathbb{Z}_{pq}$ with $\mathbb{Z}_p \times \mathbb{Z}_q$ and makes the recombination step explicit.
- The primality tests that find RSA primes are the computational side of Fermat's theorem; Carmichael numbers such as 1729 show why the plain Fermat test is not enough.
- Euler's theorem is a statement about the group of units $U(n)$, and Lagrange's theorem explains it in the language of groups.
- Diffie–Hellman key exchange computes in the multiplicative group of the field $\mathbb{Z}_q$; other finite fields, constructed in Finite Fields and Field Extensions, and other groups can replace it.
What we have learned
An RSA key is two distinct primes $p$ and $q$, the public pair $n = pq$ and $e$, and the private inverse $d$ of $e$ modulo $(p - 1)(q - 1)$, found by the extended Euclidean algorithm. Encryption and decryption are powers in $\mathbb{Z}_n$, cheap by repeated squaring. Fermat's theorem modulo each prime, combined by the lemma on relatively prime moduli, proves $M^{ed} \equiv M$ for every message, including the multiples of $p$ or $q$ that Euler's theorem alone misses. So 1357 undoes 13 for all 3053 messages, and the same exponents in the other order give signatures. Factoring $n$ breaks the system, and so does knowing $\varphi(n)$ or $d$. Whether breaking it requires factoring is still open.
That completes the unit. Its arithmetic, like that of the first unit, was arithmetic of integers: division, gcds, Bezout's identity and primes. The next unit, Polynomial Arithmetic, asks how much of the same theory holds for polynomials with coefficients in $\mathbb{Z}$, in a field, or in a ring such as $\mathbb{Z}_{12}$. It finds that polynomials form rings of their own, and its first article, Polynomial Rings, shows that over $\mathbb{Z}_{12}$ two nonzero polynomials can multiply to zero.
References
- Barker, E. (2020). Recommendation for Key Management: Part 1, General, NIST Special Publication 800-57 Part 1, Revision 5. National Institute of Standards and Technology. §5.6.1.1, Table 2: a 2048-bit RSA modulus provides 112 bits of security strength and a 3072-bit modulus 128 bits; §5.6.1: RSA-based schemes may need quantum-resistant replacements. record
- Boneh, D. (1999). Twenty years of attacks on the RSA cryptosystem. Notices of the American Mathematical Society, 46(2), 203–213. Fact 1: given the private exponent $d$, the modulus $n$ can be factored efficiently, and conversely. record
- Boudot, F., Gaudry, P., Guillevic, A., Heninger, N., Thomé, E., and Zimmermann, P. (2022). The state of the art in integer factoring and breaking public-key cryptography. IEEE Security & Privacy, 20(2), 80–86. Factoring and breaking RSA are not known to be equivalent; RSA-250 (829 bits) was factored in February 2020 for about 2,700 core-years; a 1024-bit modulus would take about 200 times as much; whether quantum computers able to run Shor's algorithm can be built is open. record
- Diffie, W., and Hellman, M. E. (1976). New directions in cryptography. IEEE Transactions on Information Theory, IT-22(6), 644–654. Public-key cryptosystems and public key distribution, signatures, trap-door one-way functions; §III, key exchange by exponentiation modulo a prime, whose security rests on the apparent difficulty of logarithms modulo $q$, with no proof of the converse. record · 2 pages
- Hungerford, T. W. (2014). Abstract Algebra: An Introduction (3rd ed.), Chapter 13. Brooks/Cole, Cengage Learning. The course text: the RSA construction and the proof that decryption works (Theorem 13.3). record · 44 pages
- Judson, T. W. (2026). Abstract Algebra: Theory and Applications (annual edition, August 2026), §7.2. Free online at judsonbooks.org. Public-key cryptography and RSA, with a proof of correctness that treats messages sharing a factor with $n$ separately. record · 44 pages
- Miller, G. L. (1976). Riemann's hypothesis and tests for primality. Journal of Computer and System Sciences, 13(3), 300–317. https://doi.org/10.1016/S0022-0000(76)80043-8. A deterministic primality test whose polynomial running time is proved assuming the extended Riemann hypothesis, and the method of factoring that Rivest, Shamir and Adleman cite; preliminary version in the Proceedings of the Seventh Annual ACM Symposium on Theory of Computing (1975), 234–239. record
- Moriarty, K. (Ed.), Kaliski, B., Jonsson, J., and Rusch, A. (2016). PKCS #1: RSA Cryptography Specifications Version 2.2, RFC 8017. Internet Engineering Task Force. §3.2: the second form of the private key, $p$, $q$, the exponents reduced modulo $p - 1$ and $q - 1$, and the coefficient $q^{-1} \bmod p$; §7.1: RSAES-OAEP and the conditions of its security proof; signature schemes that sign an encoded hash. record · 2 pages
- National Institute of Standards and Technology (2023). Digital Signature Standard (DSS), FIPS 186-5. §5.1: moduli of at least 2048 bits with primes of equal length; Appendix A.1.1: $e$ odd with $2^{16} < e < 2^{256}$, chosen before the primes, and $d = e^{-1} \bmod \operatorname{lcm}(p - 1, q - 1)$; Appendix B.3: the Miller–Rabin test. record · 2 pages
- Rivest, R. L., Shamir, A., and Adleman, L. (1978). A method for obtaining digital signatures and public-key cryptosystems. Communications of the ACM, 21(2), 120–126. §§II–IV: the properties of a public-key cryptosystem, privacy and signatures, credited to Diffie and Hellman; §V: the method, with $d$ chosen first; §VI: the proof of correctness for all $M$, credited to Schroeppel; §VII: repeated squaring, finding primes, computing $e$ from $d$, and the wrap-around condition; §IX: factoring $n$ breaks the system, $\varphi(n)$ or $d$ yields the factors, $\varphi(n)$ may be replaced by $\operatorname{lcm}(p - 1, q - 1)$, and the open conjecture that breaking the system requires factoring. record · 3 pages
- Shor, P. W. (1997). Polynomial-time algorithms for prime factorization and discrete logarithms on a quantum computer. SIAM Journal on Computing, 26(5), 1484–1509. Preliminary version in the Proceedings of the 35th Annual Symposium on Foundations of Computer Science (1994). Read in the arXiv version, quant-ph/9508027. record
Reviewed: verified, 16 Nov 2026 · checked against sources, 23 Nov 2026 · editorial review, 23 Nov 2026 · 6 revisions