我需要在内存中解密大文件,因为这些文件包含敏感数据(SSN,DOB等) . 换句话说,解密的数据不能处于静止状态(在磁盘上) . 我能够使用Coun的CouncyCastle API并设法使其适用于最大780 MB的文件 . 基本上这是有效的代码:

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();
}

在上面的代码中,整个解密数据存储在pgpData字符串中,如前所述,这对于高达780MB的文件很好 .

但是,我将获得更大的文件,上面的解决方案不起作用,因为我得到OutOfMemoryExeception异常 .

我一直在尝试下面的代码,并且在调用下面的DecryptPgpData方法时我不断收到错误 . 当使用512作为chunksize时,我得到“PartialInputStream中的流的过早结束”异常 . 当使用1024作为chunksize时,我得到一个“异常启动解密”异常 . 我的问题是,这是使用BouncyCastle来欺骗数据块的正确方法吗?我正在尝试解密的pgp文件是使用gpg.exe实用程序加密的 . 任何帮助都非常感谢.....我已经差不多两天了,我一直试图让这个没有成功 .

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 < 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();