Reading PEM RSA Public Key Only using Bouncy Castle

前端 未结 6 1783
孤独总比滥情好
孤独总比滥情好 2020-12-24 00:50

I am trying to use C# to read in a .pem file that contains only a RSA public key. I do not have access to the private key information, nor does my application

6条回答
  •  粉色の甜心
    2020-12-24 01:08

    The following code will read a public key from a given filename. The exception handling should be changed for any production code. This method returns an AsymetricKeyParameter:

    public Org.BouncyCastle.Crypto.AsymmetricKeyParameter ReadAsymmetricKeyParameter(string pemFilename)
    {
        var fileStream = System.IO.File.OpenText(pemFilename);
        var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(fileStream);
        var KeyParameter = (Org.BouncyCastle.Crypto.AsymmetricKeyParameter)pemReader.ReadObject();
        return KeyParameter;
    }
    

提交回复
热议问题