RSA Playground

Generate RSA keys from small primes, encrypt and decrypt with modular exponentiation, then factor n to see why key size matters. Interactive, in-browser.

Advertisement

RSA Playground: Learn Public-Key Cryptography Step by Step

This interactive RSA playground lets you generate a key pair from small primes, encrypt and decrypt numbers with modular exponentiation, and then break the key by factoring the modulus — so you can see, first-hand, why real RSA uses enormous primes. It shows every intermediate value: the primes p and q, the modulus n, the totient, the public and private exponents, and each step of encryption and decryption. Everything runs in your browser. The key sizes here are deliberately tiny for readability, which means they are for learning only and must never be used to protect real data.

RSA is the classic introduction to public-key cryptography because its security rests on one asymmetry you can watch happen: multiplying two primes is easy, but factoring their product back into those primes is hard. This tool makes that asymmetry concrete by letting you do both.

How RSA Key Generation Works

The playground follows the standard RSA construction, and shows each value as it is computed:

  1. Choose two primes p and q. Presets range from 8-bit toys (p=11, q=13) up to 16-bit demonstrations, each labelled with how weak it is.
  2. Compute the modulus n = p × q. This becomes part of both the public and private keys and is the number an attacker must factor.
  3. Compute the totient φ(n) = (p − 1)(q − 1), the count of numbers below n that are coprime to it.
  4. Choose a public exponent e that is coprime to φ(n). Common choices are 3, 17 and 65537; the tool offers these and explains the trade-off.
  5. Compute the private exponent d, the modular inverse of e modulo φ(n) — the number such that e × d ≡ 1 (mod φ(n)), found with the extended Euclidean algorithm.

The public key is the pair (n, e) and can be shared with anyone. The private key is (n, d) and must be kept secret. The whole scheme hinges on the fact that computing d requires φ(n), and computing φ(n) requires knowing p and q — which is why factoring n breaks everything.

Encryption, Decryption and Modular Exponentiation

To encrypt a message m (a number less than n), RSA computes the ciphertext c = me mod n. To decrypt, it computes m = cd mod n. Both are modular exponentiation: raise a number to a power, keeping only the remainder modulo n at each step. The playground uses fast square-and-multiply exponentiation and can show the process. A worked example with the classic p=61, q=53 key (n=3233): with e=17, the message m=65 encrypts to c=6517 mod 3233 = 2790, and with the matching d, 2790d mod 3233 returns 65. The magic — that raising to e and then to d gets you back the original — is guaranteed by Euler's theorem, because e and d were chosen as inverses modulo φ(n).

Why Factoring Is Hard — and the Break Tab

The break tab factors n by trial division and reports how many iterations and how long it took. On an 8-bit key it finishes instantly; step up to the 14- and 16-bit presets and you can watch the effort climb steeply. That growth is the entire security argument. Multiplying p and q takes microseconds no matter how large they are, but the best known factoring algorithms take time that grows super-polynomially in the size of n. Real RSA uses keys of 2048 or 4096 bits — primes hundreds of digits long — where factoring is believed to be infeasible for any classical computer within the lifetime of the data. The tiny keys here factor in milliseconds precisely so you can see, rather than take on faith, why size is everything.

Toy Keys Are for Learning Only

