open atlas
↑ Back to track
Security Foundations SECF · 02 · 03

Asymmetric crypto and key exchange

Public-key crypto solves the key-distribution problem symmetric ciphers can't, but it's 1000x slower — so you use it only to agree a symmetric key. This lesson covers RSA vs ECC, Diffie-Hellman key exchange, and why ephemeral DH gives forward secrecy.

SECF Middle ◷ 15 min
Level
FoundationsJuniorMiddleSenior

Symmetric encryption is fast and unbreakable — and useless until two parties share a key. That’s the catch nobody warns you about: AES-256 is great, but how does the browser and the server both know the same 32 random bytes when their first packet crosses an open Wi-Fi network full of people who can read everything? You can’t ship the key in the clear; an eavesdropper grabs it and decrypts the whole session. You can’t pre-share it; you’ve never met the server. For decades this was a genuine wall — until 1976, when Diffie and Hellman published a trick that lets two strangers derive a shared secret over a wiretapped line, where the attacker watches every byte go by and still can’t compute it. Everything you call “HTTPS” rests on that one move.

By the end of this lesson you’ll know why asymmetric crypto exists, where it’s used and where it isn’t, how Diffie-Hellman lets strangers agree on a key in the open, and what forward secrecy actually buys you.

Why a second kind of crypto exists

Symmetric ciphers — AES, ChaCha20 — use one key for both encrypt and decrypt. They are fast (a modern CPU does gigabytes per second with AES-NI) and, at 256 bits, beyond brute force. Their fatal limitation is the key-distribution problem: both sides need the same secret before they can talk, and getting it there safely over an untrusted network is exactly the problem you were trying to solve.

Asymmetric (public-key) crypto breaks the symmetry. Each party has a key pair: a public key it hands out freely and a private key it never shares. The two are mathematically linked so that what one does, only the other undoes. That gives you two distinct powers:

  • Encryption: anyone encrypts to your public key; only your private key decrypts. Confidentiality without a pre-shared secret.
  • Signatures: you sign with your private key; anyone verifies with your public key. Authenticity and integrity — proof the message came from the holder of that private key and wasn’t altered.

The catch is cost. RSA-2048 decryption is on the order of a thousand times slower than AES per byte, and asymmetric algorithms also have a hard size limit on what they can directly encrypt (RSA can’t encrypt more than its key size minus padding). So nobody encrypts a 4 MB request body with RSA. The universal pattern is hybrid encryption: use the expensive asymmetric step once to agree on a fresh random symmetric key, then encrypt the actual bulk traffic with fast AES. Asymmetric crypto is the handshake; symmetric crypto is the conversation.

RSA vs elliptic curve: same job, different math

Two families dominate. RSA rests on the difficulty of factoring the product of two large primes. It’s old, battle-tested, and simple to reason about — but its keys are large and getting larger as factoring attacks improve. Elliptic-curve cryptography (ECC) rests on a different hard problem (the elliptic-curve discrete log) that resists attack far better per bit, so it delivers equivalent security with dramatically smaller keys and faster operations. This is why mobile, TLS, and modern protocols moved to curves like P-256 and X25519.

The security-per-bit gap is the whole story — here is the equivalence the standards bodies publish:

Security levelSymmetric (AES)RSA key sizeECC key sizeWhat it means
~112-bit3DES (legacy)2048-bit224-bitMinimum acceptable today
128-bitAES-1283072-bit256-bitThe modern default (P-256 / X25519)
192-bitAES-1927680-bit384-bitHigh assurance (P-384)
256-bitAES-25615360-bit521-bitNote RSA’s brutal scaling: 5x the bits for 2x the security

Read the bottom row carefully: to match AES-256, RSA needs a 15360-bit key — at which point key generation and operations are painfully slow — while ECC needs only 521 bits. RSA key size scales roughly with the cube root of the work to break it, so doubling the security level more than doubles the key. That non-linear penalty is why new systems default to curves and you mostly meet RSA only in legacy certificates and existing PKI.

Diffie-Hellman: agreeing on a secret in the open

RSA-style key transport (one side picks the symmetric key and encrypts it to the other’s public key) works, but it has a flaw we’ll get to. The more important primitive is Diffie-Hellman key exchange (DH), and its elliptic-curve form ECDH. DH is not encryption — it’s a way for two parties to jointly compute a shared secret that neither one chose alone, over a channel an attacker is reading in full.

The mechanism, stripped to its essence: both sides agree on public parameters. Each side picks a private random value, derives a public value from it, and sends only the public value across the wire. Then each side combines its own private value with the other’s public value — and by the algebra of the construction, both arrive at the identical shared secret. The eavesdropper saw both public values but neither private one, and recovering the secret from what crossed the wire is the hard problem (discrete log) that no efficient attack solves. The shared secret is then run through a key-derivation function to produce the symmetric session key.

