title: Encryption and Decryption of files results in a blank file when decrypting
body: <p>Below is a simple snippet I'm using to test encryption and decryption of files.  What's happening is encryption is working fine - however, when I decrypt, the file is blank and I'm not entirely sure why.</p>
  
  <p>I'm also getting a "padding is invalid and cannot be removed" issue but I can solve that with <code>Padding.None</code> but I don't understand why the file is null when decrypted. I'm using a simple text document to test.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Security.Cryptography;
  using System.IO;
  
  namespace EncryptionTest
  {
      class Program
      {
          static void Main(string[] args)
          {
              var Mode = "";
              bool Loop = true;
              while (Loop)
              {
                  Console.WriteLine("Encrypt or Decrypt? (E/D)");
                  Mode = Console.ReadLine();
                  if (Mode.ToUpper() == "E" || Mode.ToUpper() == "D")
                      switch (Mode.ToUpper())
                      {
                          case "E":
                              Mode = "Encrypt";
                              Loop = false;
                              continue;
                          case "D":
                              Mode = "Decrpt";
                              Loop = false;
                              continue;
                      }
                  else
                      Console.WriteLine("Must be E OR D");
              }
              Console.WriteLine("What directory do you want to encrypt?");
  
              var fileDirectory = Console.ReadLine();
  
              Console.WriteLine("What password do you want to use?");
              var passWord = Console.ReadLine();
  
              string[] files = Directory.GetFiles(fileDirectory);
              for (int i = 0; i &lt; files.Length; i++)
              {
                  byte[] bytesToBeEncrypted = File.ReadAllBytes(files[i]);
                  byte[] passwordBytes = Encoding.UTF8.GetBytes(passWord);
  
                  // Hash the password with SHA256
                  passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
                  byte[] bytesCyrpt = new byte[0];
                  if (Mode == "Encrypt")
                  {
                      bytesCyrpt = AES_Encrypt(bytesCyrpt, passwordBytes);
                      File.WriteAllBytes(encryptedFileName(files[i]), bytesCyrpt);
                      File.Delete(files[i]);
                  }
                  else
                  {
                      bytesCyrpt = AES_Decrypt(bytesCyrpt, passwordBytes);
                      File.WriteAllBytes(decryptFileName(files[i]), bytesCyrpt);
                  }                  
              }
          }
  
          public static byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
          {
              byte[] encryptedBytes = null;
  
              // Set your salt here, change it to meet your flavor:
              // The salt bytes must be at least 8 bytes.
              byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
  
              using (MemoryStream ms = new MemoryStream())
              {
                  using (RijndaelManaged AES = new RijndaelManaged())
                  {
                      AES.KeySize = 256;
                      AES.BlockSize = 128;
  
                      var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
                      AES.Key = key.GetBytes(AES.KeySize / 8);
                      AES.IV = key.GetBytes(AES.BlockSize / 8);
  
                      AES.Mode = CipherMode.CBC;
  
                      using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
                      {
                          cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
                          cs.Close();
                      }
                      encryptedBytes = ms.ToArray();
                  }
              }
  
              return encryptedBytes;
          }
  
          public static byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
          {
              byte[] decryptedBytes = null;
  
              // Set your salt here, change it to meet your flavor:
              // The salt bytes must be at least 8 bytes.
              byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
  
              using (MemoryStream ms = new MemoryStream())
              {
                  using (RijndaelManaged AES = new RijndaelManaged())
                  {
                      AES.KeySize = 256;
                      AES.BlockSize = 128;
  
                      var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
                      AES.Key = key.GetBytes(AES.KeySize / 8);
                      AES.IV = key.GetBytes(AES.BlockSize / 8);
  
                      AES.Mode = CipherMode.CBC;
  
                      using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
                      {
                          cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
                          cs.Close();
                      }
                      decryptedBytes = ms.ToArray();
                  }
              }
              return decryptedBytes;
          }
  
          private static string encryptedFileName(string fileName)
          {
              string new_file_name = string.Empty;
              string extension = fileName.Split('.').Last();
              new_file_name = fileName.Replace(extension, "");
              new_file_name = new_file_name + "_encrypt." + extension;
  
              return new_file_name;
          }
  
          private static string decryptFileName(string fileName)
          {
              string new_file_name = fileName;
              new_file_name = new_file_name.Replace("_encrypt", "");
              return new_file_name;
          }
      }
  }
  </code></pre>