RSA Mechanics

Key generation, encryption and the correctness proof, worked on small numbers — plus why textbook RSA is unusable, what padding actually does, and why new systems pick elliptic curves instead.

RSA (1977–78) was the first practical public-key cryptosystem and is still holding up a large share of the world's TLS certificates and code signatures. Its security rests on the presumed hardness of factoring a large semiprime. The mechanics fit on one page — which is why every course teaches it — but essentially every real-world RSA break has come not from factoring but from the gap between those clean equations and a safe implementation.

Why asymmetric crypto exists at all #

With symmetric ciphers, every pair of communicating parties needs its own shared key: MM users require (M2)≈M2/2\binom{M}{2} \approx M^2/2 keys, all distributed out of band, and one compromised machine exposes every conversation it participated in. Public-key crypto makes it linear — 2M2M keys, one keypair per user, public halves published freely.

The trade is speed. RSA is orders of magnitude slower than AES, so nobody encrypts bulk data with it: you encrypt a random session key with the recipient's public key, ship that alongside the AES-encrypted payload, and that hybrid pattern is what TLS, PGP, S/MIME and age all do.

Key generation and the algorithm #

  1. Pick two large random primes p,qp, q of equal bit length. Compute n=pqn = pq.
  2. Compute φ(n)=(p−1)(q−1)\varphi(n) = (p-1)(q-1).
  3. Choose a public exponent ee with gcd⁡(e,φ(n))=1\gcd(e, \varphi(n)) = 1.
  4. Compute d=e−1 mod φ(n)d = e^{-1} \bmod \varphi(n) with the extended Euclidean algorithm.
  5. Public key is (e,n)(e, n); private key is dd. Discard pp and qq — but never disclose them.

c=me mod n,m=cd mod nc = m^e \bmod n, \qquad m = c^d \bmod n

Correctness follows from Euler's theorem: since ed≡1(modφ(n))ed \equiv 1 \pmod{\varphi(n)}, write ed=kφ(n)+1ed = k\varphi(n) + 1, so

cd=med=mkφ(n)+1=(mφ(n))k⋅m≡1k⋅m=m(modn)c^d = m^{ed} = m^{k\varphi(n)+1} = \left(m^{\varphi(n)}\right)^k \cdot m \equiv 1^k \cdot m = m \pmod n

Textbook RSA is broken — padding is not optional #

The bare equation c=me mod nc = m^e \bmod n fails as an encryption scheme on multiple counts:

  • Deterministic. Same plaintext → same ciphertext. An attacker with a guessable message space (a yes/no vote, a credit card number, a session ID) just encrypts every candidate under the public key and matches. Semantic security requires randomness in every encryption.
  • Malleable. (m1m2)e=m1e⋅m2e mod n(m_1 m_2)^e = m_1^e \cdot m_2^e \bmod n — multiply a ciphertext by rer^e and the plaintext gets multiplied by rr, without the private key. This homomorphism is a feature in other contexts and a disaster here.
  • Small message + small exponent. If me<nm^e < n (e.g. e=3e = 3 and a short message), no modular reduction happens and m=cem = \sqrt[e]{c} over the integers. Håstad's broadcast attack generalizes it: the same message sent to ee recipients under ee different moduli is recoverable by CRT.

The fixes are the padding schemes in RFC 8017:

Scheme Purpose Status
PKCS #1 v1.5 encryption randomized padding legacy — Bleichenbacher-vulnerable (below)
OAEP randomized, chosen-ciphertext-secure encryption current for RSA encryption
PKCS #1 v1.5 signatures deterministic signature padding still widely deployed, acceptable when implemented carefully
PSS randomized signature padding with a security proof current preference

Failure modes that aren't factoring #

Nobody has factored a 2048-bit RSA modulus. The public record of RSA compromise is almost entirely about implementation:

  • Bad randomness in key generation. Heninger et al. (2012) scanned the internet and computed the private keys of 0.50% of TLS hosts — roughly 64,000 — because their moduli shared a prime factor with another host's. The cause was embedded devices generating keys at first boot from an unseeded entropy pool. Any two moduli sharing a factor are both broken by a single GCD, which costs nothing against a corpus you already have. This makes entropy a load-bearing dependency of RSA, not a detail.
  • Side channels. Timing and power analysis of modular exponentiation, and the CRT fault attack — a single bit-flip during one CRT half lets gcd⁡(m−se,n)\gcd(m - s^e, n) reveal pp. Constant-time implementations and signature verification-after-signing are mandatory countermeasures.
  • Shared or reused moduli, structurally weak primes from a flawed generator — ROCA (CVE-2017-15361, 2017), an Infineon key-generation defect that made 1024- and 2048-bit keys factorable from the public key alone, forcing Estonia to suspend and re-issue certificates for 760,000 national ID cards and affecting Infineon TPMs, Gemalto smartcards and YubiKeys besides.

Source claims audit #

Seeded from a university lab manual (RSA lab, following Applied Cryptography). Status of its load-bearing claims:

Claim from source Status Notes
Key generation steps, e⋅d≡1(mod(p−1)(q−1))e \cdot d \equiv 1 \pmod{(p-1)(q-1)}, encrypt/decrypt equations, correctness derivation current Matches the original paper
Worked example p=47,q=71,e=79,d=1019p=47, q=71, e=79, d=1019 and all six ciphertext blocks current Independently recomputed; all values confirmed
M2/2M^2/2 vs 2M2M key-count argument for asymmetric crypto current Standard motivation, still the clearest one
Hybrid encryption: session key under RSA, bulk under a symmetric cipher current Exactly what TLS/PGP do
"Primes of 100–200 digits or more" outdated 100 decimal digits ≈ 332 bits — far too small. Today: 1024-bit primes for a 2048-bit modulus, minimum
"Asymmetric cryptography was proposed only about 20 years ago" outdated Dates the text to ~1997; Diffie–Hellman 1976, RSA 1977–78
"Discard pp and qq" needs nuance Correct not to transmit them, but implementations retain them for CRT decryption — they must be protected, not literally destroyed
Message split into fixed blocks, zero-padded on the left outdated / dangerous This is textbook RSA. Real RSA encrypts one padded block (OAEP) — usually a symmetric key — never a raw block-chunked message
Source presents RSA with no padding at all — Added: determinism, malleability, Håstad, Bleichenbacher lineage
"Message can equally be encrypted with dd and decrypted with ee" needs correction Superficially true algebraically, but "encryption with the private key" is not signing — real signatures require PSS/PKCS#1 padding and hashing; see digital signatures

Sources

  1. paperA Method for Obtaining Digital Signatures and Public-Key Cryptosystems (Rivest, Shamir, Adleman, 1978)
  2. docsRFC 8017 — PKCS #1 v2.2: RSA Cryptography Specifications
  3. paperChosen Ciphertext Attacks Against Protocols Based on RSA Encryption Standard PKCS #1 (Bleichenbacher, 1998)
  4. paperMining Your Ps and Qs: Widespread Weak Keys in Network Devices (Heninger et al., USENIX Security 2012)
  5. docsNIST SP 800-57 Part 1 Rev. 5 — Recommendation for Key Management
  6. paperThe Return of Coppersmith's Attack: Practical Factorization of Widely Used RSA Moduli (ROCA)
  7. paperEverlasting ROBOT: the Marvin Attack (Kario, 2023)

Searches titles, aliases, headings, summaries, and full text.↑↓ move ↵ open esc close