Articles & BlogBy 10 min read

Once two devices share a secret key, something has to use that key to scramble the actual data. There are two designs for doing it. A block cipher chops the data into fixed-size chunks and scrambles each chunk. A stream cipher uses the key to produce a long run of random-looking bits and blends that run into the data, one bit at a time.

Why should anyone outside a cryptography class care? Because the most common mistakes in real products are made here. A strong cipher used in the wrong way can leak the shape of your data, let an attacker alter a message without being noticed, or fall apart completely when one number is reused. The cipher's name on the box, AES or ChaCha20, tells you less than how it is being driven. This guide assumes you know what a key and a symmetric cipher are. If not, Encryption and Hashing in Plain Terms covers that in a few minutes.

A wartime Lorenz cipher machine in a museum case, a black metal frame holding a long row of notched metal wheels with numbered rims, with motors and a dial mounted on topThe twelve wheels of a Lorenz SZ42, a wartime machine that enciphered teleprinter messages as they were typed, one character after another. Photo by Farai Gandiya, CC0, via Wikimedia Commons.

What is a block?

A block is a fixed number of bits that the cipher handles as one unit. For the Advanced Encryption Standard (AES), the block is 128 bits, which is 16 bytes, about the length of the phrase "PAY BOB $100 NOW". That size is fixed by the standard, FIPS 197, whatever the key length. AES-128, AES-192 and AES-256 differ in their key size and in how many times they stir the block (10, 12 and 14 rounds), not in the block itself.

Think of the cipher as a machine with one slot. Put in exactly 16 bytes and a key, and 16 different bytes come out. Put the output back in with the same key, in reverse, and the original returns. Change one bit of the input and roughly half the output bits change.

That is all a block cipher does. It knows nothing about messages longer than one block. A 5 megabyte photo is 312,500 blocks. A 1,000 byte email is 62 and a half, and the machine does not take half blocks. Something has to decide how the blocks are fed through, what happens to the leftover, and whether one block affects the next.

What is a mode of operation, and why does it matter so much?

That something is the mode of operation: the set of rules for using a one-block machine on data of any length. The US National Institute of Standards and Technology (NIST) published the classic list in December 2001 as SP 800-38A. It defines five modes. Three are worth knowing by name: ECB, CBC and CTR.

The mode decides most of the security properties a user actually experiences. The same AES, with the same key, can be nearly useless in one mode and excellent in another. When you read "AES-256-GCM" or "AES-128-CBC" in a settings page, the last part is the mode.

ModeHow blocks are handledNeeds padding?Main weakness
ECBEach block aloneYesIdentical blocks show through
CBCEach block mixed with the previous oneYesNo tamper detection, awkward to get right
CTRA counter is encrypted to make keystreamNoFails badly if a counter value repeats

Why does ECB leak patterns?

ECB stands for Electronic Codebook, and it is the mode you get by doing the obvious thing: cut the data into blocks and encrypt each one separately.

The flaw follows directly. NIST's own document says it without decoration: "under a given key, any given plaintext block always gets encrypted to the same ciphertext block." Identical input blocks give identical output blocks, so the pattern of repeats in the original survives encryption.

A diagram in two halves. The top half shows ECB mode, where every block is encrypted alone. Three plaintext blocks read PAY BOB 100 dollars, PAY BOB 100 dollars again, and PAY EVE 900 dollars. Each passes through AES with the same key separately. The first two ciphertext blocks come out identical, outlined in red, with the note that the same block in gives the same block out, so the repeat is visible. The bottom half shows counter mode. Three counter values, the nonce followed by 1, 2 and 3, each pass through AES to make keystream that is XORed with the plaintext. All three ciphertext blocks are different, outlined in green, with the note that the same plaintext twice gives two unrelated ciphertexts. The byte values shown are made up for illustration.ECB turns equal blocks into equal blocks. Counter mode feeds every block a different number, so repeats vanish.

For example, picture a payroll file where each record fills one block and the salary field can be one of a few pay grades. An attacker who cannot read a single record can still sort the staff into groups with equal pay, and if they know one person's grade, they know everyone's in that group. Or picture a simple image, large areas of one color with a shape in the middle. Every all-white block encrypts to the same thing, every all-black block to another, and the outline of the shape is still there in the "encrypted" file.

It gets worse. Because blocks are independent, an attacker can cut, copy and reorder them. Swap the block holding "PAY EVE $900" into the place of another payment and the result still decrypts cleanly.

As a rule of thumb, ECB is never the right choice for anything longer than one block. If a product's documentation mentions it, ask why.

CBC: what does chaining the blocks fix?

CBC is Cipher Block Chaining. Before each block goes into the machine, it is mixed with the ciphertext of the block before it. The first block has no predecessor, so it is mixed with a random starting value called an initialization vector, or IV. NIST's rule for CBC is that the IV "must be unpredictable."

Now two identical plaintext blocks give different ciphertext, because each was mixed with something different first. The pattern leak is gone. For many years CBC was a common default, and plenty of stored data and older network gear still uses it.

