C# - Decrypt large files in memory using BouncyCastle -


i have requirement need decrypt large files in memory because these files contain sensitive data (ssns, dobs, etc). in other words decrypted data cannot @ rest (on disk). able use bouncycastle api c# , managed make work files 780 mb. here's code works:

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

in above code entire decrypted data stored in pgpdata string , that's fine files 780mb stated previously.

however, getting larger files , solution above not work outofmemoryexeception exception.

i've been trying code below, , keep getting errors when decryptpgpdata method below invoked. when using 512 chunksize "premature end of stream in partialinputstream" exception. when using 1024 chunksize "exception starting decryption" exception. question is, correct way decerypt chunks of data using bouncycastle? pgp file i'm trying decrypt encrypted using gpg.exe utility. appreciated.....it's been 2 days i've been trying make work no success.

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


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -