I am trying to decrypt PGP encrypted string using the BouncyCastle PGP SDK.
I have tried several approaches and in each case I get a following error at the beginning of the process (exact place shown below):
unknown object in stream 47
I have so far tried two nuget packages:
- BouncyCastle.OpenPGP v.1.8.1.
- BouncyCastle-Ext v1.7.0
I have tried approaches presented in two articles:
- http://jasongoemaat.blogspot.ro/2015/09/c-bouncy-castle-file-decryption.html
- https://code.msdn.microsoft.com/vstudio/Pretty-Good-Privacy-using-4f473c67#content
And I have tried three methods of getting test keys and encrypted strings:
- https://www.igolder.com/PGP/generate-key/
- https://sela.io/pgp/
- Generating it using the code from the tutorial on MSDN (link above)
At the moment my code looks as follows:
public void DecryptFile(string inputFilePath, string outputPath)
{
using (FileStream fsEncryptedFile = File.Open(inputFilePath, FileMode.Open))
using (Stream decoderStream = PgpUtilities.GetDecoderStream(fsEncryptedFile))
{
PgpObjectFactory factory = new PgpObjectFactory(decoderStream);
PgpObject obj = factory.NextPgpObject(); //<-- ERROR THROWN HERE
if (!(obj is PgpEncryptedDataList))
{
obj = factory.NextPgpObject();
}
PgpEncryptedDataList edl = obj as PgpEncryptedDataList;
foreach (PgpPublicKeyEncryptedData data in edl.GetEncryptedDataObjects())
{
PgpPrivateKey privateKey = null;
using (FileStream fsKeyring = File.Open(this.privateKeyPath, FileMode.Open))
{
PgpSecretKeyRingBundle bundle = new PgpSecretKeyRingBundle(fsKeyring); // <-- also same error can happen here if I skip the error above
PgpSecretKey secretKey = bundle.GetSecretKey(data.KeyId);
privateKey = secretKey.ExtractPrivateKey(this.signature.ToCharArray());
}
PgpObjectFactory plainFactory = new PgpObjectFactory(data.GetDataStream(privateKey));
PgpObject message = plainFactory.NextPgpObject();
if (message is PgpCompressedData)
{
message = new PgpObjectFactory(((PgpCompressedData) message).GetDataStream()).NextPgpObject();
}
if (message is PgpLiteralData)
{
PgpLiteralData ld = (PgpLiteralData) message;
using (FileStream outStream = File.Create(outputPath))
{
Stream inStream = ld.GetInputStream();
byte[] buffer = new byte[0x100000];
int count = inStream.Read(buffer, 0, buffer.Length);
while (count > 0)
{
outStream.Write(buffer, 0, count);
count = inStream.Read(buffer, 0, buffer.Length);
}
outStream.Close();
}
}
else
{
Console.WriteLine("ERROR: Unknown type of message in PGP file: {0}", message.GetType().FullName);
}
}
}
}
Here are my test keys and hello world message generated at https://www.igolder.com/PGP/generate-key/
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: BCPG C# v1.6.1.0
mQENBFoAOVkBCACQs2N2Z/b6Fpx+w6Et380qVCtwxJ6GBCRAgwVLGNSKoPJ/L7Sh
dBYJAY5hAWWkSNRislYUG7iykK3JtJVOetlts1xFQX5gVVABZphGx9OtbGo95dUJ
vxV3w7cpXwHPB45skd2qjzIZLh8PY0BYC4Rf8SIa2LBhPh5CqT339Z8h0dGBhykj
X8LyzCVnKVjUUZpaGHoM5zo3jtIhxBgYs6WowuWaMtpEynQW753klBIW+d7UsJ2x
zrHu3SNE4hU5ayNvZKwx3Mb1LRf2ydqaniyLoL4nrPzeIr19vBHRfr52itjcG1hD
4PoD5zECwKK4KOqfPAjg/8fKa/oDZserdIxnABEBAAG0AIkBHAQQAQIABgUCWgA5
WQAKCRC3T9Uo2/H1P2yQCACMjLPb3z1hRErwevJtyEvaixIovVS/I/8anW9t9egr
yaoufqC0WPGN8nDrFXRInYeV3hV1C6qSq1BUGsOhS1aFDsjqqUdtQ1oIemAs9BnV
trD/noOkR11THtA2+gO3nyIo6vLADiqDQ5RQjzOMTwGVAHJjf8JpxvlIyrb/09sO
j1CmhcZfUPYNLV15h3XWHLk1dJhtYTjKYtH+kbS2THln/0LOCd/h9+rdmojQmtau
8wkgeX9vhiaGItZ+xjTb7jNTTn+fzO3IkeffgklIZbhX7/NqUKKNFUKYzHDwuS2L
uTgd1VhOQWfI/ZTIwmVqoCMOlYdtmrsl12tBp0JZUXJS
=8xO7
-----END PGP PUBLIC KEY BLOCK-----
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: BCPG C# v1.6.1.0
lQOsBFoAOVkBCACQ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=wxc0
-----END PGP MESSAGE-----
Any idea why am I constantly getting this error ?
Thanks