Crypt::CBC Encryption with Crypt::Blowfish Objects

A tutorial Perl example is provided to show how to use Crypt::CBC to perform encryption with a predefined Crypt::Blowfish a give IV value, which allows you to specify secret keys in the range of 8 bytes (64 bits) and 56 bytes (448 bits).

From previous tutorials, we learned how to use Crypt::CBC with Crypt::Blowfish with the Literal Key option, which allows us to specify the secret key and the IV directly with the Literal Key option.

However the Literal Key option has a big limitation. That is you can only specify a secret key with exactly 56 bytes (448 bits). But the actual cipher module, Crypt::Blowfish does support secret keys shorter than 56 bytes. This leads us to look at the second option, using Crypt::Blowfish object with Crypt::CBC.

According to the Crypt::CBC manual page, we need to provide the following parameters when calling the new() method to use the user specified Crypt::Blowfish object and IV option:

To help us understanding the Literal Key option, I wrote the following example Perl script, Crypt-CBC-Blowfish-Cipher-Object.pl:

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

   print("Crypt::CBC Blowfish Object Test - output in Hex:\n");
   $keyHex = "1122334455667788" unless $keyHex;
   $ivHex = "0000000000000000" 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::CBC with Blowfish Object ...\n");
   $blowfish = new Crypt::Blowfish($keyStr);
   $cipher = Crypt::CBC->new( 
      -cipher => $blowfish,
      -iv => $ivStr,
      -header => 'none',
      -padding => 'none'
      );

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

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

   $passStr = $cipher->passphrase();
   $passHex = unpack("H*", $passStr);
   print("   Pass Phrase    ($passHex)\n");

   $ivStr = $cipher->iv();
   $ivHex = unpack("H*", $ivStr);
   print("   IV             ($ivHex)\n");

Now try this example script with different size secret keys:

C:\Herong>perl Crypt-CBC-Blowfish-Cipher-Object.pl \
   1122334455667788 0000000000000000 0123456789abcdef
   
Crypt::CBC Blowfish Object Test - output in Hex:
   Secret Key     (1122334455667788)
   IV             (0000000000000000)
   Plaintext      (0123456789abcdef)
Creating Crypt::CBC with Blowfish Object ...
Encrypting plaintext...
   Ciphertext     (103248f0eea98e89)
Decrypting ciphertext...
   Decrypted text (0123456789abcdef)
   Pass Phrase    ()
   IV             (0000000000000000)

C:\Herong>perl Crypt-CBC-Blowfish-Cipher-Object.pl \
   11223344556677889900112233445566 0000000000000000 0123456789abcdef
   
Crypt::CBC Blowfish Object Test - output in Hex:
   Secret Key     (11223344556677889900112233445566)
   IV             (0000000000000000)
   Plaintext      (0123456789abcdef)
Creating Crypt::CBC with Blowfish Object ...
Encrypting plaintext...
   Ciphertext     (22f7ed41b986a09a)
Decrypting ciphertext...
   Decrypted text (0123456789abcdef)
   Pass Phrase    ()
   IV             (0000000000000000)

The output confirms that:

Table of Contents

 About This Book

 Blowfish Cipher Algorithm

 Perl Crypt::Blowfish Module

 Perl Crypt::ECB Perl Module

Perl Crypt::CBC Module

 What is Crypt::CBC

 Installing Crypt::CBC 2.33 with ActivePerl

 Crypt::CBC Encryption with Literal Keys

 Crypt::CBC Literal Key Error Cases

Crypt::CBC Encryption with Crypt::Blowfish Objects

 Crypt::CBC Operation Simulation

 Crypt::CBC Encryption Verification

 Blowfish CBC 2-Block Test Vectors

 Crypt::CBC Prepending IV to Ciphertext

 Crypt::CBC Encryption with Salted Keys

 Crypt::CBC Salted Key Test Cases

 Crypt::CBC Secret Key and IV Algorithm

 Crypt::CBC Encryption with Random Salt

 Crypt::CBC Padding Options

 Crypt::CBC Padding Option Tests

 Crypt::CBC Blowfish Encryption Summary

 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

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

 PHP Mcrypt Extension for Blowfish

 Blowfish 8-Bit Cipher in PHP

 References

 Full Version in PDF/EPUB