RSA Encryption Example Step by Step (With Small Numbers)
A full RSA example with small numbers: p = 61, q = 53, e = 17, d = 2753. Encrypt 65 to 2790 and back, with every step of the math shown and checked.
RSA works by turning a message into a number m and raising it to a public power modulo a public number n: c = m^e mod n. Decryption raises c to a private power d: m = c^d mod n. The two exponents undo each other only because d was computed from the secret prime factors of n. With the classic textbook values p = 61 and q = 53, you get n = 3233, φ(n) = 3120, e = 17 and d = 2753, and the message 65 encrypts to 2790, which decrypts back to 65. The rest of this article walks through every one of those numbers, shows the arithmetic you would actually do by hand, and then explains why this tiny version is a toy and what real RSA adds.
Every value below was computed with the same BigInt helpers (modPow, gcd, modInverse) that power the interactive RSA page on this site, not copied from a textbook.
The whole example at a glance
Before the details, here is the complete key and one round trip. Keep this table open; each line gets its own section below.
| Quantity | How it is obtained | Value | Public or secret |
|---|---|---|---|
| p | chosen prime | 61 | secret |
| q | chosen prime | 53 | secret |
| n | p × q | 3233 | public |
| φ(n) | (p − 1)(q − 1) | 3120 | secret |
| e | coprime with φ(n) | 17 | public |
| d | e⁻¹ mod φ(n) | 2753 | secret |
| m | the message, as a number | 65 | secret |
| c | m^e mod n | 2790 | sent in the clear |
| m again | c^d mod n | 65 | recovered |
The public key is the pair (n, e) = (3233, 17). The private key is d = 2753. The message 65 is not arbitrary: it is the ASCII code of the letter "A", which is how a computer would see a one-character message. In this toy key, any message must be a number smaller than n, so 0 to 3232.
Step 1: two primes and a product that hides them
Both 61 and 53 are prime. Their product is n = 61 × 53 = 3233, and n is published. The whole security of RSA hangs on one asymmetry: going from (61, 53) to 3233 is a single multiplication, while going back from 3233 to (61, 53) requires factoring.
For 3233 that is easy. The square root of 3233 is about 56.9, so a factor must appear among the primes up to 56. Trial division tries 2, 3, 5, 7, 11 and so on; the remainders are all non-zero until the sixteenth prime, 53, which divides 3233 exactly. Sixteen divisions and the key is broken. Hold on to that thought: the security section shows what happens when n has 617 digits instead of 4.
Step 2: φ(n), the number that must stay secret
Euler's totient φ(n) counts how many numbers between 1 and n share no factor with n. For a product of two primes it has a simple formula:
φ(n) = (p − 1)(q − 1) = 60 × 52 = 3120
Why this number? Because of Euler's theorem: for any m coprime with n, m^φ(n) ≡ 1 (mod n). Exponents "wrap around" modulo φ(n), and RSA exploits exactly that wrap-around to make one exponent cancel the other.
φ(n) is as sensitive as the primes themselves. If an attacker learns n = 3233 and φ(n) = 3120, they get p + q = n − φ(n) + 1 = 114. Two numbers with sum 114 and product 3233 are the roots of x² − 114x + 3233 = 0. The discriminant is 114² − 4 × 3233 = 64, whose square root is 8, so the roots are (114 + 8) / 2 = 61 and (114 − 8) / 2 = 53. Knowing φ(n) is the same as knowing the factorization.
Step 3: choosing e, and why it must be coprime with φ(n)
The public exponent e must have an inverse modulo φ(n), otherwise no private exponent d exists and encryption cannot be undone. An inverse exists exactly when gcd(e, φ(n)) = 1.
Here 3120 = 2⁴ × 3 × 5 × 13, so a lot of small candidates fail: 3 and 5 divide 3120, and so does 13. Among the small primes, 7, 11 and 17 pass the test. The textbook example uses 17, and gcd(17, 3120) = 1.
In practice almost every RSA key uses e = 65537 (binary 10000000000000001), because it is prime, large enough to avoid small-exponent pitfalls, and has only two 1 bits, which makes encryption fast. A curious detail of this toy key: 65537 mod 3120 = 17, so e = 65537 would behave exactly like e = 17 and give the same d = 2753.
Step 4: computing d with the extended Euclidean algorithm
We need d such that 17 × d ≡ 1 (mod 3120). The extended Euclidean algorithm finds it by running Euclid's gcd computation while tracking how each remainder can be written as a combination of 3120 and 17.
Each row below computes the next remainder r = previous remainder − q × current remainder, and updates the coefficient t of 17 with the same rule.
| Step | Division | Quotient q | Remainder r | Coefficient t of 17 |
|---|---|---|---|---|
| start | 3120 | 0 | ||
| start | 17 | 1 | ||
| 1 | 3120 = 183 × 17 + 9 | 183 | 9 | 0 − 183 × 1 = −183 |
| 2 | 17 = 1 × 9 + 8 | 1 | 8 | 1 − 1 × (−183) = 184 |
| 3 | 9 = 1 × 8 + 1 | 1 | 1 | −183 − 1 × 184 = −367 |
| 4 | 8 = 8 × 1 + 0 | 8 | 0 | stop |
The last non-zero remainder is 1, which confirms gcd(17, 3120) = 1. Reading the same thing by back-substitution:
1 = 9 - 8
= 9 - (17 - 9) = 2 x 9 - 17
= 2 x (3120 - 183 x 17) - 17
= 2 x 3120 - 367 x 17
So −367 × 17 ≡ 1 (mod 3120). A negative exponent is awkward, so add 3120: d = −367 + 3120 = 2753. Check: 17 × 2753 = 46801 = 15 × 3120 + 1. The remainder is 1, as required.
Step 5: encrypting 65 with square-and-multiply
Computing 65^17 directly would produce a 31-digit number before reducing it. Real implementations never do that. They use square-and-multiply: read the exponent in binary from left to right, square the running value for each bit, and multiply by the base when the bit is 1, reducing mod n after every operation.
17 in binary is 10001.
| Bit | Exponent so far | Operation | Value mod 3233 |
|---|---|---|---|
| 1 | 1 | start with 65 | 65 |
| 0 | 2 | 65² = 4225 | 992 |
| 0 | 4 | 992² = 984064 | 1232 |
| 0 | 8 | 1232² = 1517824 | 1547 |
| 1 | 16 | 1547² = 2393209 | 789 |
| 17 | 789 × 65 = 51285 | 2790 |
Four squarings and one multiplication, and no intermediate value ever exceeds n². The ciphertext is c = 2790. This is also why e = 65537 is popular: its binary form has only two 1 bits, so encryption costs 16 squarings and a single multiplication.
The same operation, modular exponentiation, is the core of Diffie-Hellman key exchange. If you want to see it from another angle, the Diffie-Hellman visualization mixes colors and then numbers with exactly this mod-power step.
Step 6: decrypting 2790 back to 65
Decryption is the same algorithm with the private exponent. 2753 in binary is 101011000001: 12 bits, five of them set, so the ladder takes 11 useful squarings and 4 extra multiplications.
| Bit | Exponent so far | Value mod 3233 |
|---|---|---|
| 1 | 1 | 2790 |
| 0 | 2 | 2269 |
| 1 | 5 | 1425 (square), then × 2790 → 2393 |
| 0 | 10 | 806 |
| 1 | 21 | 3036 (square), then × 2790 → 3213 |
| 1 | 43 | 400 (square), then × 2790 → 615 |
| 0 | 86 | 3197 |
| 0 | 172 | 1296 |
| 0 | 344 | 1689 |
| 0 | 688 | 1215 |
| 0 | 1376 | 1977 |
| 1 | 2753 | 3065 (square), then × 2790 → 65 |
The last line lands on 65. The letter "A" is back.
Why it comes back
Since e × d = 46801 = 1 + 15 × 3120, we have c^d = m^(e×d) = m × (m^3120)^15. By Euler's theorem m^3120 ≡ 1 (mod 3233), so the whole expression reduces to m. That is the entire trick: d is chosen so that e × d is one more than a multiple of φ(n). (The identity also holds for the rare messages that share a factor with n, by a short argument with the Chinese remainder theorem.)
A signature in two lines
Signing swaps the roles of the exponents. The signer uses the private d, and anyone verifies with the public e:
s = m^d mod n = 65^2753 mod 3233 = 588
check: s^e mod n = 588^17 mod 3233 = 65 (matches m)
Only the holder of d could have produced 588, yet anyone with (3233, 17) can confirm it. In real systems you sign a hash of the message (for example SHA-256), padded with a scheme such as RSA-PSS, never the raw message.
How hard is it to break? From 16 divisions to 2,700 core-years
For n = 3233, trial division needs 16 attempts. Trial division up to √n becomes useless very fast, though. A 2048-bit modulus has 617 decimal digits, and its square root is a 1024-bit number: no amount of hardware will ever enumerate candidates up to that bound.
The best known classical method is the general number field sieve. It is sub-exponential: much faster than trial division, but its cost still grows faster than any polynomial in the size of n. The public record shows what that means in practice. RSA-250, a 250-digit (829-bit) challenge number, was factored in 2020 at a cost of about 2,700 CPU core-years. A 2048-bit modulus is estimated to be billions of times harder than that.
If you want to feel this growth instead of reading about it, the interactive RSA page lets you build a key with p = 5 and q = 11, see n drawn as a rectangle, and then watch your own browser try to factor keys of increasing size until it visibly gives up. The same doubling logic for symmetric keys is on the brute-force and key size page.
Why textbook RSA is insecure
Everything above is "textbook RSA", and it should never be used as is. Two properties break it:
- It is deterministic. Encrypting 65 with (3233, 17) always gives 2790. An attacker who suspects the message is "A" encrypts 65 with the public key and compares. With a small set of possible messages (yes or no, a salary, a vote), every message can be guessed and checked.
- It is malleable. Multiplying ciphertexts multiplies plaintexts. Take c = 2790 and multiply it by 2^17 mod 3233 = 1752: you get 3017, which decrypts to 130, exactly 2 × 65. The attacker changed the hidden message without ever knowing it.
Small exponents without padding, primes shared between keys, and weak random number generators have all broken real RSA keys. The fix for encryption is OAEP padding (defined in PKCS #1 v2, RFC 8017), which mixes the message with fresh randomness before exponentiation, so the same message encrypts differently every time and tampered ciphertexts are rejected. Signatures use RSA-PSS for the same reason. The older PKCS #1 v1.5 encryption padding is still around, but it has a long history of padding-oracle attacks, starting with Bleichenbacher in 1998.
Key sizes that make sense today
| Modulus size | Rough security level | Status |
|---|---|---|
| 512 bits | very low | factored publicly in 1999 |
| 829 bits | low | RSA-250, factored in 2020 |
| 1024 bits | about 80 bits | deprecated |
| 2048 bits | about 112 bits | current minimum |
| 3072 bits | about 128 bits | recommended for long-term data |
Use at least 2048 bits. NIST guidance treats 2048-bit RSA as acceptable through 2030 and 3072 bits or more beyond that. For comparison, symmetric ciphers like AES reach 128-bit security with a 128-bit key, which is why RSA is used to protect or sign small things (keys, hashes) and never to encrypt bulk data.
The quantum footnote that is no longer a footnote
Shor's algorithm factors integers in polynomial time on a sufficiently large, error-corrected quantum computer. No such machine exists today, but an attacker can record RSA-protected traffic now and decrypt it later. That is why migration has started: NIST standardized ML-KEM (FIPS 203) for key establishment and ML-DSA (FIPS 204) as a post-quantum digital signature in 2024. RSA will keep running on the web for years, but new long-lived systems are being designed with these replacements in mind.
Try it with your own numbers
Reading the tables is one thing; watching the same steps move is another. On the RSA visualization, build the same kind of key with p = 5 and q = 11, see n drawn as a rectangle, see why e = 2 is rejected because it shares a factor with φ(n), watch square-and-multiply run bit by bit, and then see factoring time explode as n grows. Then browse the other modern cryptography walkthroughs to see how AES, SHA-256 and Diffie-Hellman fit next to RSA.