Understanding the RSA key math calculator — concept and principle
RSA is the public-key system behind many digital signatures and key exchanges. This calculator runs textbook RSA with tiny primes so you can follow every step: building the modulus, finding the private exponent, encrypting a number and decrypting it again. It is meant for students and anyone who wants to see why RSA works.
How it is calculated
- Choose two primes p and q and form the modulus n = p × q.
- Compute Euler's totient φ(n) = (p − 1)(q − 1).
- Pick a public exponent e with gcd(e, φ(n)) = 1. The calculator stops if e is not coprime with φ(n), or if p or q is not prime.
- Find the private exponent d = e⁻¹ mod φ(n), the number with e × d ≡ 1 (mod φ(n)). The tool uses the extended Euclidean algorithm.
- Encrypt a message number m (0 ≤ m < n): c = mᵉ mod n. Decrypt: m = cᵈ mod n. Powers are computed by square-and-multiply with exact big integers.
The principle
Euler's theorem says m^φ(n) ≡ 1 (mod n) when m and n share no factor. Since e × d = 1 + t × φ(n) for some whole number t, raising c = mᵉ to the power d gives m^(1 + tφ(n)) = m × (m^φ(n))^t ≡ m. (With the Chinese remainder theorem the result also holds for every m below n.) The security rests on the fact that n and e are public but computing d needs φ(n), and finding φ(n) is as hard as factoring n. Factoring 3,233 is instant; factoring a 2,048-bit modulus is far beyond current computers.
Worked example
- Inputs: p = 61, q = 53, e = 17, m = 65.
- n = 61 × 53 = 3,233.
- φ(n) = 60 × 52 = 3,120. gcd(17, 3,120) = 1, so e is valid.
- d = 2,753, because 17 × 2,753 = 46,801 = 15 × 3,120 + 1.
- c = 65¹⁷ mod 3,233 = 2,790.
- 2,790^2,753 mod 3,233 = 65, the original message.
How to read the result
If decryption returns the original m, the key pair is consistent. Getting a different number usually means m ≥ n, which cannot be represented uniquely. Common real-world e is 65,537; real moduli are 2,048 bits or more.
Common mistakes
- Using a message number equal to or larger than n.
- Choosing an e that shares a factor with φ(n), so no d exists.
- Thinking textbook RSA is safe to use as is. Without padding it is deterministic and malleable: the same m always gives the same c.
Frequently asked questions
Why is e = 65,537 so common?
It is prime, has only two 1-bits in binary, so encryption and signature checks are fast, and it is large enough to avoid known small-exponent attacks when padding is used.
Can I use Carmichael's λ(n) instead of φ(n)?
Yes. Many libraries compute d modulo λ(n) = lcm(p − 1, q − 1), which gives a smaller but equally valid d.
How large should RSA keys be today?
Current guidance treats 2,048 bits as the minimum for general use, with 3,072 bits for longer-term protection.
Sources: R. L. Rivest, A. Shamir, L. Adleman, "A Method for Obtaining Digital Signatures and Public-Key Cryptosystems", Communications of the ACM 21(2), 1978 · IETF RFC 8017, PKCS #1: RSA Cryptography Specifications Version 2.2 · NIST SP 800-57 Part 1, Recommendation for Key Management