title: Unable to decrypt file encrypted using AesManaged
body: <p>I am attempting to use <code>System.Security.Cryptography.AesManaged</code> to encrypt a file in my .net application.  It needs to be decrypted in an embedded Linux enviroment, so the .net libraries will not be available to me.</p>
  
  <p>The code I have at the moment looks something like this:</p>
  
  <pre><code>string encPassword = "ABCDABCDABCDABCDABCDABCDABCDABCD";
  string sourceFile = "myFile.txt";
  string targetFile = "myFile.encrypted.txt";
  FileStream fsInput = = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);
  FileStream fsOutput = new FileStream(targetFile, FileMode.OpenOrCreate, FileAccess.Write);
  
  CryptoStream cryptoStream = null;
  
  try
  {
      byte[] key = Encoding.ASCII.GetBytes(encPasswd);
      byte[] IV = new byte[16];
      Array.Copy(key, 0, IV, 0, 16);
  
      AesManaged aes = new AesManaged();
      aes.Key = key;
      aes.IV = IV;
      aes.BlockSize = 128;
      aes.KeySize = 256;
      aes.Mode = CipherMode.CBC;
  
      ICryptoTransform encryptor = aes.CreateEncryptor();
  
      cryptoStream = new CryptoStream(fsOutput, encryptor, CryptoStreamMode.Write);
  
      byte[] buffer = new byte[BUFFER_LENGTH];
      long bytesProcessed = 0;
      long fileLength = fsInput.Length;
      int bytesInCurrentBlock;
  
      do
      {
          bytesInCurrentBlock = fsInput.Read(buffer, 0, BUFFER_LENGTH);
          cryptoStream.Write(buffer, 0, bytesInCurrentBlock);
          bytesProcessed = bytesProcessed + bytesInCurrentBlock;
      }
      while (bytesProcessed &lt; fileLength);
  
      return true;
  }
  // ...
  </code></pre>
  
  <p>This encrypts the file okay.  Now I am trying to decrypt the file using a 3rd-party utility on Windows that is also supported in Linux, to give me confidence that the Linux developer will be able to decrypt it.</p>
  
  <p>A quick search on SourceForge let me to <a href="http://sourceforge.net/projects/enqrypt/" rel="nofollow">Enqrypt</a>.  However, if I use <code>Enqrypt</code> on the encrypted file like this:</p>
  
  <pre><code>enqrypt.exe -d -aes -256 -cbc -k ABCDABCDABCDABCDABCDABCDABCDABCD myFile.encrypted.txt
  </code></pre>
  
  <p>where <code>-d</code> indicates decrypt, <code>-256</code> indicates the key size, <code>-cbc</code> the mode, and <code>-k</code> preceding the key.</p>
  
  <p>it doesn't give me the original file.</p>
  
  <p>I have tried this with a few 3rd party utilities but I can't seem to decrypt it.</p>
  
  <p>Are there any obvious errors with how I am attempting to encrypt and decrypt this file?</p>
  
  <h3>Update</h3>
  
  <p>In response to recommendations from @Paŭlo, I now have the following test code (don't worry, I plan to change the key and IV to be different):</p>
  
  <pre><code>byte[] key = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 };
  byte[] IV =  { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 };
  </code></pre>
  
  <p>The block size is still 128 and the key size is still 256 in code.</p>
  
  <p>I now try to decrypt the file using <code>openssl</code> like so:</p>
  
  <pre><code>openssl enc -d -aes-256-cbc -in c:\encrypted.txt -out c:\decrypted.txt -K 11223344556677881122334455667788 -iv 11223344556677881122334455667788
  </code></pre>
  
  <p>This results in the following error:</p>
  
  <pre><code>bad decrypt 11452:error:06065064:digital envelope routines:EVP_DecryptFinal:bad decrypt:evp_enc.c:450:
  </code></pre>
  
  <p>Any idea what I am doing wrong?</p>