It has three drawbacks.

  • Padding. The data must fill whole blocks, so the last one is topped up. That 1,000 byte email becomes 63 blocks, 1,008 bytes. Padding that is checked carelessly can leak information to an attacker.
  • No parallel encryption. Each block waits for the one before it, which wastes modern multi-core processors.
  • No tamper detection. CBC hides data. It does not notice if someone changes the ciphertext. A separate integrity check has to be bolted on, and that is easy to get wrong.

This is why TLS 1.3, the protocol behind HTTPS, dropped CBC. Its specification keeps only ciphers that encrypt and authenticate in one step.

CTR: how does a block cipher become a stream?

CTR is Counter mode, and it uses the block cipher in an unexpected way: the message never goes through the machine at all.

The sender takes a starting value used only once, called a nonce (short for "number used once"), and attaches a counter: 1, 2, 3 and so on. Each nonce-and-counter value is encrypted with the key. The outputs, strung together, form a long run of unpredictable bits called the keystream. The keystream is then combined with the message using XOR.

XOR is the simplest operation in computing. Compare two bits: if they differ, write 1, and if they match, write 0. So 0110 XOR 1010 is 1100. Its useful property is that doing it twice with the same value undoes it: 1100 XOR 1010 gives back 0110. The receiver generates the same keystream from the same key and nonce, XORs again, and the message reappears.

Counter mode fixes CBC's practical problems. There is no padding, because you use only as much keystream as you need. Every block can be computed at once on separate processor cores. And you can decrypt the middle of a large file without touching the start.

It has one unforgiving rule and one gap.

Watch out

A counter value must never be used twice with the same key. If it is, two messages are XORed with the same keystream, and an attacker who XORs the two ciphertexts together cancels the keystream out and is left with the two messages blended, which is often enough to recover both.

The gap is the same one CBC has: no tamper detection. In fact it is sharper here. NIST notes that counter mode ciphertext is "malleable": flip a bit of the ciphertext, and exactly that bit of the decrypted message flips. Closing that gap is the whole purpose of the mode covered in Galois/Counter Mode: Encryption That Notices Tampering.

What is a stream cipher, then?

A stream cipher skips the block machine and is built from the start to do what counter mode does: turn a key and a nonce into keystream, then XOR.

The one that matters today is ChaCha20. Its designer, Daniel J. Bernstein, introduced the ChaCha family in a paper dated January 2008, and the version used on the internet is specified in RFC 8439 from June 2018. The numbers:

  • A 256-bit key.
  • A 96-bit nonce and a 32-bit counter.
  • 20 rounds of simple operations: add, rotate, XOR.
  • Keystream produced 64 bytes at a time.

Because it uses only operations every processor does quickly, ChaCha20 is fast without special hardware. RFC 8439 puts it at "around three times as fast" as AES on platforms that lack built-in AES support. The same document sets a limit: one key and nonce can cover about 256 gigabytes, after which the counter would repeat. And it repeats the rule from the warning above. Reuse a nonce with the same key and the result "reveals the XOR of the plaintexts."

Stream ciphers have a mixed history. An older one called RC4 was once a standard choice for web traffic. It was formally banned from every version of TLS by RFC 7465 in February 2015. ChaCha20 is a much later and more conservative design. On the web it never travels alone: it is always paired with a tamper check called Poly1305.

So the line between the two families is blurrier than the names suggest. AES in counter mode behaves like a stream cipher, and ChaCha20 internally produces its keystream in 64 byte chunks. What separates them in practice is how they are built and where they run fastest.

Where is each one used?

WhereWhat does the workKind
HTTPS on a laptop or serverAES in Galois/Counter ModeBlock cipher in a counter-based mode
HTTPS on a device without AES hardwareChaCha20-Poly1305Stream cipher
A phone's or laptop's storageAES in XTS modeBlock cipher in a mode built for drives
Older VPNs, file formats and databasesAES in CBC modeBlock cipher, chained

Storage gets its own mode because a drive has unusual needs: any sector must be readable and writable alone, and the encrypted data cannot be larger than the original. NIST approved XTS for that job in SP 800-38E.

What should you do with this? For a family, almost nothing, and that is good news. Browsers and phones pick sound modes automatically, and software updates are how old ones get retired, so keep updates on. A company has more to check. Its developers and vendors make these choices in code and configuration, and the old options are still there to be picked by accident. A sensible review asks three questions of every system that encrypts: is ECB used anywhere, is any CBC use paired with a proper integrity check, and is RC4 switched off everywhere?

What to remember

  • A block cipher scrambles one fixed-size block. AES blocks are 128 bits whatever the key length.
  • The mode of operation decides how a block cipher handles real data, and it matters as much as the cipher.
  • ECB encrypts equal blocks to equal blocks, so patterns show through. Avoid it.
  • CBC hides patterns but needs padding and cannot detect tampering.
  • Counter mode and stream ciphers make keystream and XOR it with the data. They are fast and flexible, and they fail badly if a nonce or counter repeats under the same key.
  • ChaCha20 is the stream cipher in common use, and it is fastest where AES hardware is missing.

The thread running through every section is that hiding data and protecting it from changes are different jobs. The modes here do only the first. The next guide in this set, Galois/Counter Mode: Encryption That Notices Tampering, shows how the second job is done, and Modern Encryption: A Map of What Protects You Today shows where all of these pieces sit in the systems you use.