RSA Calculator

Enter two prime numbers p and q and a public exponent e to generate an RSA key pair — modulus n, Euler's totient φ(n), and private exponent d — plus optionally encrypt and decrypt a message.

Quick Facts

Modulus
n = p × q
p and q must be distinct primes; n is published as part of the public key.
Euler's totient
φ(n) = (p − 1)(q − 1)
Counts integers less than n that share no common factor with n.
Key condition
gcd(e, φ(n)) = 1
e must share no common factor with φ(n) so a modular inverse d exists.
Encrypt / Decrypt
C ≡ Me (mod n), M ≡ Cd (mod n)
d is the modular inverse of e mod φ(n), found with the extended Euclidean algorithm.

Your Results

Calculated
Modulus (n)
-
n = p × q, part of the public key
Euler's Totient φ(n)
-
φ(n) = (p − 1)(q − 1)
Private Exponent (d)
-
d = e⁻¹ mod φ(n) — keep secret
Encrypted Message (C)
-
C = Me mod n

Ready

Enter two primes and a public exponent, then press Calculate.

How RSA Key Generation and Encryption Work

RSA is a public-key cryptosystem built on the fact that multiplying two large prime numbers is fast, but factoring their product back into those primes is computationally hard. This calculator walks through the textbook RSA key-generation steps — choosing primes p and q, computing the modulus n and Euler's totient φ(n), and deriving the private exponent d from a public exponent e — and can optionally encrypt and decrypt a sample message so you can see the math work end to end.

Generating the key pair

Start with two distinct prime numbers, p and q (61 and 53, for example — the classic textbook values). Multiply them to get the modulus n = p × q = 3233. Compute Euler's totient φ(n) = (p − 1)(q − 1) = 3120, which counts how many integers between 1 and n share no common factor with n. Next choose a public exponent e with 1 < e < φ(n) and gcd(e, φ(n)) = 1; common real-world choices are 3, 17, or 65537, though any coprime value works for learning purposes (17 is used here). Finally, find the private exponent d — the modular multiplicative inverse of e modulo φ(n), i.e. the value satisfying e × d ≡ 1 (mod φ(n)). This calculator finds d with the extended Euclidean algorithm. The public key is the pair (n, e); the private key is the pair (n, d), and only d needs to stay secret.

Encrypting and decrypting a message

To encrypt a message M (represented as an integer smaller than n), compute the ciphertext C ≡ Me (mod n) using the public key. To decrypt, the holder of the private key computes M ≡ Cd (mod n), recovering the original message. This works because raising to the e-th power and then the d-th power modulo n is equivalent to raising to the e×d power, and e × d ≡ 1 (mod φ(n)) — a consequence of Euler's theorem — which returns M unchanged modulo n. This calculator performs both steps automatically when you supply a message, so you can confirm the recovered value matches your input.

Common mistakes when choosing p, q, and e

  • Reusing p or q, or setting p = q: n = p² is trivially factorable by taking a square root, which breaks the encryption entirely.
  • Picking e that shares a factor with φ(n): no modular inverse d exists, so a private key cannot be computed and decryption is impossible.
  • Encrypting a message M ≥ n: modular arithmetic wraps around, so decryption returns M mod n instead of your original message.
  • Using tiny textbook primes for real security: primes like 61 and 53 only illustrate the math — real RSA keys use primes hundreds of digits long.

Why RSA is considered secure

RSA's security rests on the integer factorization problem: given n, finding its prime factors p and q is believed to be computationally infeasible for large enough n (typically 2048 bits or more in modern practice), even though computing n from p and q is trivial. Anyone can use the public key (n, e) to encrypt, but only someone who knows the private exponent d — which requires knowing φ(n), which requires knowing p and q — can decrypt. This calculator uses small demonstration primes so results compute instantly and remain easy to verify by hand; it is a teaching and homework-checking tool, not a substitute for a vetted cryptography library.

Frequently Asked Questions

What is RSA encryption?
RSA (Rivest-Shamir-Adleman) is a public-key cryptosystem that uses a pair of mathematically linked keys: a public key (n, e) for encryption and a private key (n, d) for decryption. It relies on the difficulty of factoring the product of two large prime numbers.
How do I calculate the RSA modulus n and totient φ(n)?
Choose two distinct prime numbers p and q. The modulus is n = p × q, and Euler's totient is φ(n) = (p − 1)(q − 1). For the classic example p = 61 and q = 53, n = 3233 and φ(n) = 3120.
How is the private key d calculated from e?
d is the modular multiplicative inverse of the public exponent e modulo φ(n) — the value satisfying e × d ≡ 1 (mod φ(n)). It is found using the extended Euclidean algorithm, which only produces a valid d when gcd(e, φ(n)) = 1.
Why must p and q be prime and different from each other?
If p and q were not prime, φ(n) could not be computed as (p − 1)(q − 1). If p equals q, then n = p² is trivial to factor by taking a square root, which would let anyone recover the private key and break the encryption.