C# BouncyCastle PKCS#8

邮差的信 提交于 2020-04-18 06:10:42

问题


I want load the PEM using .net framework (not .netcore)

-----BEGIN ENCRYPTED PRIVATE KEY-----
MIHs........................................................CAAw
DAYI........................................................gZAf
Y/Iu........................................................X7DZ
ZKoE........................................................OYQQ
3ZST........................................................A2E=
-----END ENCRYPTED PRIVATE KEY-----
  • I tried to use the following code using BouncyCastle, but it throw PemException: "problem creating ENCRYPTED private key: Org.BouncyCastle.Crypto.InvalidCipherTextException: pad block corrupted"
 class Passowrd : IPasswordFinder
    {
        private string v;

        public Passowrd(string v)
        {
            this.v = v;
        }

        public char[] GetPassword()
        {
            return v.ToCharArray();
        }
    }

var pemReader = new PemReader(new StringReader(privateKeyText), new Passowrd("PASSWORD"));
var pemObj = pemReader.ReadObject(); // this line throw PemException
  • However, I load the exact same PEM file using .netcore3.1 by the following code:
    var ecdsa = ECDsa.Create();
    ecdsa.ImportEncryptedPkcs8PrivateKey(passSpan, privateKeyBytes, out _);

回答1:


You can read the PEM either using

var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(new StringReader(privateKeyText), new Passowrd("PASSWORD"));

or

var pemReader = new Org.BouncyCastle.Utilities.IO.Pem.PemReader(new StringReader(privateKeyText));

then read content like

var pemObject = pemReader.ReadPemObject();


来源:https://stackoverflow.com/questions/61241727/c-sharp-bouncycastle-pkcs8

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