Crypt::Blowfish Verification with Test Vectors

A tutorial Perl example is provided to show how to verify the Crypt::Blowfish module with standard Blowfish 16-round algorithm test vectors.

From previous tutorials, we have learned that Crypt::Blowfish only performs encryption and decryption on a single cipher 64-bit block and a 64-bit key. In this tutorial, we will focus on the accuracy of the encryption algorithm using the standard test vectors provided earlier in this book.

Here is my sample Perl script, Crypt-Blowfish-Test-Vector.pl, that can be used to verify Crypt::Blowfish against a given test vector:

```#- Crypt-Blowfish-Test-Vector.pl
#-
use Crypt::Blowfish;

my (\$keyHex, \$plainHex, \$expectedHex) = @ARGV;
\$keyHex = "0000000000000000" unless \$keyHex;
\$plainHex = "0000000000000000" unless \$plainHex;
\$expectedHex = "4EF997456198DD78" unless \$expectedHex;

\$keyStr = pack("H16", \$keyHex);
\$plainStr = pack("H16", \$plainHex);
\$cipher = new Crypt::Blowfish(\$keyStr);
\$cipherStr = \$cipher->encrypt(\$plainStr);
\$cipherHex = unpack("H16", \$cipherStr);

\$cipher = new Crypt::Blowfish(\$keyStr);
\$decryptStr = \$cipher->decrypt(\$cipherStr);
\$decryptHex = unpack("H16", \$decryptStr);

print("Crypt::Blowfish Test Vector:\n");
print("   Key in Hex:           (\$keyHex)\n");
print("   Ciphertext in Hex:    (\$cipherHex)\n");
print("   Expected text in Hex  (\$expectedHex)\n");
print("   Decrypted text in Hex (\$decryptHex)\n");
print("   Plaintext in Hex      (\$plainHex)\n");
```

Let's run it with some Blowfish test vectors:

```C:\herong>perl Blowfish-Perl-Block-Test.pl \
0000000000000000   0000000000000000   4EF997456198DD78

Crypt::Blowfish Test Vector:
Key in Hex:           (0000000000000000)
Ciphertext in Hex:    (4ef997456198dd78)
Expected text in Hex  (4EF997456198DD78)
Decrypted text in Hex (0000000000000000)
Plaintext in Hex      (0000000000000000)

C:\herong>perl Blowfish-Perl-Block-Test.pl \
FFFFFFFFFFFFFFFF   FFFFFFFFFFFFFFFF   51866FD5B85ECB8A

Crypt::Blowfish Test Vector:
Key in Hex:           (FFFFFFFFFFFFFFFF)
Ciphertext in Hex:    (51866fd5b85ecb8a)
Expected text in Hex  (51866FD5B85ECB8A)
Decrypted text in Hex (ffffffffffffffff)
Plaintext in Hex      (FFFFFFFFFFFFFFFF)

C:\herong>perl Blowfish-Perl-Block-Test.pl \
0123456789ABCDEF   1111111111111111   61F9C3802281B096

Crypt::Blowfish Test Vector:
Key in Hex:           (0123456789ABCDEF)
Ciphertext in Hex:    (61f9c3802281b096)
Expected text in Hex  (61F9C3802281B096)
Decrypted text in Hex (1111111111111111)
Plaintext in Hex      (1111111111111111)

C:\herong>perl Blowfish-Perl-Block-Test.pl \
FEDCBA9876543210   0123456789ABCDEF   0ACEAB0FC6A0A28D

Crypt::Blowfish Test Vector:
Key in Hex:           (FEDCBA9876543210)
Ciphertext in Hex:    (0aceab0fc6a0a28d)
Expected text in Hex  (0ACEAB0FC6A0A28D)
Decrypted text in Hex (0123456789abcdef)
Plaintext in Hex      (0123456789ABCDEF)

C:\herong>perl Blowfish-Perl-Block-Test.pl \
7CA110454A1A6E57   01A1D6D039776742   59C68245EB05282B

Crypt::Blowfish Test Vector:
Key in Hex:           (7CA110454A1A6E57)
Ciphertext in Hex:    (59c68245eb05282b)
Expected text in Hex  (59C68245EB05282B)
Decrypted text in Hex (01a1d6d039776742)
Plaintext in Hex      (01A1D6D039776742)
```

All 5 test vectors are verified correctly. We can now assume that the Crypt::Blowfish module developed by A.M. Kuchling is a correct implementation of Blowfish 16-round algorithm.

Exercise: Verify Crypt::Blowfish with more test vectors.

Last update: 2018.