CipherMatrix

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
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.
  1. CalculateModulus n = p × q
    n = p·q
    n = 61 × 53 = 3233
  2. CalculateEuler totient φ(n)
    φ(n) = (p − 1)(q − 1)
    φ(n) = 60 × 52 = 3120
  3. CalculateCheck that e is coprime to φ(n)
    gcd(17, 3120) = 1 ✓
  4. 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 ✓
    iqrst
    0—1710
    1—312001
    201710
    31839-1831
    418184-1
    511-3672
    6803120-17
  5. NoteKeys
    Public key (n, e) = (3233, 17)
    Private key (n, d) = (3233, 2753)
  6. CalculateEncrypt: c = m^e mod n = 65^17 mod 3233
    square-and-multiply
    Exponent 17 in binary: 10001
    PositionBitSquareMultiplyr
    4116565
    30992—992
    201232—1232
    101547—1547
    0178927902790
  7. ResultCiphertext
    c = 2790
  8. 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
Plaintext m
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.
  1. CalculateModulus n = p × q
    n = p·q
    n = 61 × 53 = 3233
  2. CalculateEuler totient φ(n)
    φ(n) = (p − 1)(q − 1)
    φ(n) = 60 × 52 = 3120
  3. CalculateCheck that e is coprime to φ(n)
    gcd(17, 3120) = 1 ✓
  4. 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 ✓
    iqrst
    0—1710
    1—312001
    201710
    31839-1831
    418184-1
    511-3672
    6803120-17
  5. NoteKeys
    Public key (n, e) = (3233, 17)
    Private key (n, d) = (3233, 2753)
  6. CalculateDecrypt: m = c^d mod n = 2790^2753 mod 3233
    square-and-multiply
    Exponent 2753 in binary: 101011000001
    PositionBitSquareMultiplyr
    111127902790
    1002269—2269
    91142523932393
    80806—806
    71303632133213
    61400615615
    503197—3197
    401296—1296
    301689—1689
    201215—1215
    101977—1977
    0130656565
  7. ResultPlaintext
    m = 65
  8. 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
Ciphertext c
48
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.
  1. CalculateModulus n = p × q
    n = p·q
    n = 11 × 13 = 143
  2. CalculateEuler totient φ(n)
    φ(n) = (p − 1)(q − 1)
    φ(n) = 10 × 12 = 120
  3. CalculateCheck that e is coprime to φ(n)
    gcd(7, 120) = 1 ✓
  4. 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 ✓
    iqrst
    0—710
    1—12001
    20710
    3171-171
    470120-7
  5. NoteKeys
    Public key (n, e) = (143, 7)
    Private key (n, d) = (143, 103)
  6. CalculateEncrypt: c = m^e mod n = 9^7 mod 143
    square-and-multiply
    Exponent 7 in binary: 111
    PositionBitSquareMultiplyr
    21199
    11811414
    01534848
  7. ResultCiphertext
    c = 48
  8. 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
Ciphertext c
771129065953
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.
  1. CalculateModulus n = p × q
    n = p·q
    n = 1000003 × 1000033 = 1000036000099
  2. CalculateEuler totient φ(n)
    φ(n) = (p − 1)(q − 1)
    φ(n) = 1000002 × 1000032 = 1000034000064
  3. CalculateCheck that e is coprime to φ(n)
    gcd(65537, 1000034000064) = 1 ✓
  4. 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 ✓
    iqrst
    0—6553710
    1—100003400006401
    206553710
    3152590751789-152590751
    4361133549326701-36
    51656-56458577637
    614771113912477-73
    71179-1678498253110
    821194470908983-293
    9160-6149407236403
    1015910620316219-696
    1111-167697234551099
    125901000034000064-65537
  5. NoteKeys
    Public key (n, e) = (1000036000099, 65537)
    Private key (n, d) = (1000036000099, 983264276609)
  6. CalculateEncrypt: c = m^e mod n = 123456789^65537 mod 1000036000099
    square-and-multiply
    Exponent 65537 in binary: 10000000000000001
    PositionBitSquareMultiplyr
    1611123456789123456789
    15030072681662—30072681662
    140243926053270—243926053270
    130997265716737—997265716737
    120634066277937—634066277937
    110493585362225—493585362225
    10042860736165—42860736165
    90267250226498—267250226498
    80114158213236—114158213236
    70319590369305—319590369305
    60611971779731—611971779731
    50869896680255—869896680255
    40998480528820—998480528820
    30801046374944—801046374944
    20370215671604—370215671604
    1089707690534—89707690534
    01665766381097771129065953771129065953
  7. ResultCiphertext
    c = 771129065953
  8. 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.

Related calculators