Discuss this help topic in SecureBlackbox Forum
Work with files via streams
The easiest way to work with files in OpenPGPBlackbox is to use file streams. This is because file streams provide flexible access to file data and consume very little memory. This makes processing even of really huge files easy and resource-friendly.
If you need to protect (encrypt or sign) a file, do the following:
FileStream instream = new FileStream(@"C:\OpenPGP\music.mp3", FileMode.Open);
FileStream outstream = new FileStream(@"C:\OpenPGP\music.pgp", FileMode.Create);
writer.Encrypt(instream, outstream);
instream.Close(); outstream.Close();Note that you must include stream closure in a finally section of try/finally operator to guarantee stream closure even in case of exception thrown by Encrypt() method:
try { // doing OpenPGP stuff } finally { instream.Close(); }
If you need to unprotect (decrypt, verify) a file, you create the input stream as you do above, but you need to provide the output stream on the fly inside the handler of OnCreateOutputStream event. This is because the OpenPGP archive may contain a number of files, and for each of them OnCreateOutputStream will be raised. Inside the handler you create as stream just as you do in your synchronous code. Set FreeOnExit to true to have the stream closed to you by TElPGPReader, or to false if you wish to close it by yourself.
void handleCreateOutputStream(object sender, string filename, DateTime timestamp, ref Stream stream, ref TSBBoolean freeOnExit) { stream = new FileStream(@"C:\OpenPGP\extracted_" + filename, FileMode.Create); freeOnExit = true; }
How To articles about file encryption and signing with OpenPGP