CFB (Cipher Feedback)
CFB, or Cipher Feedback mode, is a mode of operation used with block ciphers to turn them into a stream cipher. It allows a block cipher like AES (Advanced Encryption Standard) to encrypt data of arbitrary length, and not just data that fits into a block. CFB mode is used in various cryptographic applications for secure data transmission and storage.
CFB mode begins with an IV, which should be a unique value for each encryption operation to ensure security. The IV is usually the size of the block cipher's block size.
- The IV is encrypted with the block cipher.
- The output of this encryption is then XORed (exclusive OR) with a segment of the plaintext to produce the ciphertext segment.
- For the next block, a segment of the previously generated ciphertext is used (rather than the IV) and encrypted with the block cipher. This output is again XORed with the next segment of plaintext to produce the next segment of ciphertext.
- This process continues until all plaintext is encrypted.
The same process is used for decryption, with the ciphertext being fed back into the cipher after each block is processed. The IV is encrypted, then XORed with the first segment of ciphertext to recover the first segment of plaintext. Subsequent segments of ciphertext are encrypted and XORed with the next segment of ciphertext to produce the next segment of plaintext.
CFB turns a block cipher into a stream cipher, making it suitable for encrypting data of any size. If a few bits of ciphertext are lost in transmission, decryption can resynchronize after the loss. However, the bits following the error up to the next block boundary will be corrupted. An error in a ciphertext block affects the decryption of this block and the next block; after that, the decryption process synchronizes correctly.
Since CFB mode can process data in segments smaller than the block size, it does not require padding the plaintext to a multiple of the block size. CFB mode is useful in scenarios where data is streaming or the total data size is not known upfront. It's also used when error propagation is a desirable property, such as in some secure communication protocols.
An example:
# Pseudo-code example using a block cipher in CFB mode
cipher = BlockCipher(key, mode=CFB, iv=initialization_vector)
# Encrypt
ciphertext = cipher.encrypt(plaintext)
# Decrypt
decrypted_text = cipher.decrypt(ciphertext)