3

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

Stream clear = pbe.GetDataStream(privKey);

它返回一个错误: 异常解密密钥

这是我的解密代码:

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 第三方库。

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

4

1 回答 1

5

如果您正在关注 BouncyCastle 类 PGPEncrypt、PGPDecrypt 和 PGPEncryptionKeys...

在 PGPEncryptionKeys 类下,添加此方法:

/// <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);
}

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

^_^

于 2013-05-08T10:19:31.330 回答