Encryption, from the ground up · Puzzle 1 · 2 of 6

One key for both

In puzzle 0, encryption turned the password into gibberish. Here's how the standard way does it, why it's fast, and the one problem it leaves open.

The problem

In puzzle 0, encryption turned the password into ciphertext with a key. Your app and your server will send thousands of messages: every request, every response, every image. Whatever does the encrypting has to be fast, and both sides have to be able to undo it. What does that look like?

See it

The same key encrypts and decrypts; AES cuts data into 128-bit blocks and scrambles each one; the open problem is getting the same key to both sides your app your server keysame key blocks of 128 bits scrambled: AES listenercopy?
  1. One key, and both sides have it: lock with it, unlock with the same key. That's symmetric encryption.

    Step 1 of 4
  2. The standard, AES, cuts the data into blocks of 128 bits, 16 bytes each.

    Step 2 of 4
  3. It scrambles every block with the key, in a way only the same key can undo. Fast enough for everything you send. Predict: what's the catch?

    Step 3 of 4
  4. Both sides need that key. Send it over the line, and the listener copies it, then decrypts everything.

    Step 4 of 4

The building block

The simplest arrangement: one key, and it works both ways. Encrypt with it, decrypt with the same key. That's symmetric encryption.

The standard one is AES. It takes the data in blocks of 128 bits (16 bytes) and scrambles each with the key, in rounds of mixing and substituting, in a way that can only be undone with that same key. Keys come in three sizes: 128, 192 or 256 bits. A 128-bit key has 2 to the power 128 possible values, a 39-digit number: far more than any computer could ever try, which is why guessing it isn't a way in.

AES is fast, and phone chips have circuits built for it, so it can encrypt everything you send. (In real use, AES doesn't scramble your data blocks directly, which would make two identical blocks look identical. Instead it scrambles a counter, a number that goes up by one for every block, and the scrambled result is mixed into your data. Same key, a fresh pattern every block. And puzzle 5 adds a check against changes.) That's why, as puzzle 5 shows, even HTTPS uses it for all the actual data.

But it leaves one problem. Your app and your server both need the same key. You can't send it over the line from puzzle 0: the listener would copy it, and then decrypt everything. And you can't build one key into every copy of your app: anyone could pull it out of the app file. How do two sides get the same key, when everything they say can be heard? That's puzzle 2.

The analogy

A box with one lock and one key, cut twice. Lock it with your copy, your friend opens it with theirs. The box is fast and strong. The hard part is getting the second copy of the key to your friend without anyone on the way copying it.

Check yourself

Your app encrypts a 1 MB photo with AES. Roughly how does AES handle it?

Right. Fixed-size blocks, one key.

Not quite. Every block uses the same key; a new key per block would need its own way to share.

Not quite. The key is 128 to 256 bits; AES works through the data in 128-bit blocks.

Why can't your app just send the AES key to the server first?

Not quite. A key is only 16 to 32 bytes; the problem is who can see it on the way. Re-read that part ↑

Right. Anything sent openly, the listener gets too.

Not quite. Either side could make it; the problem is sending it where others can hear. Re-read that part ↑

What's the difference between a 128-bit block and a 128-bit key?

Right. Data piece vs secret.

Not quite. The block is your data; the key is separate, and must stay secret.

Sources

  • NIST FIPS 197: Advanced Encryption Standard (AES) ("The AES algorithm is a symmetric block cipher that can encrypt (encipher) and decrypt (decipher) digital information. The AES algorithm is capable of using cryptographic keys of 128, 192, and 256 bits to encrypt and decrypt data in blocks of 128 bits.")

What you now know

In symmetric encryption, the same key locks and unlocks. AES, the standard, encrypts in blocks of 128 bits with a key of 128, 192 or 256 bits; it's fast enough for everything you send. Its open problem: both sides need the same key, and you can't just send it.