An RSA calculator makes a toy RSA key pair from two primes, encrypts and decrypts numbers, and shows how a small key can be broken by factoring. Enter two primes, and it finds the modulus n, the totient λ(n), the public exponent e and the private exponent d. Then you can encrypt a number with the public key and decrypt it with the private key. It uses exact big-number arithmetic and runs in your browser. It is meant for learning, not for protecting data.
How to use the RSA calculator
- Choose Make a key pair, Encrypt, Decrypt or Break a small key.
- Enter the primes, or the message, the ciphertext and the key numbers.
- Press Calculate and read the result.
How RSA keys are made
The Wikipedia article on the RSA cryptosystem lists the steps of key generation. Choose two distinct large prime numbers p and q. Compute n = pq. Compute λ(n), Carmichael's totient function, which is lcm(p − 1, q − 1), although the original RSA paper used φ(n) = (p − 1)(q − 1). Choose an integer e with 1 < e < λ(n) that shares no factor with λ(n), and the page says the most commonly chosen value is 2¹⁶ + 1 = 65,537. Then find d as the modular inverse of e modulo λ(n).[1] The public key is (n, e) and the private key is (n, d). The calculator does each step and shows the numbers. If you leave e blank, it picks the first of the common choices 65,537, 257, 17 and so on that fits.
Encrypting and decrypting
The page gives the encryption of a message m as c ≡ mᵉ (mod n) using the public key, and decryption as cᵈ ≡ (mᵉ)ᵈ ≡ m (mod n).[1] The message, as a number, must satisfy 0 ≤ m < n.[1] The calculator uses fast modular exponentiation, so that even large numbers are quick. It checks that the message is below n, and tells you if it is not.
The worked example from the article
The page gives a small example, ignoring padding. The primes are p = 61 and q = 53, so n = 3,233. Then λ(3233) = lcm(60, 52) = 780. The public exponent is e = 17, which has no common factor with 780, and the private exponent is d = 413, because 17 × 413 leaves a remainder of 1 when divided by 780. The public key is (3233, 17) and the private key is (3233, 413). Encrypting the message m = 65 gives c = 65¹⁷ mod 3233 = 2790, and decrypting 2790 with d gives back 65.[1] Enter these numbers in the key, encrypt and decrypt modes and you get the same results. The page notes that such a toy key is broken by trivial factoring of 3233.[1]
Why small keys are weak
The security of RSA rests on the fact that factoring n back into p and q is hard for large numbers. The break mode factors a small n by trial division, so you can see how quick it is for 3233, which splits into 53 × 61 at once. Once you have p and q, the private key follows from e. The article's infobox says that key sizes are typically 2,048 to 4,096 bits, and that a key of 896 bits has been broken.[1] A number with 14 digits is less than 47 bits, which is why the calculator can break it in a moment.
What this calculator does not do
Real RSA does not encrypt plain numbers or text directly. The page says that the plaintext m used in the formulas is the padded message, and it notes that a small e, such as 3, may expose vulnerabilities in insecure padding schemes, and that there are at least nine values of m that give a ciphertext equal to m.[1] Real systems use padding such as OAEP, large random primes and well-tested libraries. Use this calculator to understand the idea, to check a homework problem or to see what d is for a given pair of primes.
A step-by-step example of making keys
Take the primes p = 17 and q = 11. Then n = 187 and λ(n) = lcm(16, 10) = 80. Choose e = 7, which has no common factor with 80. The private exponent d is the inverse of 7 modulo 80, which is 23, because 7 × 23 = 161 = 2 × 80 + 1. The public key is (187, 7) and the private key is (187, 23). Encrypting m = 88 gives 88⁷ mod 187 = 11, and decrypting 11 with d = 23 gives 88 back. Use the key pair mode with these primes to see the numbers, and then try the encrypt and decrypt modes.
The role of the totient
Euler's totient φ(n) = (p − 1)(q − 1) and Carmichael's λ(n) = lcm(p − 1, q − 1) both work for finding d, because both are multiples of the order of every number modulo n. The calculator shows both, since textbooks differ. With λ(n), the private exponent is usually a bit smaller, and the two values of d give the same decryption. For p = 61 and q = 53, φ = 3,120 and λ = 780, and e = 17 gives d = 413 with λ and d = 2,753 with φ. Both decrypt correctly.
Common mistakes
- Using primes that are not prime. RSA needs two primes, and the calculator checks them.
- Using an e that shares a factor with λ(n). Then d does not exist.
- Choosing a message larger than n. Split it into smaller numbers.
- Using a toy key for real data. It is easy to break.
What next
Use this often? Add RSA to your Home screen.
Frequently asked questions
What is an RSA calculator?
A tool that makes an RSA key pair from two primes and encrypts and decrypts numbers with it. It is for learning, not for real security.
How are RSA keys generated?
Choose two primes p and q, compute n = pq and λ(n), choose e coprime to λ(n), and find d as the inverse of e modulo λ(n).
What is the article's example?
p = 61, q = 53, n = 3233, e = 17 and d = 413. The message 65 encrypts to 2790 and decrypts back to 65.
Is a key made with this calculator secure?
No. The keys are small and there is no padding. Real RSA needs keys of 2,048 bits or more and a padding scheme.
Why can the calculator break small keys?
Factoring a small n into two primes is quick. For numbers with hundreds of digits it is believed to be very hard.
Sources
Was this tool helpful?
What went wrong?
We only receive the reason you pick, never what you typed into the tool.
Thanks for your feedback.