Posts Tagged ‘3DES’

Question:
Does the Crypt2 Component support 3DES ??

Setting “CryptAlgorithm” to “3des” or “des” does not change the encoded
output when the “EncryptStringENC” method is used.

Answer:
Yes. The CryptAlgorithm should be set to “DES” (case-insensitive). The KeyLength property determines if it is DES (56 or 64), 3DES (112 or 128) or 2-Key TDES (168 or 192). Each byte in a DES key has a parity bit. An 8-byte key (64 bits) has 8 parity bits and 56 encryption bits. The Chilkat Crypt component will accept either number. In other words, to get Triple-DES (3DES) you may set the key length to either 168 or 192.

This post provides C# 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


C# Code:

    string keyAscii = "1234567890123456ABCDEFGH";
    byte[] key = ASCIIEncoding.ASCII.GetBytes(keyAscii);

    byte[] clearText = ASCIIEncoding.ASCII.GetBytes("The quick brown fox jumped over the lazy dog");

    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    des.KeySize = 192;
    des.Key = key;
    des.Mode = CipherMode.ECB;
    des.Padding = PaddingMode.Zeros;
    byte[] cipherText = des.CreateEncryptor().TransformFinalBlock(clearText, 0, clearText.Length);

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

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