If you're following the BouncyCastle classes PGPEncrypt, PGPDecrypt and PGPEncryptionKeys...
Under the PGPEncryptionKeys class, add this method:
/// <summary>
/// Return the last key we can use to decrypt.
/// Note: A file can contain multiple keys (stored in "key rings")
/// </summary>
private PgpSecretKey GetLastSecretKey(PgpSecretKeyRingBundle secretKeyRingBundle)
{
return (from PgpSecretKeyRing kRing in secretKeyRingBundle.GetKeyRings()
select kRing.GetSecretKeys().Cast<PgpSecretKey>()
.LastOrDefault(k => k.IsSigningKey))
.LastOrDefault(key => key != null);
}
still inside the PgpEncryptionKeys class, make sure the ReadSecretKey method looks like this:
private PgpSecretKey ReadSecretKey(string privateKeyPath, bool toEncrypt)
{
using (Stream keyIn = File.OpenRead(privateKeyPath))
using (Stream inputStream = PgpUtilities.GetDecoderStream(keyIn))
{
PgpSecretKeyRingBundle secretKeyRingBundle = new PgpSecretKeyRingBundle(inputStream);
PgpSecretKey foundKey = toEncrypt ? GetFirstSecretKey(secretKeyRingBundle) : GetLastSecretKey(secretKeyRingBundle);
if (foundKey != null)
return foundKey;
}
throw new ArgumentException("Can't find signing key in key ring.");
}
^_^