RSA on small numbers
A trapdoor made of two primes
RSA rests on a trapdoor: multiplying two large primes p and q is easy, while recovering them from the product n = p · q alone is, in practice, impossible. The public key is the pair (n, e); the private key is the exponent d, the inverse of e modulo λ(n) = lcm(p − 1, q − 1). You compute it with the extended Euclidean algorithm from the Arithmetic modulo p station — but only if you know p and q. Encryption is the power c = mᵉ mod n, decryption is cᵈ mod n. A signature is the same operation with the private key, checked with the public key; signatures as a topic belong to the Digital signatures station. The toy computes exactly these formulas, only on numbers you could factor in your head and without padding — as in a textbook, not as in real code.
Toy implementation — do not use in productionTextbook RSA without padding, on numbers you could factor in your head — unsafe outside learning. For real: RSA-OAEP and RSA-PSS from WebCrypto, with a key of at least 2048 bits.
RSA on small numbers
Type a whole number from 0 to 3,232 — the message must be smaller than n.
Step 10 of 10 · A signature is the same operation with the private key: s = mᵈ mod n = 588. Anyone checks it with the public key: sᵉ mod n = 65 = m.
- n = p · q
- 61 · 53 = 3,233
- λ(n) = lcm(p − 1, q − 1)
- 780
- φ(n) = (p − 1)(q − 1)
- 3,120
- d (private key)
- 413
- m (message)
- 65
- c (ciphertext)
- 2,790
- cᵈ mod n (decrypted)
- 65
- s = mᵈ mod n (signature)
- 588
Extended Euclid for 17⁻¹ mod 780: k — quotient, r — remainder, t — coefficient (t · e ≡ r).
| k | r | t |
|---|---|---|
| — | 780 | 0 |
| — | 17 | 1 |
| 45 | 15 | −45 |
| 1 | 2 | 46 |
| 7 | 1 | −367 |
Without padding
Deterministic: encrypt m = 65 again and you get the same c = 2,790. Whoever guesses the message checks the guess by encrypting it with the public key.
Malleable: c · 2ᵉ mod n = 3,017 decrypts to 130 = 2m mod n. The attacker changed the content knowing neither m nor d.
Below its result the toy shows two flaws of textbook RSA. It is deterministic: the same message always gives the same ciphertext, so whoever guesses the content can check the guess. It is also malleable: a ciphertext can be turned into the ciphertext of another message without the key. That is why RFC 8017 treats the bare exponentiation as a primitive that provides no security outside a scheme, and requires RSAES-OAEP encryption and RSASSA-PSS signatures for new applications. A real key has an n of at least 2048 bits and usually e = 65,537: FIPS 186-5 (Appendix A.1.1) requires 2¹⁶ < e < 2²⁵⁶, and 65,537 is the smallest odd number in that range. This is the same thing in WebCrypto:
Real RSA in your browser
WebCrypto will generate two RSA 2048 key pairs with e = 65,537 on your device: RSA-OAEP for encryption and RSA-PSS for signing. The private keys cannot be exported.
No keys yet.
In the toy the same message always gives the same ciphertext. OAEP adds a random seed to the message and checks the structure on decryption, and PSS adds a random salt to the signature: the ciphertext does not reveal repeats, and a changed one fails the check.
Weak randomness, common factors
Nobody can factor a 2048-bit n today. For scale: RSA-240, a 795-bit number, was factored in 2019 at a cost of about 900 core-years. But the gcd of two moduli takes a fraction of a millisecond — and if two keys share a prime p, gcd(n₁, n₂) = p factors both at once. This really happened: in 2012 Heninger, Durumeric, Wustrow and Halderman collected keys from across the internet and recovered the private keys of 0.50% of TLS hosts and 0.03% of SSH hosts — almost all of them routers, firewalls and server management cards that generated their key on first boot, before the system had gathered entropy. Independently, Lenstra’s team found that two out of every thousand RSA moduli they collected offer no security. The explanation given by Heninger et al.: the devices started with an identical generator state and drew the same p, and while drawing q OpenSSL mixed the current time in seconds into its pool, so q came out different. The toy recreates this mechanism in simplified form, with the MT19937 generator from the Randomness station — but the primes and keys are real, 2048-bit ones.
Toy implementation — do not use in productionA simplified model: MT19937 with 3 bits of boot state instead of an entropy pool. The primes are real (Miller–Rabin, 5 rounds), but generate real keys with WebCrypto once the system has gathered entropy.
A fleet of routers and common factors
12 routers of the same model generate RSA 2048 keys right after first power-on. The generator starts in one of 8 states and gets the clock second while drawing q. Your browser searches for the 1024-bit primes — Miller–Rabin, in chunks per frame.
The devices are waiting to be powered on.
The second attack waits in the “Break it” mode of the toy in the first section: Shor’s algorithm. It reduces factoring n to finding the period r of the sequence aˣ mod n. An even r gives y = a^(r/2) mod n with y² ≡ 1, and gcd(y − 1, n) and gcd(y + 1, n) are factors of n. A quantum computer finds the period in polynomial time; in the toy a plain search stands in for it, which works only for a small n. A 2025 estimate puts factoring a 2048-bit RSA key at under a week on a machine with less than a million noisy qubits (Gidney; assuming a 0.1% gate error rate and a 1 µs cycle time). The details belong to the Shor’s algorithm station.
Where RSA runs
- TLS 1.3An RSA certificate key signs the handshake only with RSA-PSS (
rsa_pss_*); the RSASSA-PKCS1-v1_5 code points refer only to signatures in certificates and are not defined for signed handshake messages.Source (TLS 1.3) - OpenSSHSince version 8.8 (September 2021) OpenSSH rejects RSA signatures with SHA-1 (
ssh-rsa) by default; the same RSA keys keep signing with SHA-256 or SHA-512 as in RFC 8332.Source (OpenSSH) - JWT and JOSEThe RS256 algorithm is RSASSA-PKCS1-v1_5 with SHA-256, and PS256 is RSASSA-PSS with SHA-256. RFC 7518 requires a key of at least 2048 bits for them.Source (JWT and JOSE)
- Taiwan citizen smart cards (2013)Bernstein and co-authors factored at least 184 RSA keys from certified smart cards issued under Taiwan’s “Citizen Digital Certificate”; 103 of them shared primes and fell to a batch-GCD computation.Source (Taiwan citizen smart cards (2013))
- NIST IR 8547Because of the quantum threat NIST will deprecate RSA, like ECDSA and ECDH, after 2030 and disallow it under its standards and guidelines after 2035, in favour of ML-KEM and post-quantum signatures.Source (NIST IR 8547)
RSA only with padding, only from a library
Encrypt with RSA-OAEP, sign with RSA-PSS — from WebCrypto or an audited library. Use a key of at least 2048 bits with e = 65,537, generated by the library from the system CSPRNG, with a non-extractable private key. OAEP holds at most k − 2 · hLen − 2 bytes (RFC 8017 §7.1.1) — 190 B at 2048 bits with SHA-256 — so encrypt longer data with AES-GCM and use RSA only for the key (the Envelope encryption station).
const { publicKey, privateKey } = await crypto.subtle.generateKey(
{ name: 'RSA-OAEP', modulusLength: 2048, publicExponent: new Uint8Array([1, 0, 1]), hash: 'SHA-256' },
false, // the private key cannot be exported
['encrypt', 'decrypt'],
);
// RSA only for a short secret, e.g. a 32-byte AES key (OAEP holds ≤ 190 B)
const c = await crypto.subtle.encrypt({ name: 'RSA-OAEP' }, publicKey, aesKeyBytes);Do not compute RSA yourself like the toy: without padding it is deterministic and malleable, and the timing of
exponentiation on BigInt gives away the bits of d. Do not encrypt with PKCS #1 v1.5 — RFC 8017 keeps it only
for compatibility with existing applications. Do not generate keys on a device’s first boot, before the system
has gathered entropy, or with a home-made prime generator.
// ✕ textbook RSA: same m → same c, and c · 2ᵉ decrypts to 2m
const c = modpow(m, e, n);
// ✕ a key from a generator seeded with the boot state
const p = nextPrime(mt19937(bootState));