To say it plainly: the keys this tool generates provide zero security. An 8-bit or 16-bit modulus is factored by this very page in a blink, and even the largest preset is trivially breakable. The playground exists to teach the mathematics — key generation, modular exponentiation, the role of the totient, and the factoring asymmetry — not to protect anything. For real cryptography, use a vetted library (such as your platform's standard crypto module or OpenSSL) with at least 2048-bit keys, proper padding (OAEP), and secure key storage. Never roll your own RSA for production, and never use short keys.

Frequently Asked Questions

What is the difference between the public and private key?

The public key (n, e) is shared and used to encrypt (or verify signatures). The private key (n, d) is secret and used to decrypt (or sign). They are mathematically linked through the totient of n.

Why is 65537 the common public exponent?

65537 is a prime of the form 216+1, which makes encryption fast (few bits set) while avoiding the security weaknesses that very small exponents like 3 can introduce with poor padding. The tool lets you compare 3, 17 and 65537.

What is modular exponentiation?

It is computing baseexponent mod n efficiently, reducing modulo n as you go so the numbers stay small. It is the core operation of both RSA encryption and decryption.

Why does factoring break RSA?

Because the private exponent d is derived from φ(n), and φ(n) can only be computed if you know the prime factors p and q of n. Factor n and you can reconstruct the private key.

Can I use these keys for real encryption?

No. The keys are far too small and are broken instantly. Use them to understand the maths, then use a real library with 2048-bit or larger keys for anything that matters.

What key size does real RSA use?

2048 bits is the common minimum today, with 3072 or 4096 bits for longer-term protection. The primes involved are hundreds of digits long, which is what makes factoring infeasible.

Does RSA encrypt large messages directly?

In practice, no. RSA encrypts a small value — typically a symmetric key or a padded hash — and the bulk data is encrypted with a fast symmetric cipher. This playground demonstrates the core operation on small numbers.

RSA Signatures: the Same Maths in Reverse

RSA is not only for encryption. The same key pair supports digital signatures, and understanding this deepens the intuition the playground builds. To sign, you apply the private key to a hash of the message — effectively signature = hashd mod n — and anyone can verify it by applying the public key: hash = signaturee mod n. It is encryption run with the keys swapped. Because only the holder of d could have produced a value that decrypts correctly with e, a valid signature proves the message came from the key owner and was not altered. This is why the same factoring hardness that protects encrypted messages also protects signatures: forging one without the private key means solving the same infeasible problem. In practice the message is hashed first (with SHA-256, for example) and padded, which is where the hash generator connects to RSA.

Common Pitfalls the Playground Reveals

Experimenting with small keys surfaces mistakes that are invisible at production scale. Pick a public exponent e that shares a factor with φ(n) and no valid d exists — the tool will show the modular inverse failing, which is exactly why e must be coprime to the totient. Try to “encrypt” a message value larger than n and it wraps around and cannot be recovered, demonstrating why the message must be smaller than the modulus and why real RSA encrypts a short key rather than bulk data. And factoring even a 16-bit modulus in a visible fraction of a second makes it viscerally clear that textbook RSA without adequate key size and padding is not security theatre you can skip. These are the same failure modes that, at 2048 bits and with proper padding, are engineered away — but seeing them in miniature is the fastest way to understand why those safeguards exist.

Related Cryptography Tools

Continue with the public-key concepts explainer for the bigger picture of asymmetric cryptography, contrast RSA with the classical Caesar cipher to appreciate how far ciphers have come, and use the hash generator to explore the digests that RSA signatures are computed over.

What Is RSA Encryption

RSA (Rivest-Shamir-Adleman) is the first widely adopted public key cryptosystem, published in 1977. It enables secure communication, digital signatures, and key exchange without requiring parties to share a secret key in advance. RSA's security is based on the computational difficulty of factoring the product of two large prime numbers — a problem that remains intractable for classical computers at sufficient key sizes.

RSA is used in TLS/SSL certificates, PGP email encryption, code signing, secure boot, and countless other security protocols. While newer elliptic curve algorithms offer equivalent security with smaller keys, RSA remains foundational to understanding public key cryptography and is still the most widely deployed asymmetric algorithm.

How RSA Works

RSA key generation, encryption, and decryption follow these mathematical steps:

Key Generation

  1. Choose two large random primes p and q (each 1024+ bits for RSA-2048)
  2. Compute n = p x q (the modulus, used in both public and private keys)
  3. Compute phi(n) = (p-1)(q-1) (Euler's totient)
  4. Choose public exponent e (commonly 65537) such that gcd(e, phi(n)) = 1
  5. Compute private exponent d such that e x d = 1 mod phi(n)
  6. Public key: (n, e) — Private key: (n, d)

Operations

OperationFormulaUsed For
Encryptc = m^e mod nConfidentiality — encrypting data with the recipient's public key
Decryptm = c^d mod nDecrypting data with your private key
Signs = hash(m)^d mod nDigital signatures — proving authorship with your private key
Verifyhash(m) = s^e mod nVerifying a signature with the signer's public key

Key Size and Security

RSA Key SizeEquivalent Symmetric StrengthStatus
1024-bit~80-bitDeprecated — factorable with sufficient resources
2048-bit~112-bitMinimum acceptable — adequate through ~2030
3072-bit~128-bitRecommended for new deployments
4096-bit~152-bitLong-term security, but slower operations

Common Use Cases

  • Learning cryptography: Experiment with small RSA key sizes to understand modular arithmetic, key generation, and the relationship between public and private keys
  • Understanding TLS certificates: See how RSA key pairs underpin the certificate chain that secures HTTPS connections
  • Digital signature exploration: Sign and verify messages to understand how code signing, document signing, and certificate validation work
  • Security assessment: Evaluate whether systems use adequate RSA key sizes and proper padding schemes (OAEP vs PKCS#1 v1.5)
  • Post-quantum planning: Understand why RSA will be broken by Shor's algorithm on quantum computers and why migration to post-quantum algorithms is necessary

Best Practices

  1. Minimum 2048-bit keys — NIST, ENISA, and major browsers require at least RSA-2048. Use RSA-3072 or RSA-4096 for certificates and keys that must remain secure beyond 2030.
  2. Use OAEP padding for encryption — RSA-OAEP (Optimal Asymmetric Encryption Padding) is the recommended padding scheme. Never use textbook RSA (no padding) or PKCS#1 v1.5 for new implementations.
  3. Use PSS padding for signatures — RSA-PSS (Probabilistic Signature Scheme) provides a security proof and is preferred over PKCS#1 v1.5 signatures.
  4. Never encrypt large data directly with RSA — RSA can only encrypt data smaller than the key size. Use hybrid encryption: encrypt data with AES, then encrypt the AES key with RSA.
  5. Prepare for post-quantum migration — Shor's algorithm will break RSA when large-scale quantum computers exist. NIST has standardized ML-KEM (Kyber) as a quantum-safe replacement. Begin evaluating hybrid RSA + post-quantum approaches.
This tool is provided for informational and educational purposes only. All processing happens in your browser — no data is sent to or stored on our servers. While we strive for accuracy, we make no warranties about the completeness or reliability of results.