There’s a critical caveat: raw DH gives you confidentiality against a passive eavesdropper, but it does not tell you who’s on the other end. An active attacker who sits in the middle can run a separate DH with each side and relay between them — a man-in-the-middle. DH must therefore be combined with authentication: in TLS the server signs its DH public value with the private key behind its certificate, so the client knows the value came from the real server and not an impostor. Key exchange establishes a secret; signatures and certificates establish who you’re sharing it with. You need both.

Why this works

Why not just keep using RSA key transport, where the client encrypts the chosen session key to the server’s public key? Because of what happens after a breach. With RSA key transport, the session key is protected by the server’s long-lived private key — so an attacker who records your encrypted traffic today and steals that private key in two years can go back and decrypt every recorded session. The static key never changed, so one theft unlocks the whole archive. This is precisely the weakness ephemeral Diffie-Hellman removes, and it’s why TLS 1.3 dropped RSA key transport entirely and mandates ephemeral (EC)DH for every handshake.

Forward secrecy: making the past un-decryptable

The fix for that archive problem is forward secrecy (also called perfect forward secrecy). The idea: generate a brand-new, throwaway DH key pair for every session — that’s the ephemeral in ECDHE — and discard the private values the moment the handshake completes. The long-term certificate key is used only to sign the ephemeral exchange, proving identity; it never encrypts the session key and never touches the shared secret.

The payoff is the property that an attacker who compromises the server’s long-term private key later cannot decrypt traffic captured earlier. Each session’s secret died with the ephemeral keys that produced it; there is nothing left on disk to recover it from. Without forward secrecy, one stolen private key retroactively unlocks years of recorded sessions — the “harvest now, decrypt later” attack, where an adversary records ciphertext today specifically to crack it once it obtains the key. With forward secrecy, each session is its own island. This is why TLS 1.3 makes ephemeral (EC)DH non-negotiable.

Pick the best fit

You're configuring TLS for a service that handles sensitive long-lived data, and you must choose the key-exchange mode. Which choice gives forward secrecy and why does it matter here?

Where asymmetric crypto is — and isn’t — used

The senior mental model: asymmetric crypto is for bootstrapping trust and keys, not for bulk work. It shows up at exactly three moments — agreeing a session key (ECDHE), proving identity (the certificate’s signature over the handshake), and verifying authenticity at rest (signed software updates, signed JWTs, code signing). Everywhere else — the gigabytes of actual request and response bodies, the data encrypted in your database, the files on disk — is symmetric AES, because asymmetric is too slow and size-limited to do that work. When you see “encrypt this 50 MB upload,” the answer is never “RSA”; it’s “generate a random AES key, encrypt the data with AES, and protect that one small key with the asymmetric layer.”

Quiz

Why do real systems use hybrid encryption (asymmetric to agree a key, then symmetric for the data) instead of just encrypting everything with the recipient's public key?

Quiz

An attacker has been silently recording your TLS traffic for two years. Today they steal your server's long-term private key. With TLS 1.3's ephemeral ECDH, what can they decrypt?

Order the steps

Order the steps of a forward-secret TLS handshake + session, from first to last:

  1. 1 Each side generates a fresh ephemeral DH key pair for this session
  2. 2 Each side sends its ephemeral DH public value across the wire
  3. 3 The server signs its ephemeral value with its certificate's private key (authentication)
  4. 4 Both sides combine their private value with the peer's public value to derive the same shared secret
  5. 5 Bulk application data flows encrypted with the fast symmetric (AES) key derived from that secret
Recall before you leave
  1. 01
    Explain why systems use hybrid encryption and what each crypto family does in a TLS session.
  2. 02
    What is Diffie-Hellman, why does it need authentication, and how does ephemeral DH give forward secrecy?
Recap

Symmetric crypto (AES) is fast and secure but can’t solve key distribution: both parties need the same secret first. Asymmetric crypto fixes that with public/private key pairs — anyone encrypts to your public key, only your private key decrypts; you sign with your private key, anyone verifies with your public one. But it’s ~1000x slower and size-limited, so the universal pattern is hybrid encryption: use asymmetric once to agree a symmetric key, then AES for the bulk. ECC beats RSA dramatically per bit (a 256-bit curve matches a 3072-bit RSA key), which is why modern protocols use P-256 and X25519. Diffie-Hellman lets two strangers derive a shared secret over a wiretapped line because recovering it is the discrete-log problem — but it needs authentication (a signature) to stop a man-in-the-middle. Making the DH keys ephemeral and per-session gives forward secrecy: a future theft of the long-term key can’t decrypt recorded past traffic, which is why TLS 1.3 mandates ephemeral (EC)DH. Next time you see “encrypt this large payload,” your reflex is: generate a random AES key, encrypt the data with AES, and protect that one small key with the asymmetric layer — never RSA the whole thing.

Practice

Start at the top. Tasks go easiest → hardest: recall a fact, apply it to a case, then a senior-level stretch. Open one, attempt it, then reveal.

recallapplystretch0 of 6 done
Connected lessons

Something unclear?

Ask a question about this lesson. Questions are anonymous and go straight to the author to make the lesson better.

shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources2
expand
  1. 01
  2. 02

Trademarks belong to their respective owners. Editorial reference only.