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.
A toy RSA 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.
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.
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.
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.
Locking and unlocking
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.
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.
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.
Using it safely
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.
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.
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.
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.
What happens
;; step 1/12 · problem 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
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.