解密包含私钥的受密码保护的PEM

blg*_*boy 6 c# encryption bouncycastle pem private-key

我有以下使用Bouncy Castle for C#创建加密私钥的方法:

public string GetPrivateKey(AsymmetricCipherKeyPair keyPair, string password)
{
    var generator = new Pkcs8Generator(keyPair.Private, Pkcs8Generator.PbeSha1_3DES);
    generator.IterationCount = 4;
    generator.Password = password.ToCharArray();
    var pem = generator.Generate();

    TextWriter textWriter = new StringWriter();
    PemWriter pemWriter = new PemWriter(textWriter);
    pemWriter.WriteObject(pem);
    pemWriter.Writer.Flush();
    string privateKey = textWriter.ToString();
    return privateKey;
}
Run Code Online (Sandbox Code Playgroud)

看起来像这样:

-----BEGIN ENCRYPTED PRIVATE KEY-----
...
-----END ENCRYPTED PRIVATE KEY-----
Run Code Online (Sandbox Code Playgroud)

我不知道如何在Decrypt方法中使用用于加密私钥的密码。现在,在不知道如何使用he来“解密”我的私钥password的情况下,我得到了以下异常:

Org.BouncyCastle.OpenSsl.PemException:创建ENCRYPTED私钥时出现问题:System.NullReferenceException:对象引用未设置为对象的实例。在Org.BouncyCastle.OpenSsl.PemReader.ReadPrivateKey(PemObject pemObject)

这是Decrypt方法的代码:

public string Decrypt(string base64Input, string privateKey, string password)
{
    var bytesToDecrypt = Convert.FromBase64String(base64Input);

    //get a stream from the string
    AsymmetricCipherKeyPair keyPair;
    var decryptEngine = new Pkcs1Encoding(new RsaEngine());

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

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

    var decrypted = Encoding.UTF8.GetString(decryptEngine.ProcessBlock(bytesToDecrypt, 0, bytesToDecrypt.Length));
    return decrypted;
}
Run Code Online (Sandbox Code Playgroud)

Maa*_*wes 7

在我看来,您需要解密私钥才能使用它。当前未使用您的密码参数。不幸的是,要找出如何做到这一点似乎并不那么容易。


与其他许多Java API一样,Bouncy Castle也使用密码处理程序来检索密码。这样做的原因是允许程序仅在需要时才询问用户密码。这使程序可以在最短的时间内将密码保留在内存中。

因此,为了进行解密,请使用以下构造函数:

PemReader(TextReader reader, IPasswordFinder pFinder);
Run Code Online (Sandbox Code Playgroud)

的实现IPasswordFinder(用于C#的Bouncy Castle主要是Java端口,否则可能会使用委托)。


为了您的方便,代码。该代码还重构了整个密钥对,而不仅仅是私有密钥。

导入语句:

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Prng;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
using System.IO;
Run Code Online (Sandbox Code Playgroud)

解码器:

private static AsymmetricCipherKeyPair DecodePrivateKey(string encryptedPrivateKey, string password)
{
    TextReader textReader = new StringReader(encryptedPrivateKey);
    PemReader pemReader = new PemReader(textReader, new PasswordFinder(password));
    object privateKeyObject = pemReader.ReadObject();
    RsaPrivateCrtKeyParameters rsaPrivatekey = (RsaPrivateCrtKeyParameters)privateKeyObject;
    RsaKeyParameters rsaPublicKey = new RsaKeyParameters(false, rsaPrivatekey.Modulus, rsaPrivatekey.PublicExponent);
    AsymmetricCipherKeyPair kp = new AsymmetricCipherKeyPair(rsaPublicKey, rsaPrivatekey);
    return kp;
}
Run Code Online (Sandbox Code Playgroud)

所需的帮助程序类:

private class PasswordFinder : IPasswordFinder
{
    private string password;

    public PasswordFinder(string password)
    {
        this.password = password;
    }


    public char[] GetPassword()
    {
        return password.ToCharArray();
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,通常只应使用char[]而不是string密码,因为char[]使用后可以清除密码,但string不能。

现在您拥有私钥解密应该很容易。