ScienceComputer Science

RSA
calculator.

Work through RSA with small primes: find d, then encrypt and decrypt a number.

Before you rely on this result. This tool is for learning with small numbers: its keys are far too small to protect anything, so never use one for real data. Sources used on this page.

How it's worked out

RSA builds a key pair from two primes, p and q. Multiply them to get the modulus n, which is public. Then work out how many numbers below n share no factor with it. For two primes that count, called Euler’s totient, is:

φ(n)\displaystyle \varphi(n)=(p−1)(q−1)\displaystyle {}= (p - 1)(q - 1)

Pick a public exponent e that shares no factor with φ(n). The private exponent d is the number that undoes e: multiply them and divide by φ(n), and the remainder is 1.

e×d\displaystyle e \times d≡1(modφ(n))\displaystyle {}\equiv 1 \pmod{\varphi(n)}

The extended Euclidean algorithm finds d. It divides φ(n) by e, then e by the remainder, and so on until the remainder is 1, keeping track of how many times e is inside each remainder. The tool shows that table for your numbers.

To encrypt a message number m, raise it to the power e and keep the remainder after dividing by n. To decrypt, raise the ciphertext c to the power d, again mod n. Both m and c are whole numbers from 0 to n − 1.

c\displaystyle c=memodn\displaystyle {}= m^{e} \bmod n
m\displaystyle m=cdmodn\displaystyle {}= c^{d} \bmod n

Nobody computes m to the power e in full. Square and multiply walks through the bits of e from the left: square the running result for every bit, multiply by m when the bit is 1, and reduce mod n each time so the numbers stay small. The current standard, RFC 8017, also allows d to be found with Carmichael’s λ(n), the least common multiple of p − 1 and q − 1, which gives a smaller d that works just as well.

This tool is for learning. Its primes are tiny, and it uses textbook RSA with no padding. Never use a key from it for anything real.

Worked examples

The textbook key with p = 61 and q = 53

A course exercise gives p = 61, q = 53 and e = 17. Then n = 3,233 and φ(n) = 60 × 52 = 3,120. The Euclidean table runs through the remainders 3,120, 17, 9, 8, 1, 0 and reaches 1 with −367 lots of 17. Adding 3,120 makes it positive, so d = 2,753.

Check: 17 × 2,753 = 46,801, which is 15 × 3,120 + 1. Encrypting m = 65 takes 5 square-and-multiply steps and gives c = 2,790. Raising 2,790 to the power 2,753 mod 3,233 returns 65. Using λ(n) = 780 instead gives the smaller d = 413, which decrypts the same message.

Open this example in the tool

Breaking a puzzle key with n = 3233

A puzzle publishes only the public key: n = 3233 and e = 17. Trial division tries 2, then odd numbers, and finds that 3233 = 53 × 61 after 27 tries. With the primes known, φ(n) follows, and so does d = 2,753, the same private exponent the key's owner holds.

That is the whole security of RSA in one line: anyone who can factor n can rebuild d. A real modulus is so large that no known method on ordinary computers can factor it in practice.

Open this example in the tool

Seven-digit primes and e = 65537

With p = 1,000,003, q = 1,000,033 and the common exponent 65537, the modulus is 1,000,036,000,099, a 40-bit number, and d = 983,264,276,609. The message 123,456,789 encrypts to 771,129,065,953 and decrypts back to 123,456,789.

The key looks big, but trial division still breaks it in 500,002 tries, well under a second in a browser. The smallest size NIST still rates as safe is 2,048 bits, more than fifty times as many bits as this key.

Open this example in the tool

Reading your result

The big number is d, the private exponent. Under it are the two keys: the public key (e, n) that anyone may have, and the private key (d, n) that only the owner keeps. The row below gives n, φ(n), the size of n in bits, and the ciphertext when you enter a message.

The key size table comes from NIST SP 800-57, Table 2. A 2,048-bit modulus gives about 112 bits of security and 3,072 bits gives 128. Anything below 2,048 bits is no longer approved for protecting data, and every key this tool makes is far below that line.

The smaller d from λ(n) is a second valid private exponent. Both satisfy the RFC 8017 rule that e × d leaves 1 when divided by λ(n). Course exercises usually want the φ(n) version, so check which one your notes use.

In break mode, the count of tries shows how fast trial division is for small numbers. It roughly doubles every time n grows by two bits, which sounds fast until n has 2048 bits.

Common mistakes

  • Choosing an e that shares a factor with φ(n). With p = 61 and q = 53, e = 12 has no inverse, because 12 and 3,120 are both even. The tool says so instead of giving a d.
  • A message as big as n or bigger. The answer comes back mod n, so 4000 with n = 3,233 would decrypt to a different number. The tool asks for a number from 0 to n − 1. Split a long message into blocks below n.
  • Expecting every message to be hidden. With textbook RSA, 0 encrypts to 0 and 1 encrypts to 1, and the same message always gives the same ciphertext. Real systems add random padding (OAEP) for this reason.
  • Using p = q. Then φ(n) is not (p − 1)(q − 1) and decryption fails; the tool asks for two different primes.
  • Computing m to the power e in full before reducing. 65 to the power 17 is already far larger than n = 3,233, and to the power 2,753 it would not fit in any calculator. Reduce mod n after every square and every multiply instead.

Questions people ask

Find the modular inverse of e mod φ(n), the number that leaves a remainder of 1 when multiplied by e and divided by φ(n). The extended Euclidean algorithm gets there in a handful of division steps, and Python 3.8 or later does the same with pow(e, -1, phi).

Sources