Crypt::Blowfish Behavior Tests

A tutorial Perl example is provided to show how to verify behaviors of Crypt::Blowfish module on different sizes of secret keys and plaintext messages.

With Crypt::Blowfish module installed and working, we should now write a couple of test scripts to confirm our understanding on how the module works. Here is my test script called, Crypt-Blowfish-Test.pl:

#- Crypt-Blowfish-Test.pl
#- Copyright (c) 2015 HerongYang.com, All Rights Reserved.
#- 
   use Crypt::Blowfish;

   my ($keyHex, $plainHex) = @ARGV;
   print("Crypt::Blowfish Test - output in Hex:\n");
   $keyHex = "1122334455667788" unless $keyHex;
   $plainHex = "0123456789abcdef" unless $plainHex;
   $keyStr = pack("H*", $keyHex);
   $plainStr = pack("H*", $plainHex);
   print("   Key in Hex            ($keyHex)\n");
   print("   Plaintext in Hex      ($plainHex)\n");

   $cipher = new Crypt::Blowfish($keyStr);
   print("   keysize()             ".$cipher->keysize()." bytes\n");
   print("   blocksize()           ".$cipher->blocksize()." bytes\n");

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

   $cipher = new Crypt::Blowfish($keyStr);
   $decryptStr = $cipher->decrypt($cipherStr);
   $decryptHex = unpack("H*", $decryptStr);
   print("   Decrypted text in Hex ($decryptHex)\n");

As you can see, my test script allows you to specify a secret key and plaintext in any size. It will perform encryption on the plaintext, print the ciphertext, then perform decryption the ciphertext back to plaintext.

First, let's run some tests to see how many number of bytes we can specify for the secret key:

C:\herong>perl Crypt-Blowfish-Test.pl 11223344 0123456789abcdef

Crypt::Blowfish Test - output in Hex:
   Key in Hex            (11223344)
   Plaintext in Hex      (0123456789abcdef)
Invalid length key at C:/local/Perl/site/lib/Crypt/Blowfish.pm line 42


C:\herong>perl Crypt-Blowfish-Test.pl 1122334455667788 \
   0123456789abcdef

Crypt::Blowfish Test - output in Hex:
   Key in Hex            (1122334455667788)
   Plaintext in Hex      (0123456789abcdef)
   keysize()             0 bytes
   blocksize()           8 bytes
   Ciphertext in Hex     (103248f0eea98e89)
   Decrypted text in Hex (0123456789abcdef)


C:\herong>perl Crypt-Blowfish-Test.pl 11223344556677889900112233445566 \
   0123456789abcdef
   
Crypt::Blowfish Test - output in Hex:
   Key in Hex            (11223344556677889900112233445566)
   Plaintext in Hex      (0123456789abcdef)
   keysize()             0 bytes
   blocksize()           8 bytes
   Ciphertext in Hex     (22f7ed41b986a09a)
   Decrypted text in Hex (0123456789abcdef)
   
  
C:\herong>perl Crypt-Blowfish-Test.pl \
   11223344556677889900...11223345566 0123456789abcdef

Crypt::Blowfish Test - output in Hex:
   Key in Hex            (1122334455667788990011223344556677889900
                          1122334455667788990011223344556677889900
                          11223344556677889900112233445566)
   Plaintext in Hex      (0123456789abcdef)
   keysize()             0 bytes
   blocksize()           8 bytes
   Ciphertext in Hex     (c52f3b26ae7dfd39)
   Decrypted text in Hex (0123456789abcdef)
   
   
C:\herong>perl Crypt-Blowfish-Test.pl \
   11223344556677889900...1122334556677 0123456789abcdef

Crypt::Blowfish Test - output in Hex:
   Key in Hex            (1122334455667788990011223344556677889900
                          1122334455667788990011223344556677889900
                          1122334455667788990011223344556677)
   Plaintext in Hex      (0123456789abcdef)
Invalid length key at C:/local/Perl/site/lib/Crypt/Blowfish.pm line 42.

The test result confirms that Crypt::CBC requires the secret key to be in the range of 8 bytes (64 bits) and 56 bytes (448 bits). I don't understand why Crypt::CBC only supports secret key up to 56 bytes only. The Blowfish algorithm can support up to 72 bytes long keys.

Now let's run some tests see how many number of bytes we can specify for the plaintext:

C:\herong>perl Crypt-Blowfish-Test.pl 1122334455667788 0123456789a

Crypt::Blowfish Test - output in Hex:
   Key in Hex            (1122334455667788)
   Plaintext in Hex      (0123456789a)
   keysize()             0 bytes
   blocksize()           8 bytes
input must be 8 bytes long at C:/local/Perl/site/lib/Crypt/Blowfish.pm
line 50.


C:\herong>perl Crypt-Blowfish-Test.pl 1122334455667788 \
   0123456789abcdef

Crypt::Blowfish Test - output in Hex:
   Key in Hex            (1122334455667788)
   Plaintext in Hex      (0123456789abcdef)
   keysize()             0 bytes
   blocksize()           8 bytes
   Ciphertext in Hex     (103248f0eea98e89)
   Decrypted text in Hex (0123456789abcdef)

   
C:\herong>perl Crypt-Blowfish-Test.pl 1122334455667788 \
   0123456789abcde01234567

Crypt::Blowfish Test - output in Hex:
   Key in Hex            (1122334455667788)
   Plaintext in Hex      (0123456789abcde01234567)
   keysize()             0 bytes
   blocksize()           8 bytes
input must be 8 bytes long at C:/local/Perl/site/lib/Crypt/Blowfish.pm
line 50.

As expected, the plaintext must be 8 bytes (64 bits) long, since the Blowfish is a 65-bit block cipher.

Table of Contents

 About This Book

 Blowfish Cipher Algorithm

Perl Crypt::Blowfish Module

 What Is Crypt::Blowfish

 Installing Crypt::Blowfish 2.14 with ActivePerl

Crypt::Blowfish Behavior Tests

 Secret Keys with Repeating Pattern

 Crypt::Blowfish Verification with Test Vectors

 Blowfish Test Vectors with 16-Byte Keys

 Perl Crypt::ECB Perl Module

 Perl Crypt::CBC Module

 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