Skip to content
ECB and CBC Modes
step 1/5

Reading — step 1 of 5

Read

~1 min readModes of Operation

ECB and CBC Modes

AES encrypts 16 bytes at a time. To encrypt longer messages, use a mode of operation.

ECB (Electronic Codebook): simplest. Encrypt each block independently.

ciphertext = AES(P0) || AES(P1) || AES(P2) || ...

Don't use ECB. Identical plaintext blocks produce identical ciphertext blocks → patterns leak. The famous Linux penguin image encrypted in ECB looks like... a penguin.

CBC (Cipher Block Chaining): each block XOR'd with the previous ciphertext before encrypting:

C0 = AES(P0 XOR IV)
C1 = AES(P1 XOR C0)
C2 = AES(P2 XOR C1)
...

The IV (Initialization Vector) must be unpredictable (random). Same plaintext + different IV = different ciphertext. CBC IV is sent alongside ciphertext (no need to keep secret).

CBC's downsides:

  • Sequential encrypt (can't parallelize)
  • Padding required for short messages (PKCS#7)
  • Padding oracle attacks if not authenticated (use HMAC or AEAD)
python

For new code: use AEAD modes (GCM, ChaCha20-Poly1305). They authenticate AND encrypt in one operation.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…