Block cipher

A block cipher is a symmetric encryption algorithm that works on data in fixed-size pieces called blocks. With a secret key it turns each block of plaintext into a block of ciphertext of the same size. The same key turns it back. AES is the best-known block cipher today.

Block size and key size

The block size is fixed by the algorithm. AES, Camellia and ARIA use 128-bit blocks (16 bytes). Older ciphers such as DES, Triple DES, Blowfish, CAST5 and IDEA use 64-bit blocks (8 bytes). The key size is a separate number: AES works with 128, 192 or 256-bit keys, but its blocks are always 128 bits.

Block size matters for security. With 64-bit blocks, repeated ciphertext blocks become likely after about 32 GB of data encrypted with one key, and they leak information about the plaintext. The Sweet32 attack (2016) used this against Triple DES and Blowfish in TLS and OpenVPN. For new systems, use a cipher with 128-bit blocks such as AES.

Modes of operation

A block cipher on its own only encrypts a single block. A mode of operation says how to encrypt a longer message:

  • ECB encrypts every block on its own. Identical plaintext blocks give identical ciphertext blocks, so patterns in the data stay visible. Avoid it.
  • CBC mixes each block with the previous ciphertext block before encrypting it. It needs an initialization vector (IV) for the first block.
  • CTR, CFB and OFB use the block cipher to produce a keystream and XOR it with the data. The block cipher then works like a stream cipher.
  • GCM, CCM and OCB encrypt and add an authentication tag that shows whether the ciphertext was changed (authenticated encryption).

Padding

ECB and CBC can only encrypt whole blocks. If the last block is short, padding fills it up. PHP's openssl_encrypt uses PKCS#7 padding by default. That is why hello world (11 bytes) encrypted with AES-256-CBC gives 16 bytes, which is 24 characters in Base64. With AES-256-CTR no padding is needed, and the result is as long as the input: 11 bytes, or 16 Base64 characters.

On this site

The encryption tools on this site work like openssl_encrypt without an IV, so the same text and secret always give the same result. That makes results easy to compare with your own code. Real applications should use a random IV for every message, or a mode with authentication such as AES-GCM.

Searches the list of pages on this site. What you type stays in your browser.