Crypt::CFB Not Requiring Padding

A tutorial example is provided to demonstrate that Crypt::CFB encryption does not require any padding to plaintext. The last XOR operation can be performed on a partial block.

In the previous tutorial, we figured out what is the default IV value used by the Crypt::CFB module. In this tutorial, we will try to see how it does the padding.

To do this, I wrote the follow example script that allows us to run Crypt::CFB encryption with any given secret key, IV and ciphertext:

#- Crypt-CFB-Blowfish-Encryption.pl
#- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
   use Crypt::CFB;
   my ($keyHex, $ivHex, $plainHex) = @ARGV;

   print("Crypt::CFB Encryption Test - output in Hex:\n");
   $keyHex = "1122334455667788" unless $keyHex;
   $ivHex = "1111111111111111" unless $ivHex;
   $plainHex = "0123456789abcdef" unless $plainHex;
   $keyStr = pack("H*", $keyHex);
   $ivStr = pack("H*", $ivHex);
   $plainStr = pack("H*", $plainHex);
   print("   Secret Key     ($keyHex)\n");
   print("   IV             ($ivHex)\n");
   print("   Plaintext      ($plainHex)\n");
      
   print("Creating Crypt::CFB with secret key ...\n");
   $cipher = Crypt::CFB->new($keyStr, 'Crypt::Blowfish', $ivStr);

   print("Encrypting plaintext...\n");
   $cipherStr = $cipher->encrypt($plainStr);
   $cipherHex = unpack("H*", $cipherStr);
   print("   Ciphertext     ($cipherHex)\n");

   print("Decrypting ciphertext...\n");
   $cipher->reset();
   $decryptStr = $cipher->decrypt($cipherStr);
   $decryptHex = unpack("H*", $decryptStr);
   print("   Decrypted text ($decryptHex)\n");

When running Crypt-CFB-Blowfish-Encryption.pl with a full block of plaintext, I got a full block of ciphertext as shown below. So no padding was added by Crypt::CFB.

C:\herong>perl Crypt-CFB-Blowfish-Encryption.pl  
   1122334455667788 0000000000000000 0123456789abcdef

Crypt::CFB Encryption Test - output in Hex:
   Secret Key     (1122334455667788)
   IV             (0000000000000000)
   Plaintext      (0123456789abcdef)
Creating Crypt::CFB with secret key ...
Encrypting plaintext...
   Ciphertext     (c7de2d3fe72ea7ab)
Decrypting ciphertext...
   Decrypted text (0123456789abcdef)

When running Crypt-CFB-Blowfish-Encryption.pl with a half block of plaintext, I got a half block of ciphertext as shown below. So no padding was added by Crypt::CFB.

C:\herong>perl Crypt-CFB-Blowfish-Encryption.pl 
   1122334455667788 0000000000000000 01234567

Crypt::CFB Encryption Test - output in Hex:
   Secret Key     (1122334455667788)
   IV             (0000000000000000)
   Plaintext      (01234567)
Creating Crypt::CFB with secret key ...
Encrypting plaintext...
   Ciphertext     (c7de2d3f)
Decrypting ciphertext...
   Decrypted text (01234567)

Conclusion: Crypt::CFB implements CFB operation mode without padding. This is doable, because the last operation of the CFB algorithm is an XOR operation, which can be performed in a partial block of plaintext. So no need to pad the plaintext to a full block.

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

 What is Crypt::CFB?

 Installing Crypt::CFB 0.02 with ActivePerl

 Crypt::CFB with Default IV

Crypt::CFB Not Requiring Padding

 Crypt::CFB Operation Simulation

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

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

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

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

 References

 PDF Printing Version