Public-key cryptography
About this lesson
Symmetric encryption needs a shared secret key, and sending that key over an open network lets an eavesdropper copy it. Public-key cryptography fixes this with a key pair: a public key that anyone may use to lock, and a private key that only its owner uses to unlock. Build a toy RSA key in eight lines of Python (n = 3233, e = 17, d = 2753), watch 65 become 2790 and come back, then see why real systems use big keys, OAEP padding and hybrid encryption, and why the move to quantum-resistant algorithms has started.
The key-sharing problem
Symmetric encryption needs both sides to share one secret key first. If Alice just sends that key across the network, an eavesdropper, Eve, can copy it on the way. Then Eve can read everything they encrypt.
symmetric key one key both locks and unlocks problem Alice and Bob must share it first sent in clear Eve copies it on the network result every message is readable by Eve
Read the lesson step by step
The key-sharing problem
Symmetric encryption needs both sides to share one secret key first. If Alice just sends that key across the network, an eavesdropper, Eve, can copy it on the way. Then Eve can read everything they encrypt.
symmetric key one key both locks and unlocks problem Alice and Bob must share it first sent in clear Eve copies it on the network result every message is readable by EveTwo keys: a key pair
Public-key cryptography gives Bob two linked keys, a key pair. The public key works like an open padlock: anyone may use it to lock a message for Bob. Only the private key opens it, and only Bob holds it.
key pair two keys made together public key locks (encrypts) share it with anyone private key unlocks (decrypts) never share it Bob keeps the private key to himselfToy RSA: the public key
Here is RSA, the classic method, with toy numbers. Bob picks two secret primes, p = 61 and q = 53, and multiplies them: n = 3233. He also picks e = 17. The pair n and e is his public key.
p, q = 61, 53 two primes, kept secret n = p * q 3233 e = 17 shares no factor with (p-1)(q-1) public key (n, e) = (3233, 17)The private number d
From p and q, Bob works out phi = 60 × 52 = 3120. Then he finds d = 2753, because 17 × 2753 = 46801, which is 15 × 3120 + 1. Python's pow(e, -1, phi) finds it. d is his private key.
phi = (p-1)*(q-1) 60 × 52 = 3120 d = pow(e, -1, phi) the inverse of e, mod phi check 17 × 2753 = 46801 = 15 × 3120 + 1 private key d = 2753 (with n) p, q and phi are thrown away or kept as secret as dShare the public key openly
Bob sends his public key across the open network. Eve can copy it too, and that is fine: the public key can only lock messages, not open them. Anyone, Eve included, may lock a message for Bob.
sent (n, e) = (3233, 17) Alice gets a copy Eve gets a copy too: harmless what it does locks, never unlocksAlice locks the message
Alice's message is the number 65 (the letter A in ASCII). She locks it with Bob's public key: raise 65 to the power 17, divide by 3233 and keep the remainder. That gives the ciphertext c = 2790.
m = 65 the letter "A" (ASCII 65) c = pow(m, e, n) m to the power e, remainder mod n c = 65^17 mod 3233 = 2790 ciphertext the locked messageThe ciphertext travels
Alice sends 2790. Eve copies it, but to undo the lock she needs d, and d never left Bob. Knowing the public key does not help her: locking is easy, but unlocking without the private key is very hard.
on the wire c = 2790 Eve has n, e and c Eve lacks d (it never left Bob) one-way easy to lock, hard to unlock without dOnly the private key opens it
Bob unlocks it with his private key: raise 2790 to the power 2753, divide by 3233 and keep the remainder. Out comes 65, Alice's message. The two keys undo each other, and only whoever holds d can undo the lock.
back = pow(c, d, n) back = 2790^2753 mod 3233 = 65 65 = "A" the message is back only d undoes eWhy Eve can't find d
To find d, Eve needs p and q, so she must split n into its two primes (factor it). 3233 = 61 × 53 is easy. Real keys use an n of at least 2048 bits, about 617 digits: far beyond any known method today.
toy n = 3233 = 61 × 53 (instant) real n ≥ 2048 bits ≈ 617 decimal digits NIST RSA-2048 ≈ 112-bit security (SP 800-57) RSA-250 (829 bits) took ~2700 CPU core-years in 2020In practice: share a fast key
Public-key maths is slow, so real systems use it only to share or agree on a fresh random key for symmetric encryption, such as AES. That fast key then encrypts the actual data. HTTPS (TLS 1.3) works this way.
step 1 public-key crypto shares a fresh AES key step 2 AES (symmetric) encrypts the data, fast TLS 1.3 agrees keys with (EC)DHE, then uses AES-GCM this mix is called hybrid encryptionMistakes and their fixes
Never use textbook RSA like this toy: the same message always gives the same ciphertext, so short messages can be guessed. Use a trusted library with OAEP padding, keys of 2048 bits or more, and keep private keys out of code.
mistake fix textbook RSA, no padding RSA-OAEP from a vetted library small keys (this toy) RSA ≥ 2048 bits (NIST) private key in source code a key store or secrets manager your own crypto maths use a vetted library (OWASP)Planning for quantum computers
A large quantum computer could factor n and break RSA and elliptic-curve keys. NIST published quantum-resistant standards in 2024, such as ML-KEM (FIPS 203). CISA urges organisations to list where they use public-key cryptography and plan the move.
risk a large quantum computer breaks RSA and ECC NIST FIPS 203 ML-KEM (August 2024) NIST IR 8547 (2026): the plan to retire RSA and ECC CISA inventory your public-key use, plan migration
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01Alice wants to send Bob a secret using public-key encryption. Which key does she lock the message with?
Anyone may lock a message for Bob with his public key. Only Bob's private key can unlock it, so only Bob can read it.
02Eve copies Bob's public key and the ciphertext from the network. Why can she still not read the message?
The public key only locks. To find d she would need p and q, the two primes of n. 3233 = 61 × 53 is easy, but a 2048-bit n (about 617 digits) is far beyond known methods.
03In the toy example (n = 3233, e = 17, d = 2753), what does pow(2790, 2753, 3233) return in Python?
Decrypting with the private key undoes the encryption: 2790 to the power 2753, mod 3233, is 65, Alice's original message.
04Why do HTTPS and other real systems not encrypt all their data with RSA?
This mix is hybrid encryption. TLS 1.3 agrees on fresh keys with (EC)DHE and then protects the data with a symmetric cipher such as AES-GCM.
05A developer encrypts short status codes with RSA, no padding, a 1024-bit key and the private key pasted into the source code. What is the best fix?
Textbook RSA is deterministic, so short messages can be guessed; 1024-bit RSA is below NIST's 112-bit minimum; and keys in source code leak. OWASP lists all three as cryptographic failures.
Glossary
- Public-key (asymmetric) cryptography
- Cryptography that uses a key pair instead of one shared key: what one key locks, only the other key can unlock.
- Public key and private key
- The two halves of a key pair. The public key can be given to anyone and is used to encrypt (lock); the private key stays with its owner and is used to decrypt (unlock).
- RSA
- The classic public-key method (Rivest, Shamir and Adleman, 1978). Its security rests on how hard it is to split a large number n into its two prime factors.
- Modular arithmetic (mod)
- Keeping only the remainder after dividing. 65^17 mod 3233 means: raise 65 to the power 17, divide by 3233, keep the remainder (2790).
- Ciphertext
- The locked (encrypted) form of a message. It can travel over an open network; without the right key it reveals nothing useful.
- Hybrid encryption
- Using public-key cryptography only to share or agree on a fresh symmetric key, then encrypting the data with that faster key (for example, AES).
- OAEP padding
- Optimal Asymmetric Encryption Padding: random padding added before RSA encryption so the same message never gives the same ciphertext (RFC 8017, NIST SP 800-56B).
- Post-quantum cryptography
- New public-key methods, such as ML-KEM (FIPS 203), designed to stay secure even against a large quantum computer that could break RSA and elliptic-curve cryptography.
Notes and sources
- The numbers are deliberately tiny (n = 3233) so you can check them by hand or in Python. Real RSA keys are at least 2048 bits long and are made by a vetted library, never by hand.
- This is "textbook RSA": the bare maths with no padding. Real RSA encryption adds random OAEP padding (RFC 8017, NIST SP 800-56B), so the same message gives a different ciphertext each time.
- In 2020 the 829-bit RSA-250 challenge number was factored with about 2700 CPU core-years of computing. A 2048-bit modulus is far beyond known classical methods; NIST rates it at 112 bits of security.
- TLS 1.3 no longer encrypts the session key with RSA; it agrees on it with (elliptic-curve) Diffie-Hellman, another public-key method, and uses public-key signatures to prove who the server is.
- NIST IR 8547 (October 2026) sets out how federal systems move from RSA and elliptic-curve cryptography to the new standards. The same keys protect data recorded today, so long-lived secrets are the first to move.
- This course is about defence. It explains weaknesses at the level of the mistake and the fix, and never how to attack a real system.
- NIST SP 800-57 Part 1 Rev. 5: Recommendation for Key Management (security strengths of RSA key sizes)
- NIST SP 800-56B Rev. 2: Pair-Wise Key Establishment Using Integer Factorization Cryptography (RSA-OAEP)
- NIST SP 800-131A Rev. 2: Transitioning the Use of Cryptographic Algorithms and Key Lengths
- NIST FIPS 203: Module-Lattice-Based Key-Encapsulation Mechanism Standard (ML-KEM)
- NIST IR 8547: Transition to Post-Quantum Cryptography Standards
- CISA: Post-Quantum Cryptography Initiative
- OWASP Top 10 2021, A02: Cryptographic Failures
- OWASP Cryptographic Storage Cheat Sheet
- RFC 8017: PKCS #1: RSA Cryptography Specifications Version 2.2
- RFC 8446: The Transport Layer Security (TLS) Protocol Version 1.3
- Rivest, Shamir, Adleman: A Method for Obtaining Digital Signatures and Public-Key Cryptosystems (CACM, 1978)
- Python documentation: built-in pow() (modular inverse with exponent -1)
- UC San Diego: New record set for cryptographic challenge (RSA-250, 2020)
Quiz Unlocks after the last step.