Exception on decrypting file using BouncyCastle PGP

廉价感情. 提交于 2019-12-30 07:13:35

问题


I was trying to decrypt this sample file given by the client, using a class called PgpDecrypt. But when the code comes to this line:

Stream clear = pbe.GetDataStream(privKey);

it returns an error: exception decrypting secret key

Here's my decryption code:

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\");

I am using BouncyCastle as my third party library for .NET.

Any idea to solve this would be a great help. Thanks in advance!


回答1:


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

^_^



来源:https://stackoverflow.com/questions/13679204/exception-on-decrypting-file-using-bouncycastle-pgp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!