title: AES Encryption C#
body: <p>EDIT: Added my hash code to the bottom of this.</p>
  
  <p>I am having some problems creating a message integrity key for a solution I am creating. In order for this to be correct I need to use the following settings.</p>
  
  <p>Mode: ECB
  KeySize: 256
  BlockSize: 128
  Padding: PKCS7</p>
  
  <p>I am using a 32 byte key which is generated from a file and also a blank IV as I understand ECB does not require one.</p>
  
  <p>My problem I am expecting a 48 byte output from this before the encoding however I am receiving a 64 byte output.</p>
  
  <p>I have shown some code below about how am I am trying to achieve this but I am not having much success.</p>
  
  <pre><code>public static string Encrypt(string hash) {
  
          // Create a new instance of the AesManaged
          // class.  This generates a new key and initialization 
          // vector (IV).
          using (AesManaged myAes = new AesManaged()) {
  
              myAes.Key = File.ReadAllBytes("keyfile");
              myAes.Mode = CipherMode.ECB;
              myAes.IV = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
              myAes.KeySize = 256;
              myAes.BlockSize = 128;
              myAes.Padding = PaddingMode.PKCS7;
  
              // Encrypt the string to an array of bytes.
              byte[] encrypted = EncryptStringToBytes_Aes(hash, myAes.Key, myAes.IV);
  
              // Decrypt the bytes to a string.
              string roundtrip = DecryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV);
  
              //Display the original data and the decrypted data.
              Console.WriteLine("Original:   {0}", hash);
              Console.WriteLine("Round Trip: {0}", roundtrip);
  
              // Encode
              string encoded = Convert.ToBase64String(encrypted);
  
              Console.WriteLine("Encoded:    {0}", encoded);
              return encoded;
          }
      }
  
      static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV) {
          // Check arguments.
          if (plainText == null || plainText.Length &lt;= 0)
              throw new ArgumentNullException("plainText");
          if (Key == null || Key.Length &lt;= 0)
              throw new ArgumentNullException("Key");
          if (IV == null || IV.Length &lt;= 0)
              throw new ArgumentNullException("Key");
          byte[] encrypted;
          // Create an AesManaged object
          // with the specified key and IV.
          using (AesManaged aesAlg = new AesManaged()) {
              aesAlg.Key = Key;
              aesAlg.IV = IV;
  
              // Create a decrytor 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(plainText);
                      }
                      encrypted = msEncrypt.ToArray();
                  }
              }
          }
          // Return the encrypted bytes from the memory stream.
          return encrypted;
      }
  
  static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
      {
          // Check arguments.
          if (cipherText == null || cipherText.Length &lt;= 0)
              throw new ArgumentNullException("cipherText");
          if (Key == null || Key.Length &lt;= 0)
              throw new ArgumentNullException("Key");
          if (IV == null || IV.Length &lt;= 0)
              throw new ArgumentNullException("Key");
  
          // Declare the string used to hold
          // the decrypted text.
          string plaintext = null;
  
          // Create an AesManaged object
          // with the specified key and IV.
          using (AesManaged aesAlg = new AesManaged())
          {
              aesAlg.Key = Key;
              aesAlg.IV = IV;
  
              // Create a decrytor to perform the stream transform.
              ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
  
              // Create the streams used for decryption.
              using (MemoryStream msDecrypt = new MemoryStream(cipherText))
              {
                  using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                  {
                      using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                      {
  
                          // Read the decrypted bytes from the decrypting stream
                          // and place them in a string.
                          plaintext = srDecrypt.ReadToEnd();
                      }
                  }
              }
  
          }
  
          return plaintext;
  
      }
  
          public static string getHashSha256(string text) {
          byte[] bytes = Encoding.UTF8.GetBytes(text);
          SHA256Managed hashstring = new SHA256Managed();
          byte[] hash = hashstring.ComputeHash(bytes);
          string hashString = string.Empty;
          foreach (byte x in hash) {
              hashString += String.Format("{0:x2}", x);
          }
          return hashString;
      }
  </code></pre>