title: Decrypting data encrypted with AES-256-CBC in C# as with PHP
body: <p>I did some encryption using PHP in my Database and would normally decrypt using:</p>
  
  <pre><code>$encrypt_method = "AES-256-CBC";
  $secret_key = "testing";
  $secret_iv = "testingyes!!!";
  $key = hash('sha256', $secret_key); // hash the key
  $iv = substr(hash('sha256', $secret_iv), 0, 16); // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
  echo(openssl_decrypt(base64_decode($data), $encrypt_method, $key, 0, $iv)); // the decrypted data
  </code></pre>
  
  <p>I'm trying to do the same task but with C# 2013 to decrypt the same data, any ideas?</p>
  
  <p>I would encrypt in php using:</p>
  
  <pre><code>$encrypt_method = "AES-256-CBC";
  $secret_key = "testing";
  $secret_iv = "testingyes!!!";
  $key = hash('sha256', $secret_key); // hash the key
  $iv = substr(hash('sha256', $secret_iv), 0, 16); // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
  echo(base64_encode(openssl_encrypt($data, $encrypt_method, $key, 0, $iv))); // the encrypted data
  </code></pre>
  
  <p>encrypting: this is a test</p>
  
  <p>gives: d0EzQ2MvMHkxRks2cXg5NkFkK2twZz09=</p>
  
  <p>I tried this in C#:</p>
  
  <pre><code>public static String sha256_hash(String value)
      {
          StringBuilder Sb = new StringBuilder();
  
          using (SHA256 hash = SHA256Managed.Create())
          {
              Encoding enc = Encoding.UTF8;
              Byte[] result = hash.ComputeHash(enc.GetBytes(value));
  
              foreach (Byte b in result)
                  Sb.Append(b.ToString("x2"));
          }
  
          return Sb.ToString();
      }
  
  private static String AES_decrypt(String Input)
      {
          RijndaelManaged aes = new RijndaelManaged();
          aes.KeySize = 256;
          aes.BlockSize = 256;
          aes.Mode = CipherMode.CBC;
          aes.Padding = PaddingMode.None;
  
          aes.Key = Convert.FromBase64String(sha256_hash("testing"));            
          aes.IV = Convert.FromBase64String(sha256_hash("testingyes!!!").Substring(0, 16));
          var decrypt = aes.CreateDecryptor();
          byte[] xBuff = null;
          using (var ms = new MemoryStream())
          {
              using (var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write))
              {
                  byte[] xXml = Convert.FromBase64String(Input);
                  cs.Write(xXml, 0, xXml.Length);
              }
              xBuff = ms.ToArray();
          }
          String Output = Encoding.UTF8.GetString(xBuff);
          return Output;
      }
  
  string cipherData = "d0EzQ2MvMHkxRks2cXg5NkFkK2twZz09=";
  string f = AES_decrypt(cipherData);
  Console.Write(f);
  </code></pre>
  
  <p>But I'm getting error: specified key is not a valid size for this algorithm</p>
  
  <p>However the key I'm using is working when I use PHP</p>