title: AES Encryption using C#
body: <p>I'm a newbie in cryptography and to learn it I tried to encrypt/decrypt with <code>AES</code> in <code>C#</code>.</p>
  
  <p>Sadly I realized, that it isn't as easy as I thought.
  So I was looking for a simpler solution.</p>
  
  <p>Later I found a couple of <a href="https://www.codeproject.com/Articles/1278566/Simple-AES-Encryption-using-Csharp" rel="nofollow noreferrer">code snippets</a> including some explanation.</p>
  
  <p>I copied the code and tried to implement it into a small application.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Security.Cryptography;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace aes
  {
      class Program
      {
  
          public static string passwd = null;
          public static string content = null;
          public static string encryptedcontent = null;
  
          public static byte[] IV = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
          public static int BlockSize = 128;
  
  
          static void Encrypt()
          {
  
              if (passwd == "") return;
              //Content to Byte Array
              byte[] bytes = Encoding.Unicode.GetBytes(content);
              //Encrypt
              //Init AES
              SymmetricAlgorithm crypt = Aes.Create();
              //Init md5 hash
              HashAlgorithm hash = MD5.Create();
              //AES blocksize (AES 192 etc.) (min 128)
              crypt.BlockSize = BlockSize;
              //Generating Key
              crypt.Key = hash.ComputeHash(Encoding.Unicode.GetBytes(passwd));
              //Initialize Vectors
              crypt.IV = IV;
  
              //CryptoStram is used for encryption
              //The required Encryptor is based on the algorithm above
              //Cryptostream sends data of the encrypted byte array to Memorystream
              //The memory stream is then converted into a Base64 string and made readable
              using (MemoryStream memoryStream = new MemoryStream())
              {
                  using (CryptoStream cryptoStream =
                     new CryptoStream(memoryStream, crypt.CreateEncryptor(), CryptoStreamMode.Write))
                  {
                      cryptoStream.Write(bytes, 0, bytes.Length);
                  }
  
                  encryptedcontent = Convert.ToBase64String(memoryStream.ToArray());
              }
          }
  
  
          static void Main(string[] args)
          {
  //Set Password
              Console.WriteLine("Passwort angeben");
              Console.Write("&gt; ");
                   passwd = Console.ReadLine();
  
  //Set content to encrypt (String)
              Console.WriteLine("Zu verschlüsselner Text angeben");
              Console.Write("&gt; ");
                   content = Console.ReadLine();
  
  
              Encrypt();
  
              Console.WriteLine(encryptedcontent);
  
              Console.ReadLine();
          }
      }
  }
  
  </code></pre>
  
  <p>Subsequently I wanted to try the programm with some testdata.
  I actually got a seemingly encrypted string.</p>
  
  <p>PW: <code>supersecretpassword</code> Content: <code>I like to keep my secrets</code> Result: <code>SEEc1sLMIyfVFsoHPFRIcl437+yjUC5uFMgco3iO+oWSgJWQOwKhoDhUbFJREeqiIvaY2DBR+Ih4OJeGAc6JZQ==</code></p>
  
  <p>I tried to use some online tools to decrypt and check my result.
  Sadly most of the Webtools were not able to decrypt my result.</p>
  
  <p>And if I encrypt the sentence <code>I like to keep my secrets</code> with that online tools I get results like:
  <code>7IWuebm0T8HdrGdtkBjt5zgjbdEqYfidNZVvfgtOjH4=</code></p>
  
  <p>My result <code>SEEc1sLMIyfVFsoHPFRIcl437+yjUC5uFMgco3iO+oWSgJWQOwKhoDhUbFJREeqiIvaY2DBR+Ih4OJeGAc6JZQ==</code> </p>
  
  <p>As you can see, the two results are different.
  Unfortunately I have no idea why this could be the case.</p>
  
  <p>Thanks for you help</p>
  
  <p>Jonas</p>
  
  <p><em>P.S Somehow I deleted some of rows written in this question. I hope the new words can clarify what my problem is.</em></p>