"bf-cfb" Cipher with Literal Key

A tutorial example is provided to show you how to use the 'bf-cfb' (Blowfish in CFB mode) cipher with Literal Key to encrypt and decrypt binary data files.

So far we have learned both "enc -bf-ecb" and "enc -bf-cbc" commands to perform Blowfish encryption in ECB (Electronic CodeBook) and CBC (Cipher Block Chaining) operation modes. Let's move on to the "enc -bf-cfb" command for Blowfish encryption in CFB (Cipher FeedBack) operation mode.

In case you forgot how CFB (Cipher FeedBack) works, here a shortest version of the CFB algorithm:

Input:
   P: The plaintext in multiple blocks
   K: The secret key
   IV: The Initialization Vector
   E(K,B): The block encryption function 

Output:
   C: The ciphertext in multiple blocks

Algorithm - CFB (Cipher FeedBack) Operation Mode: 
   (P[1], P[2], P[3},...) = P    : Split plaintext into blocks

   C[1] = P[1] XOR E(K, IV)
   Loop i over 2,3,...
      C[i] = P[i] XOR E(K, C[i-1])
   End Loop

   C = (C[1], C[2], C[3},...)    : Concatenate ciphertext blocks

The CFB algorithm can also be illustrated by this simple diagram:

Algorithm - CFB (Cipher FeedBack) Operation Mode: 

IV ---->|        ---->|        ---->| 
      E(K)     /    E(K)     /    E(K)
        |     /       |     /       |
        |    /        |    /        | 
 P[1]--XOR  /  P[2]--XOR  /  P[3]--XOR
        |  /          |  /          | 
        | /           | /           | 
      C[1]          C[2]          C[3] ...

Comparing with the ECB mode we have discussed earlier, the CFB mode does use the IV (Initialization Vector). So we need to pay attention to how to control the IV with the OpenSSL "enc -bf-cfb" command.

Similar to ECB mode tutorials, let's follow the "Literal Key" way first by running the OpenSSL "enc -bf-cfb" command with "-K" and "-iv" options. In the example below, I am trying to perform encryption on a full block of 0x00:

C:\herong>
   perl -e "binmode(STDOUT); print pack('H*', '0000000000000000')" 
   > 0000000000000000.txt

C:\herong>\local\gnuwin32\bin\openssl enc -bf-cfb -e 
   -K 00000000000000000000000000000000 -iv 0000000000000000 
   -in 0000000000000000.txt -out cipher.txt -nopad -p
salt=0200000028264901
key=00000000000000000000000000000000
iv =0000000000000000

C:\herong>perl -e "while (read(STDIN,$_,1)){print unpack('H*',$_);}" 
   < cipher.txt
4ef997456198dd78

C:\herong>\local\gnuwin32\bin\openssl enc -bf-cfb -d 
   -K 00000000000000000000000000000000 -iv 0000000000000000 
   -in cipher.txt -out decrypted.txt -nopad -p
salt=0200000000265C01
key=00000000000000000000000000000000
iv =0000000000000000

C:\herong>perl -e "while (read(STDIN,$_,1)){print unpack('H*',$_);}" 
   < decrypted.txt
0000000000000000

Pretty cool, I have successfully encrypted a block of binary zeros with "bf-cfb" cipher, and decrypted it back.

The ciphertext, 0x4ef997456198dd78, is correct, because it matches the test vector published at https://www.schneier.com/code/vectors.txt. Not that for a single plaintext block, CFB operation mode will just run Blowfish encryption on the IV, then XOR with the plaintext.

key bytes               clear bytes             cipher bytes
0000000000000000        0000000000000000        4EF997456198DD78

Last update: 2015.

Table of Contents

 About This Book

 Blowfish Cipher Algorithm

 Perl Crypt::Blowfish Module

 Perl Crypt::ECB Perl Module

 Perl Crypt::CBC Module

 Perl Crypt::CFB Perl Module

 OpenSSL "enc -bf-ecb" for Blowfish/ECB Encryption

 OpenSSL "enc -bf-cbc" for Blowfish/CBC Encryption

OpenSSL "enc -bf-cfb" for Blowfish/CFB Encryption

"bf-cfb" Cipher with Literal Key

 "bf-cfb" Cipher on Multiple Blocks

 "bf-cfb" Encryption Verification

 "bf-cfb" 2-Block Test Vectors

 "bf-cfb" Cipher with Salted Key

 "bf-cfb" Cipher with Random Salt

 "enc -bf-cfb" Command Summary

 OpenSSL "enc -bf-ofb" for Blowfish/OFB Encryption

 References

 PDF Printing Version