C# 3DES (Triple-DES) Test Vector

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

PHP Three-Key Triple-DES (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

C# Blowfish Test Vectors

C# to match the Blowfish test vectors at this URL: http://www.schneier.com/code/vectors.txt

    // keys
    string[] key = new string[] { 
        "0000000000000000",
        "FFFFFFFFFFFFFFFF",
        "3000000000000000",
        "1111111111111111",
        "0123456789ABCDEF",
        "1111111111111111",
        "0000000000000000",
        "FEDCBA9876543210",
        "7CA110454A1A6E57",
        "0131D9619DC1376E",
        "07A1133E4A0B2686",
        "3849674C2602319E",
        "04B915BA43FEB5B6",
        "0113B970FD34F2CE",
        "0170F175468FB5E6",
        "43297FAD38E373FE",
        "07A7137045DA2A16",
        "04689104C2FD3B2F",
        "37D06BB516CB7546",
        "1F08260D1AC2465E",
        "584023641ABA6176",
        "025816164629B007",
        "49793EBC79B3258F",
        "4FB05E1515AB73A7",
        "49E95D6D4CA229BF",
        "018310DC409B26D6",
        "1C587F1C13924FEF",
        "0101010101010101",
        "1F1F1F1F0E0E0E0E",
        "E0FEE0FEF1FEF1FE",
        "0000000000000000",
        "FFFFFFFFFFFFFFFF",
        "0123456789ABCDEF",
        "FEDCBA9876543210" };

    string[] plainText = new string[] { 
        "0000000000000000",
        "FFFFFFFFFFFFFFFF",
        "1000000000000001",
        "1111111111111111",
        "1111111111111111",
        "0123456789ABCDEF",
        "0000000000000000",
        "0123456789ABCDEF",
        "01A1D6D039776742",
        "5CD54CA83DEF57DA",
        "0248D43806F67172",
        "51454B582DDF440A",
        "42FD443059577FA2",
        "059B5E0851CF143A",
        "0756D8E0774761D2",
        "762514B829BF486A",
        "3BDD119049372802",
        "26955F6835AF609A",
        "164D5E404F275232",
        "6B056E18759F5CCA",
        "004BD6EF09176062",
        "480D39006EE762F2",
        "437540C8698F3CFA",
        "072D43A077075292",
        "02FE55778117F12A",
        "1D9D5C5018F728C2",
        "305532286D6F295A",
        "0123456789ABCDEF",
        "0123456789ABCDEF",
        "0123456789ABCDEF",
        "FFFFFFFFFFFFFFFF",
        "0000000000000000",
        "0000000000000000",
        "FFFFFFFFFFFFFFFF" };

    string[] cipherBytes = new string[] { 
        "4EF997456198DD78",
        "51866FD5B85ECB8A",
        "7D856F9A613063F2",
        "2466DD878B963C9D",
        "61F9C3802281B096",
        "7D0CC630AFDA1EC7",
        "4EF997456198DD78",
        "0ACEAB0FC6A0A28D",
        "59C68245EB05282B",
        "B1B8CC0B250F09A0",
        "1730E5778BEA1DA4",
        "A25E7856CF2651EB",
        "353882B109CE8F1A",
        "48F4D0884C379918",
        "432193B78951FC98",
        "13F04154D69D1AE5",
        "2EEDDA93FFD39C79",
        "D887E0393C2DA6E3",
        "5F99D04F5B163969",
        "4A057A3B24D3977B",
        "452031C1E4FADA8E",
        "7555AE39F59B87BD",
        "53C55F9CB49FC019",
        "7A8E7BFA937E89A3",
        "CF9C5D7A4986ADB5",
        "D1ABB290658BC778",
        "55CB3774D13EF201",
        "FA34EC4847B268B2",
        "A790795108EA3CAE",
        "C39E072D9FAC631D",
        "014933E0CDAFF6E4",
        "F21E9A77B71C49BC",
        "245946885754369A",
        "6B5C5A9C5D9E0A5A" };

    Chilkat.Crypt2 crypt = new Chilkat.Crypt2();

    bool success;
    success = crypt.UnlockComponent("Anything for 30-day trial");
    if (success != true)
    {
        MessageBox.Show("Crypt component unlock failed");
        return;
    }

    //  Attention: use "blowfish2" for the algorithm name:
    crypt.CryptAlgorithm = "blowfish2";

    //  CipherMode may be "ecb", "cbc", or "cfb"
    crypt.CipherMode = "ecb";

    //  KeyLength (in bits) may be a number between 32 and 448.
    //  128-bits is usually sufficient.  The KeyLength must be a
    //  multiple of 8.
    crypt.KeyLength = 64;

    //  The padding scheme determines the contents of the bytes
    //  that are added to pad the result to a multiple of the
    //  encryption algorithm's block size.  Blowfish has a block
    //  size of 8 bytes, so encrypted output is always
    //  a multiple of 8.
    crypt.PaddingScheme = 3;

    //  EncodingMode specifies the encoding of the output for
    //  encryption, and the input for decryption.
    //  It may be "hex", "url", "base64", or "quoted-printable".
    crypt.EncodingMode = "hex";


    int numTests = key.Length;
    int i;

    for (i = 0; i < numTests; i++)
    {
        crypt.SetEncodedKey(key[i], "hex");

        string encStr = crypt.EncryptEncoded(plainText[i]);
        textBox1.Text += encStr + " should equal " + cipherBytes[i] + "\r\n";
    }