PHP Mcrypt Blowfish Block Cipher

This section provides a tutorial example on how to write Blowfish block cipher by calling the PHP Mcrypt function, mcrypt_encrypt() in ECB mode. Each input data block will be encrypted independently.

If we call the PHP Mcrypt mcrypt_encrypt() function with Blowfish algorithm in ECB mode, we are actually performing blowfish encryption on input data blocks one at a time independently.

So I wrote a simple Blowfish block cipher as listed blow:

#- Blowfish-Block-Cipher.php
#- Copyright (c) 2018, All Rights Reserved.

   $key = hex2bin('0000000000000000');
   $cleartext = hex2bin('0000000000000000');
   $ciphertext = hex2bin('4ef997456198dd78');
   if (count($argv)>1) $key = hex2bin($argv[1]);
   if (count($argv)>2) $cleartext = hex2bin($argv[2]);
   if (count($argv)>3) $ciphertext = hex2bin($argv[3]);

   // 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, MCRYPT_MODE_ECB);
   $validation = $result==$ciphertext;
   print("key bytes        clear bytes      cipher bytes\n");
   print(bin2hex($key).' '.bin2hex($cleartext).' '.bin2hex($ciphertext)
   print('Test Result: '.bin2hex($result)."\n");
   print('Validation: '.$validation."\n");

Before running any tests, let me check the version of my PHP installation:

C:\herong>php -version

PHP 7.0.2 (cli) (built: Jan  6 2016 13:05:11) ( ZTS )
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies

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

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

key bytes        clear bytes      cipher bytes
0000000000000000 0000000000000000 4ef997456198dd78
Test Result: 4ef997456198dd78
Validation: 1

Test 2: Run it again with another test vector. The result should match the expected ciphertext:

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

key bytes        clear bytes      cipher bytes
ffffffffffffffff ffffffffffffffff 51866fd5b85ecb8a
Test Result: 51866fd5b85ecb8a
Validation: 1

Test 3: Run it with 2 repeating blocks. The result should show 2 repeating ciphertext blocks, because ECB cipher mode does not chain clocks:

C:\herong>php Blowfish-Block-Cipher.php FFFFFFFFFFFFFFFF \
   FFFFFFFFFFFFFFFFffffffffffffffff \
key bytes        clear bytes      cipher bytes
ffffffffffffffff ffffffffffffffffffffffffffffffff 
Test Result: 51866fd5b85ecb8a51866fd5b85ecb8a
Validation: 1

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


 Full Version in PDF/EPUB