ENع
←Public-key crypto
Lesson 08 · Cryptography

Public-key cryptography

14 min12 steps5 questionsIntermediate
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 8 · Interactive
01 / 12

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
Loading figure…
or ← →
Read the lesson step by step
  1. 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
  2. Two 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 himself
  3. Toy 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)
  4. 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 d
  5. Share 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 unlocks
  6. Alice 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 message
  7. The 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 d
  8. Only 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 e
  9. Why 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 2020
  10. In 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 encryption
  11. Mistakes 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)
  12. 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

Quiz Unlocks after the last step.