title: C# - Decrypt large files in memory using BouncyCastle
body: <p>I have a requirement where I need to decrypt large files in memory because these files contain sensitive data (SSNs, DOBs, etc). In other words the decrypted data cannot be at rest (on disk). I was able to use the BouncyCastle API for C# and managed to make it to work for files up to 780 MB. Basically here's the code that works: </p>
  
  <pre><code>string PRIVATE_KEY_FILE_PATH = @"c:\pgp\privatekey.gpg";
  string PASSPHRASE = "apassphrase";
  string pgpData;
  string[] pgpLines;
  string pgpFilePath = @"C:\test\test.gpg";
  
  Stream inputStream = File.Open(pgpFilePath, FileMode.Open);
  Stream privateKeyStream = File.Open(PRIVATE_KEY_FILE_PATH, FileMode.Open);
  string pgpData = CryptoHelper.DecryptPgpData(inputStream, privateKeyStream, PASSPHRASE);
  string[] pgpLines = pgpData.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
  
  Console.WriteLine(pgpLines.Length);
  Console.ReadLine();
  
  foreach (var x in pgpLines)
  {
          Console.WriteLine(x);
          Console.ReadLine();
  }    
  </code></pre>
  
  <p>In the above code the entire decrypted data is stored in the pgpData string and that's fine for files up to 780MB as stated previously. </p>
  
  <p>However, I will be getting much larger files and my solution above does not work as I get OutOfMemoryExeception exception. </p>
  
  <p>I've been trying the code below, and I keep getting errors when the DecryptPgpData method below is invoked. When using 512 as the chunksize I get a "Premature end of stream in PartialInputStream" exception. When using 1024 as the chunksize I get a "Exception starting decryption" exception. My question is, is this the correct way to decerypt chunks of data using BouncyCastle? The pgp file I'm trying to decrypt was encrypted using the gpg.exe utility. Any help is much appreciated.....It's been almost two days that I've been trying to make this to work with no success.  </p>
  
  <pre><code>string PRIVATE_KEY_FILE_PATH = @"c:\pgp\privatekey.gpg";
  string PASSPHRASE = "apassphrase";
  string pgpData;
  string[] pgpLines;
  string pgpFilePath = @"C:\test\test.gpg";
  
  string decryptedData = string.Empty;
  FileInfo inFile = new FileInfo(pgpFilePath);
  FileStream fs = null;
  
  fs = inFile.OpenRead();
  int chunkSize = 1024;
  
  byte[] buffer = new byte[chunkSize];
  int totalRead = 0;
  
  while (totalRead &lt; fs.Length)
  {
           int readBytes = fs.Read(buffer, 0, chunkSize);
           totalRead += readBytes;
  
  
          Stream stream = new MemoryStream(buffer);
          decryptedData = CryptoHelper.DecryptPgpData(stream, privateKeyStream, PASSPHRASE);
                  Console.WriteLine(decryptedData);                                     
  }
  
  fs.Close();            
  Console.WriteLine(totalRead);            
  Console.ReadLine();
  </code></pre>