RSA
calculator.
Work through RSA with small primes: find d, then encrypt and decrypt a number.
- Modulus n
- 3,233
- φ(n)
- 3,120
- Bits in n
- 12
- Ciphertext c
- 2,790
This modulus has 12 bits. NIST rates 2048 bits as the smallest size that still gives 112-bit security, so a 12-bit key is for learning only: trial division finds 53 after 27 tries.
Encrypt and decrypt
The message 65 raised to 17 and reduced mod 3,233 gives the ciphertext 2,790. Raising 2,790 to d = 2,753 mod 3,233 gives 65, the message again.
Key size
NIST SP 800-57, Table 2
| Modulus bits | Security bits | Your key |
|---|---|---|
| 2,048 | 112 | |
| 3,072 | 128 | |
| 7,680 | 192 | |
| 15,360 | 256 |
Your 12-bit modulus is below every row. Sizes under 2,048 bits give less than 112-bit security and are no longer approved.
Working
- 1Modulus: multiply the two primesn = 61 × 53 = 3,233
- 2Euler’s totient: one less than each prime, multipliedφ(n) = 60 × 52 = 3,120
- 3Carmichael’s λ: the least common multiple of p − 1 and q − 1λ(n) = lcm(60, 52) = 780
- 4e must share no factor with φ(n)gcd(17, 3,120) = 1
- 5Private exponent: run the extended Euclidean table until the remainder is 117 × −367 leaves 1 when divided by 3,120, so d = −367 + 3,120 = 2,753
- 6Check: e × d is one more than a multiple of φ(n)17 × 2,753 = 46,801 = 15 × 3,120 + 1
- 7Encrypt: c = m to the power e, mod n (square and multiply)c = 65^17 mod 3,233 = 2,790
- 8Check: decrypting with d gives the message back2,790^2,753 mod 3,233 = 65
Finding 17 inverse mod 3,120
| Remainder | Quotient | Times e |
|---|---|---|
| 3,120 | 0 | |
| 17q = 183 | 183 | 1 |
| 9q = 1 | 1 | −183 |
| 8q = 1 | 1 | 184 |
| 1q = 8 | 8 | −367 |
| 0 | 3,120 |
Each remainder is the one two rows up minus the quotient times the one above. The coefficient follows the same rule, so it always says how many times e gives that remainder.
65 to the power 17, mod 3,233
| Bit | Squared | Result |
|---|---|---|
| 1 | 1 | 65 |
| 0 | 992 | 992 |
| 0 | 1,232 | 1,232 |
| 0 | 1,547 | 1,547 |
| 1 | 789 | 2,790 |
Encrypt a list
Each message
| Message m | Ciphertext c | Decrypted |
|---|---|---|
| 72 | 3,000 | 72 |
| 105 | 3,179 | 105 |
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:
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.
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.
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.
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.
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.
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.