Explore public-key cryptography hands-on. Generate real RSA keys, encrypt messages, and decrypt ciphertext — with every mathematical step explained.
RSA (Rivest–Shamir–Adleman) is a public-key cryptosystem invented in 1977. It relies on the practical difficulty of factoring the product of two large prime numbers — the "factoring problem".
RSA uses a key pair: a public key shared openly for encryption, and a private key kept secret for decryption. Anyone can encrypt a message to you; only you can decrypt it.
In real-world usage, key sizes of 2048–4096 bits are standard. This simulator uses small primes so every calculation step is human-readable.
1. Choose two distinct large primes p and q.
2. Compute Euler's totient:
3. Choose public exponent e such that 1 < e < φ(n) and gcd(e, φ(n)) = 1. Common choice: e = 65537.
4. Compute private exponent d as the modular inverse of e:
To encrypt message m (as an integer with 0 ≤ m < n) using the public key (e, n):
Fast exponentiation (square-and-multiply) makes this efficient even for huge exponents. The ciphertext c is the result.
For text, each character is first converted to its ASCII value, then encrypted individually.
To decrypt ciphertext c using the private key (d, n):
This works because of Euler's theorem: raising c to the power d modulo n reverses the encryption, recovering the original m.
RSA security relies on the integer factorization problem: given n, finding p and q is computationally infeasible for large enough n.
With a 2048-bit modulus, the best known algorithms would take longer than the age of the universe to factor n on current hardware.
Common weaknesses to avoid:
• Small primes (as used here) are insecure — easily factored.
• Padding schemes (OAEP) must be used in practice.
• Side-channel attacks target implementation, not the math.
• Quantum computers (Shor's algorithm) can break RSA — post-quantum cryptography is being standardised.
This RSA simulator was built and contributed to by the following talented students.