← Back to Table of Contents

Asymmetric Encryption and Key Pairs

Symmetric encryption uses one key for both encryption and decryption. Asymmetric encryption uses two keys: a public key and a private key. They are mathematically linked, but you can’t derive one from the other.

The Mailbox Analogy

Think of a mailbox on the street. Anyone can walk up and drop a letter through the slot. But only the person with the key to the mailbox can open it and read the letters.

The mail slot is the public key. Anyone can use it to “encrypt” (drop in a letter). The mailbox key is the private key. Only the owner can “decrypt” (open the mailbox and read the letters).

In cryptographic terms:

How It Solves Key Distribution

Remember the chicken-and-egg problem? You need to share an AES key, but you can’t send it securely because you don’t have encryption yet.

Asymmetric encryption breaks the loop:

  1. The server generates a key pair (public + private)
  2. The server publishes its public key (anyone can see it)
  3. Your browser gets the server’s public key
  4. Your browser generates a random AES key
  5. Your browser encrypts the AES key with the server’s public key
  6. Your browser sends the encrypted AES key to the server
  7. The server decrypts it with its private key
  8. Now both sides have the same AES key
sequenceDiagram
    participant B as Browser
    participant S as Server

    S->>S: Generate key pair (public + private)
    S->>B: Here's my public key
    B->>B: Generate random AES key
    B->>B: Encrypt AES key with server's public key
    B->>S: Encrypted AES key
    S->>S: Decrypt with private key
    Note over B,S: Both sides now have the same AES key
    B->>S: Data encrypted with AES
    S->>B: Data encrypted with AES

An eavesdropper sees the public key (useless for decryption) and the encrypted AES key (can’t decrypt it without the private key). The key distribution problem is solved.

Why Not Use Asymmetric for Everything?

If asymmetric encryption can encrypt data, why not use it for all the data instead of just the key exchange?

Because it’s slow. Really slow. RSA encryption is roughly 1,000 times slower than AES. Encrypting a video stream or a large web page with RSA would be painfully slow.

So TLS uses a hybrid approach:

The handshake is the expensive part, but it only happens once per connection. After that, everything runs on fast AES.

RSA: The Classic

RSA (named after Rivest, Shamir, and Adleman, who published it in 1977) was the first widely used asymmetric encryption algorithm. It’s based on the mathematical difficulty of factoring large numbers.

The idea: take two very large prime numbers, multiply them together. That multiplication is easy. But given the product, figuring out which two primes were multiplied is extremely hard. RSA key generation picks two large primes, and the security of the system depends on the difficulty of factoring their product.

RSA key sizes:

These keys are much larger than AES keys (128 or 256 bits) because the underlying math is different. The security of RSA depends on key size in a different way than AES.

Elliptic Curve Cryptography (ECC)

ECC is a newer approach to asymmetric cryptography, based on the mathematics of elliptic curves. It provides the same security as RSA but with much smaller keys.

Security Level RSA Key Size ECC Key Size
128-bit 3072 bits 256 bits
192-bit 7680 bits 384 bits
256-bit 15360 bits 521 bits

A 256-bit ECC key provides roughly the same security as a 3072-bit RSA key. Smaller keys mean faster operations and less data to transmit. This is why ECC has become the preferred choice for TLS.

The most common ECC curves used in TLS are P-256 (also called secp256r1) and X25519 (used for key exchange).

The Trust Problem

Asymmetric encryption solves key distribution. But it introduces a new problem.

Your browser receives a public key that claims to belong to yourbank.com. How do you know it actually belongs to your bank? What if an attacker intercepted the connection and sent you their own public key instead?

If you encrypt the AES key with the attacker’s public key, the attacker can decrypt it. They now have the AES key and can read everything. The encryption is working perfectly, but you’re talking to the wrong person.

This is the authentication problem. And it’s solved by digital signatures and certificates, which we’ll cover after we look at Diffie-Hellman.


Next: Diffie-Hellman: Agreeing on a Secret in Public

← Previous ChapterNext Chapter →