RSA and Asymmetric Encryption
Learn how public and private key pairs work, generate a tiny RSA key by hand, encrypt and decrypt a number, and see why real systems use hybrid encryption.
Welcome back!
Step 1 of 13Learn
A mailbox anyone can use
Picture a mailbox with a slot. Anyone walking by can drop a letter in, but only the owner, who has the key, can open the box and read the letters. The slot is public; the key is private.
That's asymmetric encryption. Each person generates a key pair:
- A public key, shared openly: published on a website, sent in an email, embedded in a certificate.
- A private key, kept secret and never shared.
Anything encrypted with the public key can only be decrypted with the matching private key. So to send Bob a secret, Alice looks up Bob's public key and encrypts with it. Even Alice can't decrypt the result afterward; only Bob can.
The two keys are mathematically linked, but computing the private key from the public key must be infeasible. That's where a trapdoor one-way function comes in.
The first practical public-key encryption system was RSA, published in 1977 by Ron Rivest, Adi Shamir and Leonard Adleman. Its security rests on the factoring problem: multiplying two large prime numbers is easy, but given only their product, finding the primes is extraordinarily hard. Multiply two 1,024-bit primes in microseconds; factor their 2,048-bit product and, with the best known classical algorithms and all the world's computers, you'd still be working long after the Sun burns out.
Step 2 of 13Compare
Symmetric vs asymmetric encryption
| Aspect | Symmetric | Asymmetric |
|---|---|---|
| Keys | One shared secret | Public key + private key |
| Who can encrypt | Only key holders | Anyone with the public key |
| Speed | Very fast (GB/s) | Thousands of times slower |
| Key distribution | Hard: secret must be shared | Easy: public key can be published |
| Examples | AES, ChaCha20 | RSA, elliptic curves |
Step 3 of 13Learn
Building a tiny RSA key
Real RSA keys use primes hundreds of digits long, but the steps are the same with small numbers.
- Choose two primes: p = 5 and q = 11.
- Compute the modulus: n = p × q = 55. This is part of the public key.
- Compute φ(n) = (p − 1)(q − 1) = 4 × 10 = 40. This count must stay secret, because it reveals the private key; computing it requires knowing p and q.
- Choose the public exponent e: a number with no common factor with 40. We pick e = 3. (Real systems almost always use e = 65,537.)
- Compute the private exponent d: the number with (e × d) mod φ(n) = 1. Here 3 × 27 = 81, and 81 mod 40 = 1, so d = 27.
The public key is (n = 55, e = 3). The private key is d = 27 (along with n).
Encryption: c = me mod n. To encrypt the message m = 7: 73 = 343, and 343 mod 55 = 13. The ciphertext is 13.
Decryption: m = cd mod n = 1327 mod 55, which works out to 7. The original message returns.
Why it works comes from a theorem by Euler about modular arithmetic: raising to the power e and then d is the same as raising to the power e × d, which is 1 more than a multiple of φ(n), and that brings every message back to itself. An attacker who knows only n = 55 must factor it to find φ(n) and then d. For 55 that's trivial; for a 2,048-bit n it's out of reach.
Step 4 of 13Try the calculator
Tiny RSA calculator
Uses the key from the text: n = 55, e = 3, d = 27. Pick a message number below 55 and watch encryption and decryption round-trip.
- Ciphertext c = m3 mod 55
- 8
- Decrypted c27 mod 55
- 2
Step 5 of 13Practice · Work it out
For an RSA key built from primes p = 7 and q = 13, what is φ(n) = (p − 1)(q − 1)?
Step 6 of 13Practice · Work it out
Using the tiny public key (n = 55, e = 3), what is the ciphertext for the message m = 2?
Step 7 of 13Learn
Key sizes and padding
Because factoring algorithms keep improving, RSA needs much longer keys than symmetric ciphers for similar security:
| Symmetric strength | RSA or classic DH modulus | Elliptic curve key |
|---|---|---|
| 112 bits | 2,048 bits | 224 bits |
| 128 bits | 3,072 bits | 256 bits |
| 256 bits | 15,360 bits | 512 bits |
2,048 bits is the common minimum for RSA today, with 3,072 or more recommended for long-term security. Elliptic curve cryptography reaches the same strength with far smaller keys, which is why ECDH and elliptic curve signatures now dominate new designs.
Textbook RSA, exactly as in the tiny example, is insecure in practice. It's deterministic, so the same message always gives the same ciphertext, and an attacker can test guesses: if the message is yes or no, encrypt both with the public key and compare. It's also malleable: multiplying ciphertexts multiplies the hidden plaintexts.
Real RSA encryption therefore uses padding schemes that add structured randomness before encrypting. The modern one is OAEP. An older scheme, PKCS#1 v1.5, led to a famous attack by Daniel Bleichenbacher in 1998, where a server's error messages let attackers decrypt data, and variants of this attack kept resurfacing for two decades. As with everything in cryptography: use a vetted library and its recommended defaults.
Step 8 of 13Practice · Chart
Key size needed for equal security
Tap the strength level where RSA needs 3072-bit keys.
Tap the bar that answers the question, then press Check.
Step 9 of 13Learn
Hybrid encryption
Public-key operations are slow. An RSA-2048 decryption takes on the order of a millisecond, thousands of times slower per byte than AES, and RSA can only encrypt a message smaller than its modulus, a couple of hundred bytes. Encrypting a video file with RSA directly would be absurd.
So real systems use hybrid encryption, combining the strengths of both families:
- Generate a random symmetric key, say a 256-bit AES key, used just for this message or session.
- Encrypt the actual data with AES-GCM using that key: fast, for any amount of data.
- Protect the AES key with public-key cryptography, either by encrypting it with the recipient's public key or, more commonly today, by deriving it from a Diffie-Hellman exchange.
- Send the encrypted key along with the encrypted data.
The recipient uses their private key to recover the AES key, then decrypts the data.
Every major secure system works this way: HTTPS sessions, encrypted email with PGP or S/MIME, messaging apps, and file encryption tools. Public-key cryptography solves the who and the key exchange; symmetric cryptography does the heavy lifting.
A useful mental model: public-key cryptography is a secure envelope that's expensive to make but works between strangers. You only put a small, precious thing in it: the key to the much bigger, cheaper box that holds the actual data.
Step 10 of 13Diagram
Hybrid encryption: best of both
Tap a shape with to see more.
- Random AES key
Fresh for every message.
- AES-GCM encrypts the file
Fast, any size.
- Recipient's public key wraps the AES key
Only 32 bytes need slow public-key encryption.
- Send both
Recipient unwraps the AES key with the private key, then decrypts the file.
Step 11 of 13Practice · Put in order
Put the steps of hybrid encryption, as performed by the sender, in order.
Drag the items, or use the arrow buttons, to put them in order.
- Generate a random one-time symmetric key
- Send the encrypted key and the encrypted data together
- Encrypt the symmetric key with the recipient's public key
- Encrypt the data with the symmetric key using AES-GCM
Step 12 of 13Note
Step 13 of 13Practice · Choose one
Bob wants anyone in the world to be able to send him encrypted files that only he can read. What should he publish?
Lesson complete!