Crypt::CBC Encryption with Literal Keys

A tutorial Perl example is provided to show how to use Crypt::CBC to perform encryption with a literal key and an IV specified by the user.

By reading the CBC (Cipher Block Chaining) algorithm, we know that it needs 3 pieces of information to perform encryption:

By reading the Crypt::CBC manual page, I see several options to manage the secret key and the IV:

The first option, user specified secret key and IV, is easier to understand, so let's try it first.

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

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

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

   print("Crypt::CBC Literal Key Test - output in Hex:\n");
   $keyHex = "1122334455667788990011223344556677889900".
             "1122334455667788990011223344556677889900".
             "11223344556677889900112233445566"
             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 Literal Key...\n");
   $cipher = Crypt::CBC->new( 
         -cipher => $algorithm,
         -literal_key => 1,
         -key => $keyStr,
         -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");

If you run this example script 2 times in a command window, you will get:

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

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

The output seems to be correct:

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

 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

 References

 PDF Printing Version