title: AES encryption on large files
body: <p>I need to encrypt and decrypt large file (~1GB).
  I tried using this example: <a href="http://www.codeproject.com/Articles/769741/Csharp-AES-bits-Encryption-Library-with-Salt" rel="noreferrer">http://www.codeproject.com/Articles/769741/Csharp-AES-bits-Encryption-Library-with-Salt</a>
  But my problem is since the file is very large, I'm getting outOfMemory exception.
  So I need to replace the memory stream with file stream, I just not sure how to do it...</p>
  
  <p>(Adding my code:)</p>
  
  <pre><code>private static void AES_Encrypt(string srcFile, string encryptedFile,  byte[] passwordBytes)
      {
  
  
          // 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};
  
          FileStream fsInput = new FileStream(srcFile,
              FileMode.Open,
              FileAccess.Read);
  
          FileStream fsEncrypted = new FileStream(encryptedFile,
                          FileMode.Create,
                          FileAccess.Write);
  
          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(fsEncrypted, AES.CreateEncryptor(), CryptoStreamMode.Write))
              {
                  byte[] bytearrayinput = new byte[fsInput.Length - 1];
                  fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
                  cs.Write(bytearrayinput, 0, bytearrayinput.Length);
                  cs.Close();
                  fsInput.Flush();
                  fsInput.Close();
                  fsEncrypted.Close();
              }
  
          }
  
  
      }
  
      public static void AES_Decrypt(string encryptedFile, string decryptedFile, 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};
  
          FileStream fsread = new FileStream(encryptedFile,
                                 FileMode.Open,
                                 FileAccess.Read);
  
          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;
  
              FileStream fsDecrypted = new FileStream(decryptedFile,
                          FileMode.Create,
                          FileAccess.Write);
  
              using (var cs = new CryptoStream(fsDecrypted, AES.CreateDecryptor(), CryptoStreamMode.Write))
              {
                  byte[] bytearrayinput = new byte[fsread.Length - 1];
                  fsread.Read(bytearrayinput, 0, bytearrayinput.Length);
                  cs.Write(bytearrayinput, 0, bytearrayinput.Length);
                  cs.Close();
                  fsread.Close();
                  fsDecrypted.Close();
              }
  
          }
      }
  </code></pre>