Caloogy Article
中文

Why RSA Decrypts Correctly: Euler's Theorem and the Chinese Remainder Theorem

Date
Jun 9, 2025
Series
Cryptography
Reading
5 min
Topics
RSAEuler's theoremLagrangeCRT

The one-sentence version

RSA turns a plaintext mm into me mod nm^e \bmod n, then recovers it with cd mod nc^d \bmod n. This works because the unit group Zn×\mathbb{Z}_n^\times has order φ(n)\varphi(n), and every element raised to the power φ(n)\varphi(n) equals 11 (Lagrange's theorem). Real implementations also use the Chinese Remainder Theorem to split one large operation modulo nn into two small ones modulo pp and modulo qq, which is about 4 times faster.

This article has three layers:

  1. The intuition: public keys, private keys and the "one-way door"
  2. Real-world use: HTTPS certificates, signatures and some classic failures
  3. The mathematics: Euler's theorem, why ed≡1ed \equiv 1, CRT speedups and a worked example

Layer 1: The Intuition

1.1 A padlock anyone can close but only you can open

An RSA public key is like an open padlock: you can make thousands of copies and send them around the world, and anyone can use one to lock a box. But only you hold the key, so only you can open a locked box.

Analogy In RSA
Open padlock (share freely) Public key (n,e)(n, e)
Key (keep hidden) Private key dd
Locking a box Encryption c=me mod nc = m^e \bmod n
Opening a box Decryption m=cd mod nm = c^d \bmod n

1.2 A one-way door: easy to multiply, hard to take apart

RSA's security comes from a very plain fact:

The private key dd is computed from pp and qq. An outsider only knows nn, and without factoring nn cannot compute dd. A structure that is "easy in one direction, hard in the other, but easy again if you know a secret (p,qp, q)" is called a trapdoor one-way function.

1.3 Why "a power of a power" returns to the start

Picture a clock face, not with 12 positions but with very many. Start from mm and step forward by multiplying by mm each time. After exactly φ(n)\varphi(n) steps you always land on 11; one more step brings you back to mm itself.

RSA chooses ee and dd so that e⋅d=1+kφ(n)e \cdot d = 1 + k\varphi(n), so "going ee times, then dd times" means circling kk full laps plus one more step, landing exactly on mm. Outsiders don't know how long a lap is (φ(n)\varphi(n)), so they don't know how many steps lead back.


Layer 2: Real-World Use

2.1 Encryption and signatures

HTTPS certificates, software-update signatures, PGP email and SSH logins relied mostly on RSA for decades. New systems increasingly use elliptic curves (see "Elliptic Curves"), because the keys are far shorter for the same security:

Security strength RSA modulus length Elliptic curve key length
112 bits 2048 bits 224 bits
128 bits 3072 bits 256 bits
256 bits 15360 bits 512 bits

2.2 Some classic failures

Failure What happened
No padding Textbook RSA is deterministic: the same plaintext always yields the same ciphertext, and a small plaintext space can simply be enumerated. Real systems must use OAEP padding
Håstad's broadcast attack With e=3e = 3, the same message is sent to 3 people. An attacker uses the CRT to combine the 3 ciphertexts into m3m^3 (an integer, with no reduction) and takes the cube root directly
Shared prime factors In 2012 researchers scanned certificates across the internet and found that many devices, because of poor randomness, shared some prime pp. Taking the gcd⁡\gcd of two values of nn factors both at once
ROCA (2017) One chip maker's way of generating primes had a special structure. It affected millions of keys (including about 750,000 Estonian ID cards), and those keys could be factored

The quantum threat

Shor's algorithm factors large integers efficiently on a quantum computer, which would break RSA completely. See "Post-Quantum Cryptography" for details.


Layer 3: The Mathematics

3.1 Key generation

  1. Choose two large primes p,qp, q and let n=pqn = pq;
  2. Compute φ(n)=(p−1)(q−1)\varphi(n) = (p-1)(q-1), the number of integers in {1,…,n}\{1, \dots, n\} coprime to nn;
  3. Choose ee with gcd⁡(e,φ(n))=1\gcd(e, \varphi(n)) = 1 (commonly e=65537e = 65537);
  4. Use the extended Euclidean algorithm to find d≡e−1(modφ(n))d \equiv e^{-1} \pmod{\varphi(n)}.

The public key is (n,e)(n, e) and the private key is dd.

3.2 Euler's theorem: a corollary of Lagrange's theorem

Zn×\mathbb{Z}_n^\times (the invertible elements modulo nn) is a finite group under multiplication, of order φ(n)\varphi(n). By Lagrange's theorem, the order of every element divides the order of the group, so

aφ(n)≡1(modn),gcd⁡(a,n)=1.a^{\varphi(n)} \equiv 1 \pmod n,\qquad \gcd(a, n) = 1.

This is Euler's theorem; when n=pn = p is prime it is Fermat's little theorem, ap−1≡1a^{p-1} \equiv 1.

3.3 Why decryption is correct

From ed=1+kφ(n)ed = 1 + k\varphi(n),

cd=(me)d=m1+kφ(n)=m⋅(mφ(n))k≡m⋅1k=m(modn).c^d = (m^e)^d = m^{1 + k\varphi(n)} = m \cdot \big(m^{\varphi(n)}\big)^k \equiv m \cdot 1^k = m \pmod n.

What if mm and nn aren't coprime?

Then mm is not in the unit group, and Euler's theorem doesn't apply directly. But we can look modulo pp and modulo qq separately: say p∣mp \mid m, then both sides are 00 modulo pp; modulo qq, Fermat's little theorem works as usual. Combining them with the Chinese Remainder Theorem still gives med≡m(modn)m^{ed} \equiv m \pmod n. (In practice you will almost never meet such an mm, and if you did, you would have factored nn.)

3.4 The CRT speedup

By the Chinese Remainder Theorem, Zn≅Zp×Zq\mathbb{Z}_n \cong \mathbb{Z}_p \times \mathbb{Z}_q is a ring isomorphism. So cd mod nc^d \bmod n can be split into two halves:

mp=c d mod (p−1) mod p,mq=c d mod (q−1) mod q,m_p = c^{\,d \bmod (p-1)} \bmod p,\qquad m_q = c^{\,d \bmod (q-1)} \bmod q,

and (mp,mq)(m_p, m_q) is then combined into the unique solution modulo nn. Reducing the exponents modulo p−1p-1 first is also thanks to Fermat's little theorem. The modulus has half as many bits, making multiplication about 4 times cheaper, and the exponent has half as many bits too; overall it is 3 to 4 times faster.

Fault attacks

If a CRT implementation makes an error in just one half (say, disturbed by a voltage glitch), the faulty signature s′s' satisfies s′≡s(modp)s' \equiv s \pmod p but is wrong modulo qq, so gcd⁡(s′e−h, n)=p\gcd(s'^e - h,\ n) = p, and a single signature factors nn (the Boneh–DeMillo–Lipton attack). So a signature should be verified locally before it is sent out.

3.5 A worked example

A tiny RSA

Take p=5p = 5, q=11q = 11, n=55n = 55, φ(n)=40\varphi(n) = 40. Take e=3e = 3; from 3×27=81≡1(mod40)3 \times 27 = 81 \equiv 1 \pmod{40} we get d=27d = 27.

Encrypt m=7m = 7: 73=343=6×55+137^3 = 343 = 6 \times 55 + 13, so c=13c = 13.

Decrypt directly: 1327 mod 55=713^{27} \bmod 55 = 7.

Decrypt with the CRT:

  • Modulo 55: 13≡313 \equiv 3, 27 mod 4=327 \bmod 4 = 3, 33=27≡23^3 = 27 \equiv 2, so m≡2(mod5)m \equiv 2 \pmod 5
  • Modulo 1111: 13≡213 \equiv 2, 27 mod 10=727 \bmod 10 = 7, 27=128≡72^7 = 128 \equiv 7, so m≡7(mod11)m \equiv 7 \pmod{11}
  • Combining: the only value from 00 to 5454 with both m≡2(mod5)m \equiv 2 \pmod 5 and m≡7(mod11)m \equiv 7 \pmod{11} is 77

Both methods give 77. (Checked with Python.)


Summary

Layer Key points
Intuition Easy to multiply, hard to take apart; knowing p,qp, q is the trapdoor
Algebra Zn×\mathbb{Z}_n^\times has order φ(n)\varphi(n); Lagrange's theorem gives Euler's theorem; ed≡1(modφ(n))ed \equiv 1 \pmod{\varphi(n)} guarantees correct decryption
Engineering The CRT speeds things up 3 to 4 times; padding is mandatory; verify signatures before sending
Future Will be broken by Shor's algorithm; migration to post-quantum algorithms is under way