Course contentsShow
← Security
Lesson 98 of 3,0523. Symmetric Cryptography Deep DiveFree lesson

CTR Mode: Turning Block Ciphers into Streams

Learn how Counter mode generates a keystream and provides parallelizable encryption without padding.

CTR Mode: Turning Block Ciphers into Streams

What you'll learn: How Counter (CTR) mode transforms a block cipher into a stream cipher by encrypting counters to generate a keystream.

The Core Idea

Instead of encrypting your plaintext directly, CTR mode encrypts a counter value that increments for each block. The encrypted counter becomes a keystream that you XOR with your plaintext. Think of it like generating a one-time pad on demand: the block cipher produces pseudo-random bits, and you mix them with your message.

How It Works

  1. Start with a nonce (number used once) and a counter (often starting at 0)
  2. Encrypt nonce || counter using your block cipher (like AES)
  3. XOR the encrypted counter with your plaintext block
  4. Increment the counter and repeat for the next block

The recipient uses the same nonce and counter sequence, encrypts the same counters, and XORs with the ciphertext to recover plaintext.

Key Advantages

No padding required: Since you're XORing, you can encrypt any length message—just use only the keystream bytes you need.

Parallelizable: Unlike CBC mode (which chains blocks), each block's counter can be encrypted independently. You can encrypt block 1, block 100, and block 1000 simultaneously because they don't depend on each other. This makes CTR mode extremely fast on modern multi-core processors.

Random access: Need to decrypt just one block in the middle? Compute that counter's encryption and decrypt instantly—no need to process previous blocks.

Critical Security Note

Never reuse a nonce with the same key. Reusing a nonce creates identical keystreams, allowing attackers to XOR two ciphertexts together and cancel out the keystream, exposing plaintext relationships.

Key Takeaway: CTR mode converts block ciphers into stream ciphers by encrypting sequential counters, enabling parallel encryption without padding, but requires unique nonces for every message.