RSA Calculator
Calculate RSA keys step-by-step from primes p and q, find modular inverses, and encrypt or decrypt numbers and text messages.
What Is RSA Encryption?
RSA (Rivest–Shamir–Adleman) is an asymmetric cryptographic algorithm widely used for secure data transmission, digital signatures, and key exchange. Unlike symmetric encryption systems where the same secret key is used to lock and unlock messages, RSA uses a mathematically linked key pair:
- Public Key $(e, N)$: Shared openly with anyone who wants to encrypt a message or verify a digital signature.
- Private Key $(d, N)$: Kept secret by the recipient to decrypt incoming ciphertexts or sign digital documents.
The security of RSA rests on the mathematical hardness of integer factorization: multiplying two large prime numbers is computationally trivial, but finding the original prime factors from their product is computationally infeasible for modern supercomputers when sufficiently large keys are used.
How RSA Key Generation Works Step-by-Step
The RSA key creation pipeline involves five core mathematical steps:
- Select Two Distinct Primes: Choose two prime numbers $p$ and $q$. In real-world cryptographic applications, these primes are hundreds of digits long (e.g. 1024 or 2048 bits).
- Calculate the Modulus $N$: Multiply the two primes together: $$N = p \times q$$ The value $N$ serves as the modulus for both the public and private keys. Its bit length represents the key size.
- Compute the Totient: Calculate Euler's totient function $\phi(N)$ or Carmichael's totient function $\lambda(N)$: $$\phi(N) = (p - 1)(q - 1)$$ $$\lambda(N) = \text{lcm}(p - 1, q - 1)$$
- Choose Public Exponent $e$: Pick an integer $e$ such that $1 < e < \phi(N)$ and $\gcd(e, \phi(N)) = 1$. The exponent $e$ must be coprime with the totient. Common choices in cryptographic standards include 3, 17, and 65537 ($2^{16} + 1$).
- Calculate Private Exponent $d$: Solve for the modular multiplicative inverse of $e$ modulo $\lambda(N)$ (or modulo $\phi(N)$): $$d \equiv e^{-1} \pmod{\lambda(N)} \implies e \times d \equiv 1 \pmod{\lambda(N)}$$ This is efficiently solved using the Extended Euclidean Algorithm. If you want to compute individual inverses, try our Modular Multiplicative Inverse Calculator.
Encryption and Decryption Formulas
Once the keys $(e, N)$ and $(d, N)$ are generated, numerical messages can be encrypted and decrypted:
- Encryption: Convert the plaintext message into an integer $m$ where $0 \le m < N$. The ciphertext $c$ is: $$c \equiv m^e \pmod{N}$$ You can verify exponentiation modular steps with our Power Modulo Calculator.
- Decryption: The recipient recovers the original plaintext message $m$ using the private exponent $d$: $$m \equiv c^d \pmod{N}$$
Worked Example
Let us trace an example using small primes:
- Choose primes: $p = 61$ and $q = 53$
- Modulus: $N = 61 \times 53 = 3233$
- Totient: $\phi(N) = (61 - 1) \times (53 - 1) = 60 \times 52 = 3120$
- Carmichael totient: $\lambda(N) = \text{lcm}(60, 52) = 780$
- Choose $e = 17$ (since $\gcd(17, 780) = 1$)
- Compute $d$: $17 \times d \equiv 1 \pmod{780} \implies d = 413$
To encrypt the plaintext number $m = 65$: $$c = 65^{17} \pmod{3233} = 2790$$ To decrypt ciphertext $c = 2790$: $$m = 2790^{413} \pmod{3233} = 65$$ The message is recovered accurately! If you need PEM-formatted key certificates for production development, explore our RSA Key Pair Generator or calculate general remainders with our Modulo Calculator.
Frequently Asked Questions
Why must p and q be prime numbers?
If $p$ or $q$ were composite, computing the totient $\phi(N)$ without knowing all individual prime factors would follow a different rule, and factoring $N$ would be significantly easier for attackers, breaking the security guarantees of the RSA cryptosystem.
What is the difference between Euler's totient and Carmichael's totient?
Euler's totient $\phi(N) = (p-1)(q-1)$ counts the numbers coprime to $N$. Carmichael's totient $\lambda(N) = \text{lcm}(p-1, q-1)$ provides the smallest exponent satisfying Euler's theorem for all units in $\mathbb{Z}/N\mathbb{Z}$. Both produce valid private keys $d$, but $\lambda(N)$ yields smaller private exponents, speeding up decryption.
Why is 65537 commonly chosen as the public exponent e?
The number $65537 = 2^{16} + 1$ is a Fermat prime ($F_4$). In binary, it has only two set bits (10000000000000001), allowing modular exponentiation to execute with just 17 multiplications, making public key operations exceptionally fast while resisting low-exponent attacks.
Can RSA encrypt messages of arbitrary length?
Direct textbook RSA can only encrypt numbers smaller than the modulus $N$. For larger text documents or files, practical systems use hybrid cryptosystems: RSA encrypts a random symmetric key (like AES-256), and AES encrypts the bulk data payload.