Crypt::CBC Operation Simulation

A tutorial Perl example is provided to show how to use Crypt::Blowfish module to simulate the CBC operation mode. The simulation result matches the result from the Crypt::CBC module.

So far, we have learned how to use Crypt::Blowfish to encrypt a single block of plaintext. We have also learned how to use Crypt::CBC to encrypt blocks of plaintext with direct control of secret key and IV. We should be able to verify Crypt::CBC encryption results by simulating the CBC algorithm with Crypt::CBC module.

Here is my example Perl script, Crypt-CBC-Blowfish-Simulation.pl, that simulates the CBC operation by calling Crypt::Blowfish one block at time. It also verifies the ciphertext by calling the Crypt::CBC module directly.

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

   print("Crypt::CBC Blowfish Simulation - 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::Blowfish Object ...\n");
   $blowfish = new Crypt::Blowfish($keyStr);

   $count = 0;
   $cipherHexPrev = $ivHex;
   while ($plainHex) {
      $count++;
      $plainHexBlock = substr($plainHex,0,16);
      $plainHex = substr($plainHex,16);
      print("Simulating CBC on block $count...\n");
      print("   Prev ciphertext ($cipherHexPrev)\n");
      print("   Plaintext       ($plainHexBlock)\n");
      
      $cipherStrPrev = pack("H*", $cipherHexPrev);
      $plainStrBlock = pack("H*", $plainHexBlock);
      $xorStrBlock = $cipherStrPrev ^ $plainStrBlock;
      $xorHexBlock = unpack("H*", $xorStrBlock);
      print("   Plaintext XORed ($xorHexBlock)\n");
      
      $cipherStrBlock = $blowfish->encrypt($xorStrBlock);
      $cipherHexBlock = unpack("H*", $cipherStrBlock);
      print("   Ciphertext      ($cipherHexBlock)\n");

      $cipherHexPrev = $cipherHexBlock;
   }

   print("Creating Crypt::CBC Object ...\n");
   $blowfish = new Crypt::Blowfish($keyStr);
   $cipher = Crypt::CBC->new( 
      -cipher => $blowfish,
      -iv => $ivStr,
      -header => 'none',
      -padding => 'none'
      );

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

The key part of the CBC mode simulation is the following logics used in CBC mode:

Now try this example script with different size plaintext:

C:\Herong>perl Crypt-CBC-Blowfish-Simulation.pl \
   1122334455667788 0000000000000000 0123456789abcdef
   
Crypt::CBC Blowfish Simulation - output in Hex:
   Secret Key      (1122334455667788)
   IV              (0000000000000000)
   Plaintext       (0123456789abcdef)
Creating Crypt::Blowfish Object ...
Simulating CBC on block 1...
   Prev ciphertext (0000000000000000)
   Plaintext       (0123456789abcdef)
   Plaintext XORed (0123456789abcdef)
   Ciphertext      (103248f0eea98e89)
Creating Crypt::CBC Object ...
Encrypting entire plaintext with Crypt::CBC...
   Ciphertext      (103248f0eea98e89)
   
C:\Herong>perl Crypt-CBC-Blowfish-Simulation.pl 1122334455667788 \
 0000000000000000 0123456789abcdef0123456789abcdef
 
Crypt::CBC Blowfish Simulation - output in Hex:
   Secret Key      (1122334455667788)
   IV              (0000000000000000)
   Plaintext       (0123456789abcdef0123456789abcdef)
Creating Crypt::Blowfish Object ...
Simulating CBC on block 1...
   Prev ciphertext (0000000000000000)
   Plaintext       (0123456789abcdef)
   Plaintext XORed (0123456789abcdef)
   Ciphertext      (103248f0eea98e89)
Simulating CBC on block 2...
   Prev ciphertext (103248f0eea98e89)
   Plaintext       (0123456789abcdef)
   Plaintext XORed (11110d9767024366)
   Ciphertext      (8f5e452a60506163)
Creating Crypt::CBC Object ...
Encrypting entire plaintext with Crypt::CBC...
   Ciphertext      (103248f0eea98e898f5e452a60506163)

C:\Herong>perl Crypt-CBC-Blowfish-Simulation.pl \
   1122334455667788 1111111111111111 
   
   0123456789abcdef0123456789abcdef0123456789abcdef
Crypt::CBC Blowfish Simulation - output in Hex:
   Secret Key      (1122334455667788)
   IV              (1111111111111111)
   Plaintext       (0123456789abcdef0123456789abcdef0123456789abcdef)
Creating Crypt::Blowfish Object ...
Simulating CBC on block 1...
   Prev ciphertext (1111111111111111)
   Plaintext       (0123456789abcdef)
   Plaintext XORed (1032547698badcfe)
   Ciphertext      (c3a27e1f67d146f0)
Simulating CBC on block 2...
   Prev ciphertext (c3a27e1f67d146f0)
   Plaintext       (0123456789abcdef)
   Plaintext XORed (c2813b78ee7a8b1f)
   Ciphertext      (dd89a07b06cad2aa)
Simulating CBC on block 3...
   Prev ciphertext (dd89a07b06cad2aa)
   Plaintext       (0123456789abcdef)
   Plaintext XORed (dcaae51c8f611f45)
   Ciphertext      (2cd3a609b55defca)
Creating Crypt::CBC Object ...
Encrypting entire plaintext with Crypt::CBC...
   Ciphertext      (c3a27e1f67d146f0dd89a07b06cad2aa2cd3a609b55defca)

C:\Herong>perl Crypt-CBC-Blowfish-Simulation.pl \
   1122334455667788 ffffffffffffffff \
   0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
   
Crypt::CBC Blowfish Simulation - output in Hex:
   Secret Key      (1122334455667788)
   IV              (ffffffffffffffff)
   Plaintext       (0123456789abcdef0123456789abcdef
                    0123456789abcdef0123456789abcdef)
Creating Crypt::Blowfish Object ...
Simulating CBC on block 1...
   Prev ciphertext (ffffffffffffffff)
   Plaintext       (0123456789abcdef)
   Plaintext XORed (fedcba9876543210)
   Ciphertext      (2b41e1b29adaa3ab)
Simulating CBC on block 2...
   Prev ciphertext (2b41e1b29adaa3ab)
   Plaintext       (0123456789abcdef)
   Plaintext XORed (2a62a4d513716e44)
   Ciphertext      (2149531ce57439d0)
Simulating CBC on block 3...
   Prev ciphertext (2149531ce57439d0)
   Plaintext       (0123456789abcdef)
   Plaintext XORed (206a167b6cdff43f)
   Ciphertext      (54061f6cddc380e2)
Simulating CBC on block 4...
   Prev ciphertext (54061f6cddc380e2)
   Plaintext       (0123456789abcdef)
   Plaintext XORed (55255a0b54684d0d)
   Ciphertext      (713fa4974566fdfd)
Creating Crypt::CBC Object ...
Encrypting entire plaintext with Crypt::CBC...
   Ciphertext      (2b41e1b29adaa3ab2149531ce57439d0
                    54061f6cddc380e2713fa4974566fdfd)

The output looks good. The ciphertext blocks produced from the simulation logic match well with the Crypt::CBC module in all 4 tests.

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