.NET AES returns wrong Test Vectors

Posted by ralu on Stack Overflow See other posts from Stack Overflow or by ralu
Published on 2010-05-09T21:57:36Z Indexed on 2010/05/09 22:48 UTC
Read the original article Hit count: 233

Filed under:
|

I need to implement some crypto protocol on C# and want to say that this is my first project in C#. After spending some time to get used on C# I found out that I am unable to get compliant AES vectors.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        public static void Main()
        {
            try
            {
                //test vectors from "ecb_vk.txt"
                byte[] key =     { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 
                byte[] data =    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
                byte[] encTest = { 0x0e, 0xdd, 0x33, 0xd3, 0xc6, 0x21, 0xe5, 0x46, 0x45, 0x5b, 0xd8, 0xba, 0x14, 0x18, 0xbe, 0xc8 };

                AesManaged aesAlg = new AesManaged();
                aesAlg.BlockSize = 128;
                aesAlg.Key = key;
                aesAlg.Mode = CipherMode.ECB;
                ICryptoTransform encryptor = aesAlg.CreateEncryptor();

                MemoryStream msEncrypt = new MemoryStream();
                CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
                StreamWriter swEncrypt = new StreamWriter(csEncrypt);
                swEncrypt.Write(data);

                swEncrypt.Close();
                csEncrypt.Close();
                msEncrypt.Close();
                aesAlg.Clear();

                byte[] encr;
                encr = msEncrypt.ToArray();
                string datastr    = BitConverter.ToString(data);
                string encrstr    = BitConverter.ToString(encr);
                string encTestStr = BitConverter.ToString(encTest);

                Console.WriteLine("data:   {0}", datastr);
                Console.WriteLine("encr:   {0}", encrstr);
                Console.WriteLine("should: {0}", encTestStr);              
                Console.ReadKey();

            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
        }
    }
}

Output is wrong:

data:   00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
encr:   A0-3C-C2-22-A4-32-F7-C9-BA-36-AE-73-66-BD-BB-A3
should: 0E-DD-33-D3-C6-21-E5-46-45-5B-D8-BA-14-18-BE-C8

I am sure that there is a correct AES implementation in .NET, so I need some advice from a .NET wizard to help with this.

© Stack Overflow or respective owner

Related posts about c#

Related posts about aes