Crypt::CBC Prepending IV to Ciphertext

A tutorial Perl example is provided to show how to use Crypt::CBC to prepend the IV value to the ciphertext, if IV is specified by the user. This saves the effort to pass the IV to receiver separately from the ciphertext.

When using Crypt::CBC with the "Literal Key" or "Crypt::Blowfish Object" option, we need to pass the IV value together with the ciphertext to the receiver, so he/she can decrypt the ciphertext with the secret key.

Passing the IV value each time we send a ciphertext in not very convenient, so Crypt::CBC offers the feature to prepend the IV to the ciphertext string. Here is how it can be done, using the "Crypt::Blowfish Object" option as an example:

To help us understanding the -header => 'randomiv' parameter, I wrote the following example Perl script, Crypt-CBC-Blowfish-Prepend-IV.pl:

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

   print("Crypt::CBC Prepend IV 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 => 'randomiv',
      -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");

If you run this example script with different IV values and see what blocks are prepended to the ciphertext:

C:\herong>perl Crypt-CBC-Blowfish-Prepend-IV.pl \
   1122334455667788 0000000000000000 0123456789abcdef
   
Crypt::CBC Prepend IV Test - output in Hex:
   Secret Key     (1122334455667788)
   IV             (0000000000000000)
   Plaintext      (0123456789abcdef)
Creating Crypt::CBC with Blowfish Object ...
Encrypting plaintext...
   Ciphertext     (52616e646f6d49560000000000000000103248f0eea98e89)
Decrypting ciphertext...
   Decrypted text (0123456789abcdef)
   Pass Phrase    ()
   IV             (0000000000000000)
   
C:\herong>perl Crypt-CBC-Blowfish-Prepend-IV.pl \
   1122334455667788 1111111111111111 0123456789abcdef
   
Crypt::CBC Prepend IV Test - output in Hex:
   Secret Key     (1122334455667788)
   IV             (1111111111111111)
   Plaintext      (0123456789abcdef)
Creating Crypt::CBC with Blowfish Object ...
Encrypting plaintext...
   Ciphertext     (52616e646f6d49561111111111111111c3a27e1f67d146f0)
Decrypting ciphertext...
   Decrypted text (0123456789abcdef)
   Pass Phrase    ()
   IV             (1111111111111111)

The output confirms that our understanding is correct:

Exercise: Write a Perl script to use Crypt::CBC Literal Key option with -header => 'randomiv'.

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