In this tutorial, you will learn the basic concepts of block cipher modes of operation. After reading this tutorial, you will understand Cipher Modes, Electronic Code Book Mode (ECB), Cipher Block Chaining Mode (CBC), Cipher Feedback Mode (CFB), Stream Cipher Mode, and Counter Mode (CTR).

**Contents: **

- Cipher Modes
- Electronic Code Book Mode (ECB)
- Cipher Block Chaining Mode (CBC)
- Cipher Feedback Mode (CFB)
- Stream Cipher Mode
- Counter Mode (CTR)

## Cipher Modes

Cipher modes, also known as block cipher modes of operation, are methods that apply block ciphers to encrypt and decrypt data. Block ciphers operate on fixed-size blocks (typically 64 or 128 bits) of plaintext, producing corresponding blocks of ciphertext, but many practical applications require encryption of data streams of arbitrary length. Cipher modes enable the use of block ciphers in these scenarios, enhancing security and versatility.

There are total five modes of ciphers.

- Electronic Code Book Mode (ECB)
- Cipher Block Chaining Mode
- Cipher Feedback Mode
- Stream Cipher Mode
- Counter Mode

## Electronic Code Book Mode (ECB)

In ECB, large plaintext is divided into consecutive 64 bit or 8 bytes blocks, and the same key is used to encrypt them one after the other. This is known as Electronic Code Book Mode (ECB).

The diagram below explains the electronic codebook mode.

- As shown in the figure, the plain text is divided into 64-bit blocks, and all the blocks are encrypted using the same key.
- Here in ECB, the term codebook is used because there is a unique ciphertext for each B bit of the plaintext block for a given key. Therefore, we can imagine a huge codebook, in which for every possible B bit plaintext pattern, an entry represents its corresponding ciphertext.
- The disadvantage of this mode is that if two plaintext blocks are similar, their ciphertext will also be the same. To overcome this problem, we use cipher block chaining mode.
- ECB mode of decryption is the opposite of encryption.

## Cipher Block Chaining Mode (CBC)

We can use CBC mode to overcome ECB problems. In CBC, the XOR operation is performed between the plaintext block and the previous ciphertext block that has been encrypted. In short, we are creating a chain of all block ciphers. Hence, it is known as cipher block chaining.

The figure below explains the working of CBC mode.

- As shown in the figure, each plaintext block is performing an XOR operation with its previous encrypted plaintext, the ciphertext.
- But here the first plaintext block has no previous ciphertext, so the randomly chosen initialization vector is XOR with the plaintext.
- Decryption is the reverse process of encryption, as shown in the figure.
- The advantage of CBC is that no two plaintexts have the same ciphertext, as in an ECB block. This method frustrates the attacker to guess the plaintext.

## Cipher Feedback Mode (CFB)

The disadvantage of cipher block chaining is that the entire 64-bit block must be reached before the decryption process can begin. Cipher Feedback Mode solves this problem, as it does not use any decryption algorithm, it only decrypts the ciphertext using an encryption algorithm.

The diagram below explains the cipher feedback mode.

- As shown in the figure, the initial vector of B bits is encrypted with the key and the encryption algorithm. Now, select the s bits from the result and discard the b-s bits. The s bits plaintext and the s bits result is XORed to produce a ciphertext.
- The ciphertext-1 goes to the shift register of block-2. The B-S bits are selected for encryption and S bits for operations. The same process is repeated for all blocks.
- The reverse process is performed to decrypt the ciphertext.
- Basically, this algorithm is used to convert block ciphers to stream ciphers and eliminate padding operations.

## Stream Cipher Mode

Stream cipher mode is also known as output feedback mode. It may be possible that one-bit changes during transmission may result in a different ciphertext. To avoid this problem, we can use output feedback mode.

The diagram below explains the stream cipher mode.

- As shown in the figure, the algorithm starts by encrypting an initialization vector using the key and the encryption algorithm.
- The generated output serves as an input for the next block. For the first block, the output is XORed with the plaintext and generates a ciphertext.
- Note that, if a bit error occurs in plaintext-1, it only affects ciphertext-1, no other ciphertexts. In short, if there is an error in the plaintext, it only affects its corresponding block, not subsequent blocks.
- The decryption process in stream cipher mode is the opposite of the encryption process, as shown in the figure.
- Do not use the same key and initialization vector pair twice in a stream cipher, in doing so, it will generate the same keystream. If we use the same keystream twice, it exposes the ciphertext to a
**keystream reuse attack**.

## Counter Mode (CTR)

In previous all modes, all modes except ECB mode have problem of random access to the encrypted data. For example, in CBC mode, if you want to access block-50 then you have to first decrypt all the blocks ahead of block-50. It becomes expensive and that’s why counter mode invented.

In this mode, each block has one initialization vector or counter. The plaintext is not encrypted directly, the initialization vector + constant is first encrypted, and result is XOR with plaintext to generate a ciphertext.

The below diagram explains the counter mode.

- As shown in the figure, the initialization vector is there in each block with stepping the initialization vector by 1 for each new block.
- The initialization vector + constant is encrypted using key and encryption algorithm. The result is XORed with the plaintext and generates a ciphertext.
- To decrypt a random block of ciphertext is easy because we can decrypt the block without decrypting all of its predecessor’s blocks.
- The size of the plaintext is equals to the size of the counter or initialization vector.
- It is simple, hardware efficient, and software efficient algorithm.

## Key Points to Remember

Here is the list of key points we need to remember about “Block Cipher Modes of Operation”.

- Electronic Code Book Mode, Cipher Block Chaining Mode, Cipher Feedback Mode, Stream Cipher Mode, and Counter Mode are the five types of cipher modes.
- In ECB, large plaintext is divided into consecutive 64 bit or 8 bytes blocks, and the same key is used to encrypt them one after the other. This is known as Electronic Code Book Mode (ECB).
- In Cipher Block Chaining mode, the XOR operation is performed between the plaintext block and the previous ciphertext block that has been encrypted.
- Cipher Feedback Mode, Stream Cipher Mode, and Counter Mode are used to convert the block cipher into stream cipher and to eliminate the padding operations.

**If you find any mistake above, kindly email to [email protected]**