Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component


Index of Chilkat Blog Posts

May 15, 2008

PHP Blowfish Encryption

This code snippet demonstrates blowfish encryption in PHP. Blowfish Encryption PHP Code Snippet.

<?php
	
	$cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
		
	// The block-size of the Blowfish algorithm is 64-bits, therefore our IV
	// is always 8 bytes:
	$iv =  '12345678';
	
	$key256 = '1234567890123456ABCDEFGHIJKLMNOP';
	$key128 = '1234567890123456';
	
	printf("iv: %s\n",bin2hex($iv));
	printf("key256: %s\n",bin2hex($key256));
	printf("key128: %s\n",bin2hex($key128));
	
	$cleartext = 'The quick brown fox jumped over the lazy dog';
	printf("clearText: %s\n\n",$cleartext);
		
	// Do 256-bit blowfish encryption:
	// The strengh of the encryption is determined by the length of the key
	// passed to mcrypt_generic_init
	if (mcrypt_generic_init($cipher, $key256, $iv) != -1)
	{
		// PHP pads with NULL bytes if $cleartext is not a multiple of the block size..
		$cipherText = mcrypt_generic($cipher,$cleartext );
		mcrypt_generic_deinit($cipher);
		
		// Display the result in hex.
		printf("256-bit blowfish encrypted:\n%s\n\n",bin2hex($cipherText));
	}
	
	// 128-bit blowfish encryption:
	if (mcrypt_generic_init($cipher, $key128, $iv) != -1)
	{
		// PHP pads with NULL bytes if $cleartext is not a multiple of the block size..
		$cipherText = mcrypt_generic($cipher,$cleartext );
		mcrypt_generic_deinit($cipher);
		
		// Display the result in hex.
		printf("128-bit blowfish encrypted:\n%s\n\n",bin2hex($cipherText));
	}
	
	// -------
	// Results
	// -------
	// You may use these as test vectors for testing your Blowfish implementations...
	// 
	// iv: 3132333435363738
	// key256: 313233343536373839303132333435364142434445464748494a4b4c4d4e4f50
	// key128: 31323334353637383930313233343536
	// clearText: The quick brown fox jumped over the lazy dog
	// 
	// 256-bit blowfish encrypted:
	// 276855ca6c0d60f7d9708210440c1072e05d078e733b34b4198d609dc2fcc2f0c30926cdef3b6d52baf6e345aa03f83e
	// 
	// 128-bit blowfish encrypted:
	// d2b5abb73208aea3790621d028afcc74d8dd65fb9ea8e666444a72523f5ecca60df79a424e2c714fa6efbafcc40bdca0	
	
?>
	
	

ASP: Blowfish Encryption to Match PHP's Mcrypt Extension
SQL Server: Blowfish Encryption to Match PHP's Mcrypt Extension
C#: Blowfish Encryption to Match PHP's Mcrypt Extension
C++: Blowfish Encryption to Match PHP's Mcrypt Extension
MFC: Blowfish Encryption to Match PHP's Mcrypt Extension
C: Blowfish Encryption to Match PHP's Mcrypt Extension
Delphi: Blowfish Encryption to Match PHP's Mcrypt Extension
Visual FoxPro: Blowfish Encryption to Match PHP's Mcrypt Extension
Java: Blowfish Encryption to Match PHP's Mcrypt Extension
Perl: Blowfish Encryption to Match PHP's Mcrypt Extension
Python: Blowfish Encryption to Match PHP's Mcrypt Extension
Ruby: Blowfish Encryption to Match PHP's Mcrypt Extension
VB.NET: Blowfish Encryption to Match PHP's Mcrypt Extension
Visual Basic: Blowfish Encryption to Match PHP's Mcrypt Extension
VBScript: Blowfish Encryption to Match PHP's Mcrypt Extension


Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to support@chilkatsoft.com

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.