By Mian Ali Khalid · Published · How we build and test tools
How to use the rsa calculator
- Enter two distinct prime numbers p and q.
- Choose a public exponent e that is coprime to
(p-1) * (q-1). - Enter an integer message smaller than
p * q. - The RSA calculator instantly computes the modulus, totient, private key, ciphertext and decrypted value.
How RSA key generation works
RSA relies on the difficulty of factoring the product of two large primes. The steps are straightforward for small numbers.
- Pick two distinct primes p and q.
- Compute the modulus
n = p * q. - Compute Euler's totient
phi(n) = (p - 1) * (q - 1). - Choose a public exponent e such that
1 < e < phi(n)andgcd(e, phi(n)) = 1. - Compute the private exponent d as the modular inverse of e modulo
phi(n), meaning(d * e) mod phi(n) = 1.
The public key is the pair (n, e) and the private key is (n, d). For example, with p = 61 and q = 53, we get n = 3233 and phi = 3120. Choosing e = 17, the extended Euclidean algorithm yields d = 2753 because 2753 * 17 = 46801 and 46801 mod 3120 = 1.
Encryption and decryption formulas
Once the keys exist, RSA transforms an integer message m into ciphertext c and back using modular exponentiation.
- Encrypt:
c = me mod n - Decrypt:
m = cd mod n
Using the previous example where n = 3233, e = 17 and d = 2753, encrypting m = 42 gives c = 4217 mod 3233 = 2557. Decrypting returns 25572753 mod 3233 = 42. The message integer must always be strictly less than n, otherwise the math wraps around and data is lost.
Reference table of small RSA parameters
This table shows valid parameter sets for tiny primes. These values are useful for manual verification but far too small for any real security.
| p | q | n | phi(n) | e | d |
|---|---|---|---|---|---|
| 3 | 11 | 33 | 20 | 3 | 7 |
| 5 | 7 | 35 | 24 | 5 | 5 |
| 5 | 11 | 55 | 40 | 3 | 27 |
| 7 | 11 | 77 | 60 | 7 | 43 |
| 11 | 13 | 143 | 120 | 7 | 103 |
You can type any of these rows into this RSA calculator to verify the results.
Computing the modular inverse
The most complex step in RSA key generation is finding d, the modular multiplicative inverse of e modulo phi(n). This is done using the Extended Euclidean Algorithm, which finds integers x and y such that e * x + phi(n) * y = gcd(e, phi(n)).
Because e and phi(n) are coprime, their greatest common divisor is 1. The algorithm therefore produces e * x + phi(n) * y = 1. Taking both sides modulo phi(n) leaves e * x = 1 mod phi(n), making x the desired inverse. If x is negative, adding phi(n) once makes it positive without changing its modular value.
Why real RSA uses much larger numbers
This RSA calculator is designed for education. Real-world RSA implementations use primes that are at least 1024 bits long, producing a modulus n of 2048 bits or more. Small primes like 61 and 53 can be factored instantly by trial division, completely breaking the system. For that reason this calculator accepts p, q and e only up to 9007199254740991 (2^53 - 1), and the primality check is exact for every number it accepts.
In production, RSA is rarely used to encrypt raw messages directly. Instead, it encrypts a symmetric key generated by tools similar to an AES encrypt decrypt workflow, or creates digital signatures verified through hashes like those from a SHA256 hash generator. Padding schemes such as OAEP add randomness to prevent identical messages from producing identical ciphertexts.
Common mistakes when learning RSA
Several pitfalls trip up students experimenting with RSA for the first time.
- Choosing non-prime numbers. If p or q is composite, the totient formula
(p-1)*(q-1)is wrong and decryption fails. - Using equal primes. Setting
p = qmeansn = p2, which is trivially factored by taking the square root. - Picking an invalid exponent. The public exponent e must share no factors with
phi(n). A common choice is 65537 because it is prime and has only two bits set, making exponentiation fast. - Messages larger than n. Any message
m >= nwill not decrypt correctly. Real protocols split data into blocks smaller than n.
Worked examples
| Example | Input | Result |
|---|---|---|
| Small textbook example | Prime p: 61; Prime q: 53; Public exponent e: 17; Message integer m: 42 | Modulus n: 3233; Totient phi(n): 3120; Public key: (3233, 17); Private exponent d: 2753; Private key: (3233, 2753); Ciphertext c: 2557; Decrypted message: 42 |
| Minimal primes | Prime p: 3; Prime q: 11; Public exponent e: 3; Message integer m: 9 | Modulus n: 33; Totient phi(n): 20; Public key: (33, 3); Private exponent d: 7; Private key: (33, 7); Ciphertext c: 3; Decrypted message: 9 |
| Larger primes | Prime p: 101; Prime q: 103; Public exponent e: 7; Message integer m: 1234 | Modulus n: 10403; Totient phi(n): 10200; Public key: (10403, 7); Private exponent d: 8743; Private key: (10403, 8743); Ciphertext c: 10378; Decrypted message: 1234 |
FAQ
What does this RSA calculator do?
It takes two prime numbers and a public exponent to generate an RSA key pair. It then encrypts and decrypts an integer message, showing every intermediate value including the modulus, totient and private exponent.
Can I use these keys for real encryption?
No. The primes used here are small enough to factor by hand. Real RSA requires primes of at least 1024 bits, secure random number generation and proper padding schemes like OAEP.
Why must p and q be prime?
Euler's totient function for a product of two primes is exactly (p-1) times (q-1). If either number is composite, that formula breaks down and the private key cannot be computed correctly.
What happens if my message is larger than n?
RSA operates modulo n, so any message greater than or equal to n wraps around and the original value cannot be recovered. You must split large data into blocks smaller than n.
How is the private exponent d calculated?
The value d is the modular multiplicative inverse of e modulo phi(n). It is found using the Extended Euclidean Algorithm, which solves the equation e times d plus phi(n) times y equals 1.
Is my data sent to a server?
No. All calculations run locally in your browser using JavaScript BigInt arithmetic. No inputs or outputs are transmitted anywhere.