Mycrypt Blowfish Block Chaining Cipher

This section provides a tutorial example on how to write Blowfish block chaining cipher by calling the PHP Mcrypt function, mcrypt_encrypt() in a given operation mode and IV (Initial Vector0).

If we want to perform Blowfish block chaining cipher in CBC, CFB or OFB operation modes, we need to call the PHP Mcrypt mcrypt_encrypt() function with the IV argument.

Here is my simple Blowfish block chaining cipher in PHP:

<?php
#- Blowfish-Block-Chaining-Cipher.php
#- Copyright (c) 2018 HerongYang.com, All Rights Reserved.

   $key = hex2bin('0000000000000000');
   $cleartext = hex2bin('4EF997456198DD78E1C030E74C14D261');
   $mode = "cbc";
   $iv = hex2bin('0000000000000000');
   $ciphertext = hex2bin('e1c030e74c14d2614ef997456198dd78');

   if (count($argv)>1) $key = hex2bin($argv[1]);
   if (count($argv)>2) $cleartext = hex2bin($argv[2]);
   if (count($argv)>3) $mode = $argv[3];
   if (count($argv)>4) $iv = hex2bin($argv[4]);
   if (count($argv)>5) $ciphertext = hex2bin($argv[5]);

   // mcrypt_ecb() was DEPRECATED in PHP 5.5.0, and REMOVED in PHP 7.0.0 
   // $result = 
   //    mcrypt_ecb(MCRYPT_BLOWFISH, $key, $cleartext, MCRYPT_ENCRYPT); 
   
   // mcrypt_encrypt() has been DEPRECATED as of PHP 7.1.0
   $result = 
      mcrypt_encrypt(MCRYPT_BLOWFISH, $key, $cleartext, $mode, $iv);

   $validation = $result==$ciphertext;
   print("Secret Key: ".bin2hex($key)." + IV: ".bin2hex($iv)."\n");
   print("Plaintext: ".bin2hex($cleartext)."\n");
   print("Oper. Mode: ".$mode."\n");
   print("Ciphertext: ".bin2hex($ciphertext)."\n");
   print("PHP Result: ".bin2hex($result)."\n");
   print("Validation: ".$validation."\n");
?>

Test 1: Run it without argument. The default test will be performed in CBC mode. The result should match the expected ciphertext:

C:\herong>php Blowfish-Block-Chaining-Cipher.php

Secret Key: 0000000000000000 + IV: 0000000000000000
Plaintext: 4ef997456198dd78e1c030e74c14d261
Oper. Mode: cbc
Ciphertext: e1c030e74c14d2614ef997456198dd78
PHP Result: e1c030e74c14d2614ef997456198dd78
Validation: 1

Test 2: Run a CFB 2-block test vector provided earlier in the book.

C:\herong>php Blowfish-Block-Chaining-Cipher.php \
   0000000000000000 E1C030E74C14D2614EF997456198DD78 cfb \
   0000000000000000 af39a7a22d8c0f19ce94e96f7a22ac91
   
Secret Key: 0000000000000000 + IV: 0000000000000000
Plaintext: e1c030e74c14d2614ef997456198dd78
Oper. Mode: cfb
Ciphertext: af39a7a22d8c0f19ce94e96f7a22ac91
PHP Result: af3c08c3657ab473564bf52dc19d78ac
Validation:

Surprisingly, the result of CFB mode does not match the expected ciphertext! I need to do some research in the next tutorial.

Test 3: Run an OFB 2-block test vector provided earlier in the book.

C:\herong>php Blowfish-Block-Chaining-Cipher.php \
   0000000000000000 E1C030E74C14D2614EF997456198DD78 ofb \
   0000000000000000 af39a7a22d8c0f19af39a7a22d8c0f19

Secret Key: 0000000000000000 + IV: 0000000000000000
Plaintext: e1c030e74c14d2614ef997456198dd78
Oper. Mode: ofb
Ciphertext: af39a7a22d8c0f19af39a7a22d8c0f19
PHP Result: af08be64e49a94be35829afefd3d699b
Validation:

Surprisingly, the result of OFB mode also does not match the expected ciphertext! I need to do some research in the next tutorial.

Table of Contents

 About This Book

 Blowfish Cipher Algorithm

 Perl Crypt::Blowfish Module

 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

 What is PHP Mcrypt Extension

 PHP Mcrypt Blowfish Block Cipher

Mycrypt Blowfish Block Chaining Cipher

 "ncfb/nofb" for Block Chaining Ciphers

 Performing CFB Operation Manually

 php_blowfish.php - PHP Blowfish Demo

 Blowfish 8-Bit Cipher in PHP

 References

 Full Version in PDF/EPUB