Programming tutorials

Symmetric block ciphers

There are two main types of symmetric encryption algorithms: block ciphers and stream ciphers. The discussion so far applies to both, but for the rest of this chapter we will talk about block ciphers.

A block cipher is a type of cipher which operates on a fixed size block of data. For example, Rijndael can operate on blocks of 128 bits (16 bytes). The cipher uses a secret key, which is also 16 bytes for Rijndael. To encrypt an 16 byte message, you feed the message and the key into the encryption algorithm, and the result is 16 bytes of ciphertext. To decrypt, you feed the ciphertext, together with the same key, into the decryption algorithm, and you get back the original message.


Of course, most useful messages are much larger than 16 bytes. In that case, you need to process the message 16 bytes at a time, using the same key to process each block. Also note that it is impossible to encrypt a block which is smaller than the block size - if the final block of the message is not exactly the right size you must add [[Block padding methods|padding]] bytes.

There are many different algorithms, and they use different block and key sizes. It is common for modern algorithms to use sizes of 128 or 256 for blocks and keys. It is not necessary for the block size to be the same as the key size (however, the ciphertext block is always the same size as the plaintext block).

Block ciphers are the most popular type of symmetric encryption, because they are efficient and meet the needs of most applications. Stream ciphers tend to be used only for more specialised applications.