title: How to replicate AES Encryption used by this online tool
body: <p><a href="http://aes.online-domain-tools.com/" rel="nofollow">http://aes.online-domain-tools.com/</a></p>
  
  <p>I am trying to replicate this encryption with c# using CBC but not getting the same results no matter what I try.</p>
  
  <p>So far my code:</p>
  
  <pre><code>    private byte[] hex2bytes(string s)
      {
          return Enumerable.Range(0, s.Length)
                           .Where(x =&gt; x % 2 == 0)
                           .Select(x =&gt; Convert.ToByte(s.Substring(x, 2), 16))
                           .ToArray();        
      }
  
  private AesCryptoServiceProvider GetProvider(byte[] key)
  {
      AesCryptoServiceProvider result = new AesCryptoServiceProvider();
      result.BlockSize = 128;
      result.KeySize = 128;
      result.Mode = CipherMode.CBC;
      result.Padding = PaddingMode.PKCS7;
  
      result.GenerateIV();
      //result.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
      result.IV = hex2bytes("ad77d666311839f5665aeb2e42f64542");
  
      byte[] RealKey = GetKey(key, result);
      _key = Encoding.ASCII.GetString(RealKey);
      result.Key = RealKey;
      // result.IV = RealKey;
      return result;
  }
  
  private byte[] GetKey(byte[] suggestedKey, SymmetricAlgorithm p)
  {
      byte[] kRaw = suggestedKey;
      List&lt;byte&gt; kList = new List&lt;byte&gt;();
  
      for (int i = 0; i &lt; p.LegalKeySizes[0].MinSize; i += 8)
      {
          kList.Add(kRaw[(i / 8) % kRaw.Length]);
      }
      byte[] k = kList.ToArray();
      return k;
  }
  
  /// &lt;summary&gt;
  /// Encrpyts the sourceString, returns this result as an Aes encrpyted, BASE64 encoded string
  /// &lt;/summary&gt;
  /// &lt;param name="plainSourceStringToEncrypt"&gt;a plain, Framework string (ASCII, null terminated)&lt;/param&gt;
  /// &lt;param name="passPhrase"&gt;The pass phrase.&lt;/param&gt;
  /// &lt;returns&gt;
  /// returns an Aes encrypted, BASE64 encoded string
  /// &lt;/returns&gt;
  public string EncryptString(string plainSourceStringToEncrypt, string passPhrase)
  {
      //Set up the encryption objects
      using (AesCryptoServiceProvider acsp = GetProvider(Encoding.UTF8.GetBytes(passPhrase)))
      {
          byte[] sourceBytes = Encoding.UTF8.GetBytes(plainSourceStringToEncrypt);
          ICryptoTransform ictE = acsp.CreateEncryptor();
  
          //Set up stream to contain the encryption
          MemoryStream msS = new MemoryStream();
  
          //Perform the encrpytion, storing output into the stream
          CryptoStream csS = new CryptoStream(msS, ictE, CryptoStreamMode.Write);
          csS.Write(sourceBytes, 0, sourceBytes.Length);
          csS.FlushFinalBlock();
  
          //sourceBytes are now encrypted as an array of secure bytes
          byte[] encryptedBytes = msS.ToArray(); //.ToArray() is important, don't mess with the buffer
          var x = BitConverter.ToString(encryptedBytes);
          //return the encrypted bytes as a BASE64 encoded string
          return Convert.ToBase64String(encryptedBytes);
      }
  }
  
  
  /// &lt;summary&gt;
  /// Decrypts a BASE64 encoded string of encrypted data, returns a plain string
  /// &lt;/summary&gt;
  /// &lt;param name="base64StringToDecrypt"&gt;an Aes encrypted AND base64 encoded string&lt;/param&gt;
  /// &lt;param name="passphrase"&gt;The passphrase.&lt;/param&gt;
  /// &lt;returns&gt;returns a plain string&lt;/returns&gt;
  public string DecryptString(string base64StringToDecrypt, string passphrase)
  {
      //Set up the encryption objects
      using (AesCryptoServiceProvider acsp = GetProvider(Encoding.UTF8.GetBytes(passphrase)))
      {
          byte[] RawBytes = Convert.FromBase64String(base64StringToDecrypt);
          ICryptoTransform ictD = acsp.CreateDecryptor();
  
          //RawBytes now contains original byte array, still in Encrypted state
  
          //Decrypt into stream
          MemoryStream msD = new MemoryStream(RawBytes, 0, RawBytes.Length);
          CryptoStream csD = new CryptoStream(msD, ictD, CryptoStreamMode.Read);
          //csD now contains original byte array, fully decrypted
  
          //return the content of msD as a regular string
          return (new StreamReader(csD)).ReadToEnd();
      }
  }
  </code></pre>