Bouncycastle解密PKCS8

时间:2018-07-17 09:03:14

标签: c# encryption rsa bouncycastle

我在使用Bouncycastle解密字符串时遇到困难。我怀疑它的编码,但找不到充气弹药的任何PKCS8编码

var bytesToDecrypt = Convert.FromBase64String(base64Input);

            AsymmetricCipherKeyPair keyPair;
            var decryptEngine = new Pkcs1Encoding(new RsaEngine());

            using (var txtreader = new StringReader(privateKey))
            {
                keyPair = (AsymmetricCipherKeyPair)new PemReader(txtreader).ReadObject();

                decryptEngine.Init(false, keyPair.Private);
            }

            var decrypted = Encoding.UTF8.GetString(decryptEngine.ProcessBlock(bytesToDecrypt, 0, bytesToDecrypt.Length));
            return decrypted;

这是PKCS8格式的RSA密钥:

-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----

This is the error code I receive

1 个答案:

答案 0 :(得分:1)

使用AsymmetricKeyParameter代替AsymmetricCipherKeyPair,如下所示:

    AsymmetricKeyParameter akp;

    var decryptEngine = new Pkcs1Encoding(new RsaEngine());

    using (var txtreader = new StringReader(privateKey))
    {
        akp = (AsymmetricKeyParameter)new PemReader(txtreader).ReadObject();

        decryptEngine.Init(false, akp);
    }