May 22, 2008

PHP Three-Key Triple-DES (3DES) Test Vector

Filed under: 3DES, PHP — Tags: , , , — admin @ 1:14 pm

This post provides PHP sample code for matching a test vector (known answer test).

3DES Settings:

  • ECB Mode
  • 192-bit key (i.e. 3 8-bit keys)
  • ASCII Key Bytes: 1234567890123456ABCDEFGH
  • ASCII Text to Encrypt: The quick brown fox jumped over the lazy dog
  • Pads with zero bytes
  • Hexadecimalized Encrypted Result:
    13d4d3549493d2870f93c3e0812a06de467e1f9c0bfb16c0
    70ede5cabbd3ca62f217a7ae8d47f2c7bf62eb309323b58b


PHP Code:


<?php

	$cipher = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, '');

	// The IV is ignored for ECB mode.
	$iv =  '12345678';

	$key192 = '1234567890123456ABCDEFGH';
	printf("key192: %s\n",bin2hex($key192));

	$cleartext = 'The quick brown fox jumped over the lazy dog';
	printf("clearText: %s\n\n",$cleartext);

	if (mcrypt_generic_init($cipher, $key192, $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("3DES encrypted:\n%s\n\n",bin2hex($cipherText));
	}

?>

3DES Code to match this test vector in other languages:
ASP: 3DES Test Vector
SQL Server: 3DES Test Vector
C#: 3DES Test Vector
C++: 3DES Test Vector
MFC: 3DES Test Vector
C: 3DES Test Vector
Delphi: 3DES Test Vector
Visual FoxPro: 3DES Test Vector
Java: 3DES Test Vector
Perl: 3DES Test Vector
PHP: 3DES Test Vector
Python: 3DES Test Vector
Ruby: 3DES Test Vector
VB.NET: 3DES Test Vector
Visual Basic: 3DES Test Vector
VBScript: 3DES Test Vector

TrackBack URL