title: File.ReadAllBytes() Out of memory exception
body: <p>I have small app that among other read any file (i.e. .exe, . jpg, . img and all others), transfer it to <code>bytes[]</code> trough <code>File.ReadAllBytes()</code>, encrypt it with 3DES and save with <code>File.WriteAllBytes()</code>. Than there is decrypt method that doing the same reverse. My problem is, everything is working fine up to 300MB original file, than I have out of memory exception. Any suggestion how to solve this problem? How to work with files >1GB? Can it help rather use streamreader or cryptostream reader and writer?</p>
  
  <p>Reading and encrypting file:</p>
  
  <pre><code> try
   {
       saveFileDialog1.FileName = "";
       DialogResult result = openFileDialog1.ShowDialog();
       if (result == DialogResult.OK) 
       {
           byte[] Results;
           System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
           fileToEncrypt = File.ReadAllBytes(openFileDialog1.FileName);
           MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
           byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(textBox1.Text));
           TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
           TDESAlgorithm.Key = TDESKey;
           TDESAlgorithm.Mode = CipherMode.ECB;
           TDESAlgorithm.Padding = PaddingMode.PKCS7;
           ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
           Results = Encryptor.TransformFinalBlock(fileToEncrypt, 0, fileToEncrypt.Length);
           saveFileDialog1.FileName = openFileDialog1.SafeFileName;
           saveFileDialog1.ShowDialog();
           if (saveFileDialog1.FileName != "")
           {
               File.WriteAllBytes(saveFileDialog1.FileName, Results);
           }
           TDESAlgorithm.Clear();
           HashProvider.Clear();
       }
   }
   catch
   {
       MessageBox.Show("Your system has not enough memory.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
   }
  </code></pre>
  
  <p>Decrypting and saving:</p>
  
  <pre><code> try
   {
       openFileDialog1.FileName = "";
       DialogResult result = openFileDialog1.ShowDialog();
       if (result == DialogResult.OK) // Test result.
       {
           byte[] Results;
           System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
           fileToDecrypt = File.ReadAllBytes(openFileDialog1.FileName);
           MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
           byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(textBox1.Text));
           TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
           TDESAlgorithm.Key = TDESKey;
           TDESAlgorithm.Mode = CipherMode.ECB;
           TDESAlgorithm.Padding = PaddingMode.PKCS7;
           ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
           Results = Decryptor.TransformFinalBlock(fileToDecrypt, 0, fileToDecrypt.Length);
           saveFileDialog1.FileName = Path.GetFileName(openFileDialog1.FileName);
           saveFileDialog1.ShowDialog();
           if (saveFileDialog1.FileName != "")
           {
               File.WriteAllBytes(saveFileDialog1.FileName, Results);
           }
           TDESAlgorithm.Clear();
           HashProvider.Clear();
       }
   }
   catch 
   {
       MessageBox.Show("Master password is wrong", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
   }
  </code></pre>
  
  <p>I am only hobby coder, so I pray for some benevolence with my code :)</p>
  
  <p><code>textbox1.Text</code> is master password.</p>