使用BouncyCastle PGP解密文件的例外情况

我试图使用一个名为PgpDecrypt的类来解密客户端给出的这个示例文件。 但是当代码来到这一行时:

Stream clear = pbe.GetDataStream(privKey); 

它返回一个错误: exception解密密钥

这是我的解密代码:

 PgpDecrypt test = new PgpDecrypt(string.Concat(pathh, "TestDecryptionFile"), string.Concat(pathh, "mypgpprivatekey.key"), "mypassphrase", @"d:/test/", string.Concat(pathh, "clientpublickey.key")); FileStream fs = File.Open(string.Concat(pathh, "TestDecryptionFile"), FileMode.Open); test.Decrypt(fs, @"d:\test\"); 

我使用BouncyCastle作为.NET的第三方库。

任何解决这个问题的想法都会有很大的帮助。 提前致谢!

如果您正在关注BouncyCastle类PGPEncrypt,PGPDecrypt和PGPEncryptionKeys ……

在PGPEncryptionKeys类下,添加以下方法:

 ///  /// Return the last key we can use to decrypt. /// Note: A file can contain multiple keys (stored in "key rings") ///  private PgpSecretKey GetLastSecretKey(PgpSecretKeyRingBundle secretKeyRingBundle) { return (from PgpSecretKeyRing kRing in secretKeyRingBundle.GetKeyRings() select kRing.GetSecretKeys().Cast() .LastOrDefault(k => k.IsSigningKey)) .LastOrDefault(key => key != null); } 

仍然在PgpEncryptionKeys类中,确保ReadSecretKey方法如下所示:

 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."); } 

^ _ ^