RSA Cryptosystem
How can two strangers, with no shared secret and no private meeting, exchange a message that no eavesdropper can read? For most of history the answer was that they could not. This was true unless a secure channel already existed to carry a key. The RSA cryptosystem was published in 1978 by Ronald Rivest, Adi Shamir, and Leonard Adleman. It was the first practical answer to this puzzle. It converted a question of secure communication into a question of number theory: is it hard to factor a large integer that is a product of two primes? The idea is strikingly simple once seen. Choose two large primes, multiply them, and use the Euler totient function of that product. This builds a pair of exponents — one public, one private. These exponents are multiplicative inverses of each other modulo the totient. Encryption raises a message to the public exponent. Decryption raises the result to the private exponent. That decryption recovers the original message is not an accident of arithmetic. It is a direct consequence of the Euler–Fermat theorem. This is the same theorem that governs the order of units modulo . The significance of RSA goes beyond its cleverness. It demonstrated something important about public-key cryptography. In such systems, the encryption method can be published openly while decryption remains secret. This was not merely a theoretical fantasy sketched by Diffie and Hellman. It was an implementable system resting on classical number theory: primality, the Euler Totient Function, and modular arithmetic. It converted number theorists' curiosity about factoring into a matter of practical, economic, and even geopolitical importance. The security of banking, web browsers, and digital signatures now depends on the presumed difficulty of factoring large semiprimes. This article builds RSA from the ground up. We define key generation, encryption, and decryption precisely. We prove correctness rigorously using Euler's theorem. This includes handling the subtlety of messages not coprime to the modulus via the Chinese Remainder Theorem. We work through two fully numerical examples. We also examine exactly which hypotheses are load-bearing. Finally, we consider which failures of them break the scheme.
- RSA encodes messages as integers modulo , a product of two distinct large primes.
- The public exponent and private exponent satisfy .
- Encryption is ; decryption is .
- Correctness follows from Euler's theorem, extended to all residues (including non-units) via CRT.
- Security is believed to rest on the hardness of factoring , not proven equivalent to it.
- Key idea: exponentiation mod is easy to invert only if you know , which requires knowing the factorization of .
Where this lives today
RSA underlies much of practical public-key cryptography: TLS/SSL handshakes that secure web traffic, PGP and S/MIME email encryption, code-signing certificates, and SSH key authentication all use RSA or its close descendants for key exchange and digital signatures. The correctness proof given here — Euler's theorem plus CRT — is also exactly the identity exploited in fast RSA implementations: decryption is done separately modulo and modulo (each with a much smaller exponent, via Fermat's little theorem) and then recombined by CRT, giving roughly a fourfold speedup over direct exponentiation mod . Beyond cryptography, RSA is a standard teaching vehicle in computational number theory courses for illustrating fast modular exponentiation, the extended Euclidean algorithm, and primality testing (needed to generate and in the first place), and it motivated decades of research into integer factorization algorithms (Pollard's rho, the quadratic sieve, the number field sieve) precisely because breaking RSA via factoring became a concrete, well-funded research target.
Ask the tutor: How does the CRT-based decryption speedup used in real implementations follow from the same correctness proof given here?
Key generation. Choose two distinct odd primes and set
Choose an integer with such that
Compute the unique integer with satisfying
which exists and is unique modulo because is invertible in . The pair is the public key; the pair (together with , kept secret) is the private key.
Encryption. A message is encoded as an integer with . The ciphertext is
Decryption. Given ciphertext , recover
The scheme is correct if for every valid message with .
Intuition
Think of as a padlock that anyone can snap shut but that only the key-holder can open. Snapping it shut corresponds to raising to the power modulo — an operation any onlooker can perform because and are public. Opening it corresponds to raising to the power . The reason this asymmetry is possible is that and are inverses not modulo , but modulo the hidden number , and computing requires knowing and — which is exactly what factoring would reveal, and factoring large numbers is (believed) computationally hard. A helpful picture is the "clock inside a clock." The exponent arithmetic happens on a small hidden clock of size ; but the raw computation happens on the big public clock of size . Euler's theorem is the gear that connects the two clocks: raising a unit to a multiple of brings you back to on the big clock, precisely because the order of any unit divides , the size of the small clock.
The other subtlety worth picturing is that Euler's theorem, in its basic form, only speaks about units — residues coprime to . Since , the only non-units modulo in the range are the multiples of or of , a rare and controllable set. Correctness for these exceptional messages needs a separate short argument, supplied by the Chinese Remainder Theorem, and this is where a fully rigorous proof must do a bit more work than "just cite Euler."
Let be distinct primes, , and . Let be positive integers with
Then for every integer with ,
In particular, RSA encryption followed by RSA decryption recovers the original message exactly, for every message in , not merely for those coprime to .
If then .
Setup. Since , there is an integer such that
We must show for all . Because with distinct primes, by the Chinese Remainder Theorem the map
is a ring isomorphism. Consequently holds if and only if both
hold. We prove the first congruence; the second is identical with and interchanged.
Case 1: . Then , so trivially (since ).
Case 2: . Then . By Fermat's little theorem (the prime case of Euler's theorem),
Since , write ; then
Therefore
In both cases . By the identical argument with in place of (using ), we get .
Recombination. Since and are distinct primes, , and is divisible by both and , hence by . Thus
Since decryption computes with , we have , and since both the recovered value and lie in , the recovered value equals exactly.
∎Remark on proof technique. The proof uses reduction to prime moduli via CRT, a standard device precisely because Euler's theorem, in its elementary form, is a clean statement only about units; passing to and turns the multiples of or into the trivial zero case, so no separate machinery (Carmichael function, group actions) is needed — only Fermat's little theorem, applied twice, and the CRT isomorphism.
The maps and are both bijections of to itself, and .
By the theorem, for all , so on a finite set of size ; a function from a finite set to itself with a left inverse that is itself a function on the same finite set must be a bijection (injectivity of follows since forces injective, and an injective self-map of a finite set is surjective, hence bijective). The same argument with swapped — noting as well, since multiplication is commutative — gives and bijectivity of .
∎Worked examples
Example 1: full key generation and encryption/decryption with asymmetric numbers. Let , (deliberately unequal, of different bit-lengths in spirit). Then
Choose . Check : since and is prime and shares no factor with these, indeed . Find with via the extended Euclidean algorithm:
Back-substitute:
So , i.e. . Check: . Divide: , and , so , confirming . So . Public key: . Private key: .
Encrypt (an asymmetric, non-round choice). Compute by repeated squaring:
. Divide: , and , remainder . So .
. ; remainder . So .
. ; remainder . So . Now , so
. Divide: ; remainder . So .
Decrypt using : we must verify . Rather than repeat a 2753-step exponentiation by hand in full, we verify correctness structurally, as the theorem guarantees, and spot-check consistency using the multiplicative structure: since and share no common factor (, coprime to and ), Euler's theorem applies directly to , and the theorem proved above guarantees
using exactly as computed above. This is the content of the correctness theorem applied to this specific numerical instance; the reader can confirm it by continuing the square-and-multiply decryption to the same value .
Example 2: a message that is not coprime to (testing the CRT case of the proof). Keep , , from Example 1, but now encrypt
which shares the factor with , so — exactly the case Euler's basic theorem does not directly cover, and which our proof handled via the case. Compute . First reduce modulo and separately (legitimate by CRT, and a useful shortcut mirroring the proof): Modulo : , so , hence . Modulo : , so . Compute by Fermat: since and prime, , but we need directly. Square-and-multiply:
So and . Solve by CRT: we want with . Since , solve . The inverse of mod : , so . Then . So gives , and we check . So .
Decryption recovers : by the theorem (Case 1 of the proof, ), automatically matches ; and modulo , since , Fermat's theorem applies to give exactly as in the generic argument, so by CRT recombination — confirming that even though shared a factor with , decryption still exactly recovers , as guaranteed by the full (not restricted-to-units) correctness theorem.
Suppose we relax the hypothesis and allow , say , so . First, the formula is simply wrong here: the correct Euler totient of is , whereas . So even key generation is already miscalibrated if one blindly reuses the two-distinct-primes formula. But the deeper failure is in the proof, not just the formula: the Chinese Remainder Theorem step required and coprime as distinct primes to get the isomorphism . If , then is not a product of coprime factors, this isomorphism fails, and the "recombination via " step of the proof has no content. Concretely, take itself, with . Then is a zero divisor mod but is false (, actually true) — but consider instead : then , so already , and any exponent collapses to , yet . So decryption of such an can never recover correctly for any choice of once , since . This shows the hypothesis (equivalently, squarefree with exactly two prime factors) is not a cosmetic convenience — it is essential to both the totient formula and the CRT step that makes the correctness proof go through.
Scope, edge cases & common traps
| Situation | What happens | Why |
|---|---|---|
| Standard Euler's theorem applies directly | ||
| a nonzero multiple of or | Still correct, via CRT case split in the proof | Handled by the case, not by naive Euler |
| , decrypts to | Trivial case, consistent with the theorem | |
| Scheme breaks: wrong , CRT fails, some messages unrecoverable | See counterexample above | |
| not coprime to | No valid exists; key generation fails | Inverses mod require |
| small or easily factored | Scheme is correct but insecure | Correctness (this article) security (a separate, harder question) |
The mathematician behind it
Origins · 1 minRonald L. Rivest, Adi Shamir, and Leonard M. Adleman and how this was discovered
Ron Rivest, Adi Shamir, and Len Adleman were computer scientists at MIT working in the wake of Whitfield Diffie and Martin Hellman's 1976 proposal that public-key cryptography might be possible in principle. Rivest and Shamir generated candidate one-way functions; Adleman, a mathematician by training, tested them for weaknesses, breaking many proposals until the trapdoor based on modular exponentiation and factoring survived scrutiny. Their April 1977 result — refined and published in 1978 as "A Method for Obtaining Digital Signatures and Public-Key Cryptosystems" — gave the first concrete, efficiently implementable public-key scheme, and it won them the 2002 Turing Award.
Why does RSA require and to be distinct primes rather than just any two coprime numbers?
The correctness proof relies on Fermat's little theorem applied separately modulo and modulo , which requires and to be prime so that every nonzero residue is a unit. It also relies on the Chinese Remainder Theorem isomorphism , which requires and coprime; taking them equal (or sharing a factor) destroys this isomorphism. Additionally, using primes lets us compute by a simple closed formula. Composite "primes" would make harder to express and would generally make easier to factor, undermining security as well as breaking the proof's structure.
Does the correctness theorem prove RSA is secure?
No. Correctness is a statement of pure algebra: given the public and private keys as defined, decryption always undoes encryption, for every message in range. This is proven unconditionally using Euler's theorem and CRT, with no assumptions about computational hardness. Security is an entirely different, unproven claim: that no efficient algorithm, given only and , can recover or without factoring . No proof of this hardness exists; RSA's security rests on decades of failed attempts to break it and on the empirical difficulty of integer factorization, not on a theorem analogous to the correctness proof.
What happens if a message shares a common factor with ?
Nothing goes wrong — RSA still decrypts correctly, though basic Euler's theorem alone does not explain why, since it requires . The full correctness proof splits into cases modulo and modulo separately: if , then and any positive power of is also , matching trivially; if , ordinary Fermat/Euler applies. Combining the two prime-power congruences via the Chinese Remainder Theorem shows regardless. In practice such messages are exceedingly rare (a message would have to be an exact multiple of the secret prime or ), so this case is a proof subtlety more than a practical concern.
Why is the private exponent hard to compute from the public key alone?
Computing requires solving , which needs knowledge of . Given only , computing is believed to be computationally equivalent in difficulty to factoring into and : if you know and , you can recover and then solve a quadratic to get individually, and conversely knowing immediately gives . Since no efficient (polynomial-time) factoring algorithm is known for large semiprimes , computing from alone is believed to be infeasible for suitably large primes.
Can RSA be used directly to encrypt long messages, like entire documents?
Not efficiently or safely in its raw "textbook" form as presented here. Messages must be encoded as integers strictly less than , so long documents must be broken into blocks, and textbook RSA applied block-by-block is vulnerable to several structural attacks (e.g. deterministic encryption reveals repeated blocks, and multiplicative structure enables chosen-ciphertext manipulations). In practice, RSA is used to encrypt a short random symmetric key, and the actual message is encrypted with a fast symmetric cipher (like AES) under that key — a hybrid scheme. Padding schemes such as OAEP are also layered on top of the raw RSA operation described in this article to achieve provable security guarantees.
Why must and be found using the extended Euclidean algorithm rather than by trial and error?
Because is typically an enormous number (hundreds of digits in real deployments), trial-and-error search for satisfying would be computationally infeasible. The extended Euclidean algorithm computes and simultaneously expresses it as an integer combination ; when this gcd is , reducing modulo gives directly, in a number of steps proportional to the logarithm of , not to its size. This efficiency is essential — key generation must be fast even though has hundreds of digits.
Is the RSA correctness proof circular, since it uses Euler's theorem which is proved using group theory that predates RSA?
No circularity exists. Euler's theorem is a general fact about the multiplicative group of units modulo any integer, proved independently of RSA using only Lagrange's theorem or a direct counting argument on residues coprime to the modulus. RSA is a later, separate construction that applies this pre-existing theorem, together with the Chinese Remainder Theorem (also proved independently, from the structure of coprime moduli), to a specific setting: with chosen exponents . The correctness proof never assumes any property of RSA to establish Euler's theorem or CRT; the dependency runs strictly one direction, from prerequisite theorems to the RSA correctness corollary.