title: C# AES Encryption Byte Array
body: <p>I want to encrypt byte array. So first I try it in <a href="http://extranet.cryptomathic.com/aescalc/index?key=00000000000000000000000000000000&amp;iv=00000000000000000000000000000000&amp;input=1EA0353A7D2947D8BBC6AD6FB52FCA84&amp;mode=cbc&amp;action=Encrypt&amp;output=1EA0353A7D2947D8BBC6AD6FB52FCA84" rel="noreferrer">this site</a>.</p>
  
  <ul>
  <li><strong>key</strong> = 00000000000000000000000000000000 </li>
  <li><strong>IV</strong>  = 00000000000000000000000000000000 </li>
  <li><strong>Input Data</strong> = 1EA0353A7D2947D8BBC6AD6FB52FCA84</li>
  <li><strong>Type</strong> = CBC</li>
  </ul>
  
  <p>It calculated this</p>
  
  <ul>
  <li><strong>Encrypted Output</strong> = C5537C8EFFFCC7E152C27831AFD383BA</li>
  </ul>
  
  <p>Then I use <strong>System.Security.Cryptography</strong>  library and calculate it. But it gives me different result. Could you help me for that situation?</p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  
  using System.IO;
  using System.Security.Cryptography;
  
  namespace DesfireCalculation
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
  
          byte key_no = 0x00;
          byte[] key = new byte[16] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
          byte[] IV = new byte[16] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
          byte[] rndB = new byte[16] { 0x1E,0xA0,0x35,0x3A,0x7D,0x29,0x47,0xD8,0xBB,0xC6,0xAD,0x6F,0xB5,0x2F,0xCA,0x84 };
  
          private void Form1_Load(object sender, EventArgs e)
          {
              try
              {
                  byte[] res=EncryptStringToBytes_Aes(BitConverter.ToString(rndB), key, IV);
                  string res_txt = BitConverter.ToString(res);
                  Console.WriteLine(res_txt);
              }
              catch (Exception ex)
              {
                  Console.WriteLine("Error: {0}", ex.Message);
              }
          }
  
          static byte[] EncryptStringToBytes_Aes(byte[] Data, byte[] Key, byte[] IV)
          {
              // Check arguments.
              if (Key == null || Key.Length &lt;= 0)
                  throw new ArgumentNullException("Key");
              if (IV == null || IV.Length &lt;= 0)
                  throw new ArgumentNullException("IV");
              byte[] encrypted;
  
              // Create an Aes object
              // with the specified key and IV.
              using (Aes aesAlg = Aes.Create())
              {
                  aesAlg.Key = Key;
                  aesAlg.IV = IV;
                  aesAlg.Mode = CipherMode.CBC;
                  aesAlg.BlockSize = 128;
                  aesAlg.FeedbackSize = 128;
                  aesAlg.KeySize = 128;
  
                  // Create an encryptor to perform the stream transform.
                  ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
  
                  // Create the streams used for encryption.
                  using (MemoryStream msEncrypt = new MemoryStream())
                  {
                      using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                      {
                          using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                          {
                              //Write all data to the stream.
                               swEncrypt.Write(Data);
                          }
                          encrypted = msEncrypt.ToArray();
                      }
                  }
              }
  
              // Return the encrypted bytes from the memory stream.
              return encrypted;    
          }
      }
  }
  </code></pre>