Xerobit

RSA Calculator: Generate Keys, Encrypt and Decrypt

Enter two prime numbers to generate an RSA key pair, then encrypt or decrypt an integer message. This RSA calculator shows every mathematical step including the modulus, totient, and modular inverse. It runs entirely in your browser using BigInt arithmetic so nothing is uploaded anywhere.

Result

  • Modulus n
    3233

    p × q

  • Totient phi(n)
    3120

    (p−1) × (q−1)

  • Public key
    (3233, 17)

    (n, e)

  • Private exponent d
    2753

    e⁻¹ mod φ(n)

  • Private key
    (3233, 2753)

    (n, d)

  • Ciphertext c
    2557

    m^e mod n

  • Decrypted message
    42

    c^d mod n

By · Published · How we build and test tools

How to use the rsa calculator

  1. Enter two distinct prime numbers p and q.
  2. Choose a public exponent e that is coprime to (p-1) * (q-1).
  3. Enter an integer message smaller than p * q.
  4. 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.

  1. Pick two distinct primes p and q.
  2. Compute the modulus n = p * q.
  3. Compute Euler's totient phi(n) = (p - 1) * (q - 1).
  4. Choose a public exponent e such that 1 < e < phi(n) and gcd(e, phi(n)) = 1.
  5. 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.

pqnphi(n)ed
311332037
57352455
5115540327
7117760743
11131431207103

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 = q means n = 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 >= n will not decrypt correctly. Real protocols split data into blocks smaller than n.

Worked examples

ExampleInputResult
Small textbook examplePrime p: 61; Prime q: 53; Public exponent e: 17; Message integer m: 42Modulus 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 primesPrime p: 3; Prime q: 11; Public exponent e: 3; Message integer m: 9Modulus 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 primesPrime p: 101; Prime q: 103; Public exponent e: 7; Message integer m: 1234Modulus 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.