I do not have much experience with PGP as a programmer. For work, I have been using, un-modified in anyway, the classes found in the top answer for https://stackoverflow.com/questions/10209291/pgp-encrypt-and-decrypt" by M.Babcock for many years whenever I have a need to decrypt or encrypt a file. Up to this point, I have regularly updated to the latest BouncyCastle.Cryptography version. Currently, my production code is on BouncyCastle.Cryptography version 1.9.0.
When I try to upgrade BouncyCastle.Cryptography to version 2.0.0 or later in my project (console application, .net 6.0), this code will error out in the class PGPEncryptDecrypt
of that solution, Decrypt
method using a Stream input. Specifically, it errors out at the last line of the code snippet below
if (message is PgpCompressedData)
{
PgpCompressedData cData = (PgpCompressedData)message;
PgpObjectFactory of = null;
using (Stream compDataIn = cData.GetDataStream())
{
of = new PgpObjectFactory(compDataIn);
}
message = of.NextPgpObject();
//...
}
The exception occurs on the line message = of.NextPgpObject():
"System.ObjectDisposedException: 'Cannot access a closed stream. Object name: 'ZLibStream'."
When I roll back to version 1.9.0 the code is working as expected again. I have searched for examples of C# code using the 2.0.0 or later version but have not found any. In my search, I did go to bouncycastle.org and saw on the release notes that version 2.0.0 and later does contain breaking changes. However, I'm not knowledgeable enough to know how to modify the class PGPEncrtypDecrypt so that it works with version 2.0.0 and later.
What needs to be changed in the classes given in the top answer for https://stackoverflow.com/questions/10209291/pgp-encrypt-and-decrypt" so that it works with BouncyCastle.Cryptography 2.0.0. I would like to use the latest, which at the time of this post is 2.2.1.