tags:

views:

45

answers:

2

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
            {

                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;
                aesAlg.Padding = PaddingMode.None;
                ICryptoTransform encryptor = aesAlg.CreateEncryptor();

                MemoryStream msEncrypt = new MemoryStream();
                CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
                csEncrypt.Write(data, 0,data.Length);

                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.

EDIT

I just found out. There has to PaddingMode.None and then works. I pasted working code. Thanks anyway.

A: 

You're missing an initialization vector - without specifying one the IV is randomly generated and thus the results will vary each time you create a new instance of the AES classes.

blowdart
there is no IV in ECB mode http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
ralu
Ad doh indeed, didn't notice the ECB setting.
blowdart
A: 

Try decrypting; it's possible the Encrypt and Decrypt methods are swapped (this is sometimes ambiguous in crypto algorithm specifications).

Andrew McGregor