RSA Encryption and Decryption Calculator
Choose two primes p and q and a public exponent e to build an RSA key pair, then encrypt a number or decrypt a ciphertext, with every modular calculation shown.
How RSA works
RSA is a public-key cryptosystem: anyone can encrypt with the public key, but only the holder of the private key can decrypt. Its security rests on the fact that multiplying two large primes is easy while recovering them from their product is believed to be very hard.
Key generation: pick two distinct primes p and q and compute n = pq and φ(n) = (p − 1)(q − 1). Choose a public exponent e with 1 < e < φ(n) and gcd(e, φ(n)) = 1. The private exponent d is the modular inverse of e modulo φ(n). The public key is (n, e) and the private key is d.
Encrypting and decrypting
A message is first encoded as an integer m with 0 ≤ m < n. Encryption computes c = me mod n and decryption computes m = cd mod n. Decryption works because ed = 1 + kφ(n) for some k, and by Euler's theorem med ≡ m (mod n). Both operations are modular exponentiations, done with the square-and-multiply method; the calculator shows the binary table for each.
The standard textbook example is p = 61, q = 53, e = 17. Then n = 3233, φ(n) = 3120 and d = 2753. The message m = 65 encrypts to c = 2790, and decrypting 2790 returns 65.
Why this is only a demonstration
Real RSA uses primes of 1024 bits or more and a padding scheme such as OAEP, because plain ("textbook") RSA is deterministic and has several known weaknesses. With the small primes used in lessons, n can be factored by hand in seconds, which immediately reveals the private key. Use this tool to understand the mathematics and to check homework, never to protect real information.
Choosing valid inputs
The calculator checks that p and q are prime and different, that e is coprime to φ(n), and that your message is smaller than n. If e is not coprime to φ(n), no private exponent exists and you will be asked to pick another e. The common choice in practice is e = 65537, which is prime, so it is coprime to φ(n) unless 65537 divides p − 1 or q − 1.
Worked examples
These examples are computed by the same engine as the calculator above, so you can compare each step with your own working.
Textbook: encrypt 65
- Prime p:
61 - Prime q:
53 - Public exponent e:
17 - Operation:
Encrypt a message m - Message m / ciphertext c:
65
Public key (n, e)
(3233, 17)
Private exponent d
2753
φ(n) = 3120
Teaching demonstration only. Textbook RSA without padding and with small primes is insecure; never use it to protect real data.
Step-by-step solution
Formula used
n = p·q, φ(n) = (p − 1)(q − 1), e·d ≡ 1 (mod φ(n))
Encrypt: c = m^e mod n. Decrypt: m = c^d mod n.
CalculateModulus n = p × q
n = p·q
n = 61 × 53 = 3233
CalculateEuler totient φ(n)
φ(n) = (p − 1)(q − 1)
φ(n) = 60 × 52 = 3120
CalculateCheck that e is coprime to φ(n)
gcd(17, 3120) = 1 ✓
CalculatePrivate exponent d = e⁻¹ mod φ(n)
e·d ≡ 1 (mod φ(n)) (extended Euclidean algorithm)
17·(-367) + 3120·(2) = 1
d = -367 mod 3120 = 2753
Check: 17 × 2753 = 46801 = 15×3120 + 1 ✓
| i | q | r | s | t |
|---|
| 0 | — | 17 | 1 | 0 |
| 1 | — | 3120 | 0 | 1 |
| 2 | 0 | 17 | 1 | 0 |
| 3 | 183 | 9 | -183 | 1 |
| 4 | 1 | 8 | 184 | -1 |
| 5 | 1 | 1 | -367 | 2 |
| 6 | 8 | 0 | 3120 | -17 |
NoteKeys
Public key (n, e) = (3233, 17)
Private key (n, d) = (3233, 2753)
CalculateEncrypt: c = m^e mod n = 65^17 mod 3233
square-and-multiply
Exponent 17 in binary: 10001
| Position | Bit | Square | Multiply | r |
|---|
| 4 | 1 | 1 | 65 | 65 |
| 3 | 0 | 992 | — | 992 |
| 2 | 0 | 1232 | — | 1232 |
| 1 | 0 | 1547 | — | 1547 |
| 0 | 1 | 789 | 2790 | 2790 |
ResultCiphertext
c = 2790
CheckVerify by decrypting
c^d mod n = 2790^2753 mod 3233 = 65 = m ✓
Textbook: decrypt 2790
- Prime p:
61 - Prime q:
53 - Public exponent e:
17 - Operation:
Decrypt a ciphertext c - Message m / ciphertext c:
2790
Public key (n, e)
(3233, 17)
Private exponent d
2753
φ(n) = 3120
Teaching demonstration only. Textbook RSA without padding and with small primes is insecure; never use it to protect real data.
Step-by-step solution
Formula used
n = p·q, φ(n) = (p − 1)(q − 1), e·d ≡ 1 (mod φ(n))
Encrypt: c = m^e mod n. Decrypt: m = c^d mod n.
CalculateModulus n = p × q
n = p·q
n = 61 × 53 = 3233
CalculateEuler totient φ(n)
φ(n) = (p − 1)(q − 1)
φ(n) = 60 × 52 = 3120
CalculateCheck that e is coprime to φ(n)
gcd(17, 3120) = 1 ✓
CalculatePrivate exponent d = e⁻¹ mod φ(n)
e·d ≡ 1 (mod φ(n)) (extended Euclidean algorithm)
17·(-367) + 3120·(2) = 1
d = -367 mod 3120 = 2753
Check: 17 × 2753 = 46801 = 15×3120 + 1 ✓
| i | q | r | s | t |
|---|
| 0 | — | 17 | 1 | 0 |
| 1 | — | 3120 | 0 | 1 |
| 2 | 0 | 17 | 1 | 0 |
| 3 | 183 | 9 | -183 | 1 |
| 4 | 1 | 8 | 184 | -1 |
| 5 | 1 | 1 | -367 | 2 |
| 6 | 8 | 0 | 3120 | -17 |
NoteKeys
Public key (n, e) = (3233, 17)
Private key (n, d) = (3233, 2753)
CalculateDecrypt: m = c^d mod n = 2790^2753 mod 3233
square-and-multiply
Exponent 2753 in binary: 101011000001
| Position | Bit | Square | Multiply | r |
|---|
| 11 | 1 | 1 | 2790 | 2790 |
| 10 | 0 | 2269 | — | 2269 |
| 9 | 1 | 1425 | 2393 | 2393 |
| 8 | 0 | 806 | — | 806 |
| 7 | 1 | 3036 | 3213 | 3213 |
| 6 | 1 | 400 | 615 | 615 |
| 5 | 0 | 3197 | — | 3197 |
| 4 | 0 | 1296 | — | 1296 |
| 3 | 0 | 1689 | — | 1689 |
| 2 | 0 | 1215 | — | 1215 |
| 1 | 0 | 1977 | — | 1977 |
| 0 | 1 | 3065 | 65 | 65 |
ResultPlaintext
m = 65
CheckVerify by encrypting
m^e mod n = 65^17 mod 3233 = 2790 = c ✓
p=11, q=13, e=7
- Prime p:
11 - Prime q:
13 - Public exponent e:
7 - Operation:
Encrypt a message m - Message m / ciphertext c:
9
Public key (n, e)
(143, 7)
Private exponent d
103
φ(n) = 120
Teaching demonstration only. Textbook RSA without padding and with small primes is insecure; never use it to protect real data.
Step-by-step solution
Formula used
n = p·q, φ(n) = (p − 1)(q − 1), e·d ≡ 1 (mod φ(n))
Encrypt: c = m^e mod n. Decrypt: m = c^d mod n.
CalculateModulus n = p × q
n = p·q
n = 11 × 13 = 143
CalculateEuler totient φ(n)
φ(n) = (p − 1)(q − 1)
φ(n) = 10 × 12 = 120
CalculateCheck that e is coprime to φ(n)
gcd(7, 120) = 1 ✓
CalculatePrivate exponent d = e⁻¹ mod φ(n)
e·d ≡ 1 (mod φ(n)) (extended Euclidean algorithm)
7·(-17) + 120·(1) = 1
d = -17 mod 120 = 103
Check: 7 × 103 = 721 = 6×120 + 1 ✓
| i | q | r | s | t |
|---|
| 0 | — | 7 | 1 | 0 |
| 1 | — | 120 | 0 | 1 |
| 2 | 0 | 7 | 1 | 0 |
| 3 | 17 | 1 | -17 | 1 |
| 4 | 7 | 0 | 120 | -7 |
NoteKeys
Public key (n, e) = (143, 7)
Private key (n, d) = (143, 103)
CalculateEncrypt: c = m^e mod n = 9^7 mod 143
square-and-multiply
Exponent 7 in binary: 111
| Position | Bit | Square | Multiply | r |
|---|
| 2 | 1 | 1 | 9 | 9 |
| 1 | 1 | 81 | 14 | 14 |
| 0 | 1 | 53 | 48 | 48 |
ResultCiphertext
c = 48
CheckVerify by decrypting
c^d mod n = 48^103 mod 143 = 9 = m ✓
Larger primes
- Prime p:
1000003 - Prime q:
1000033 - Public exponent e:
65537 - Operation:
Encrypt a message m - Message m / ciphertext c:
123456789
Public key (n, e)
(1000036000099, 65537)
Private exponent d
983264276609
φ(n) = 1000034000064
Teaching demonstration only. Textbook RSA without padding and with small primes is insecure; never use it to protect real data.
Step-by-step solution
Formula used
n = p·q, φ(n) = (p − 1)(q − 1), e·d ≡ 1 (mod φ(n))
Encrypt: c = m^e mod n. Decrypt: m = c^d mod n.
CalculateModulus n = p × q
n = p·q
n = 1000003 × 1000033 = 1000036000099
CalculateEuler totient φ(n)
φ(n) = (p − 1)(q − 1)
φ(n) = 1000002 × 1000032 = 1000034000064
CalculateCheck that e is coprime to φ(n)
gcd(65537, 1000034000064) = 1 ✓
CalculatePrivate exponent d = e⁻¹ mod φ(n)
e·d ≡ 1 (mod φ(n)) (extended Euclidean algorithm)
65537·(-16769723455) + 1000034000064·(1099) = 1
d = -16769723455 mod 1000034000064 = 983264276609
Check: 65537 × 983264276609 = 64440190896124033 = 64438×1000034000064 + 1 ✓
| i | q | r | s | t |
|---|
| 0 | — | 65537 | 1 | 0 |
| 1 | — | 1000034000064 | 0 | 1 |
| 2 | 0 | 65537 | 1 | 0 |
| 3 | 15259075 | 1789 | -15259075 | 1 |
| 4 | 36 | 1133 | 549326701 | -36 |
| 5 | 1 | 656 | -564585776 | 37 |
| 6 | 1 | 477 | 1113912477 | -73 |
| 7 | 1 | 179 | -1678498253 | 110 |
| 8 | 2 | 119 | 4470908983 | -293 |
| 9 | 1 | 60 | -6149407236 | 403 |
| 10 | 1 | 59 | 10620316219 | -696 |
| 11 | 1 | 1 | -16769723455 | 1099 |
| 12 | 59 | 0 | 1000034000064 | -65537 |
NoteKeys
Public key (n, e) = (1000036000099, 65537)
Private key (n, d) = (1000036000099, 983264276609)
CalculateEncrypt: c = m^e mod n = 123456789^65537 mod 1000036000099
square-and-multiply
Exponent 65537 in binary: 10000000000000001
| Position | Bit | Square | Multiply | r |
|---|
| 16 | 1 | 1 | 123456789 | 123456789 |
| 15 | 0 | 30072681662 | — | 30072681662 |
| 14 | 0 | 243926053270 | — | 243926053270 |
| 13 | 0 | 997265716737 | — | 997265716737 |
| 12 | 0 | 634066277937 | — | 634066277937 |
| 11 | 0 | 493585362225 | — | 493585362225 |
| 10 | 0 | 42860736165 | — | 42860736165 |
| 9 | 0 | 267250226498 | — | 267250226498 |
| 8 | 0 | 114158213236 | — | 114158213236 |
| 7 | 0 | 319590369305 | — | 319590369305 |
| 6 | 0 | 611971779731 | — | 611971779731 |
| 5 | 0 | 869896680255 | — | 869896680255 |
| 4 | 0 | 998480528820 | — | 998480528820 |
| 3 | 0 | 801046374944 | — | 801046374944 |
| 2 | 0 | 370215671604 | — | 370215671604 |
| 1 | 0 | 89707690534 | — | 89707690534 |
| 0 | 1 | 665766381097 | 771129065953 | 771129065953 |
ResultCiphertext
c = 771129065953
CheckVerify by decrypting
c^d mod n = 771129065953^983264276609 mod 1000036000099 = 123456789 = m ✓
Frequently asked questions
How do I calculate d in RSA?
d is the modular inverse of e modulo φ(n), found with the extended Euclidean algorithm. For e = 17 and φ(n) = 3120 it is d = 2753, because 17 × 2753 = 46801 = 15 × 3120 + 1.
Why must the message be smaller than n?
RSA works modulo n, so m and m + n are indistinguishable. Longer messages must be split into blocks or, in practice, encrypted with a symmetric key that RSA then protects.
Can I use this to encrypt real messages?
No. Textbook RSA with small numbers and no padding is insecure. The tool is for learning and for checking coursework.
What if p and q are not prime?
Then φ(n) is not (p − 1)(q − 1) and decryption may fail. The calculator rejects non-prime inputs with a clear message.