How C# / Netcore would like to use PEM file (Public side of things)


Publicc key are typically stored in a PEM file which contains modulus and exponent value. These get converted into RSAParameters so that it can be used by RSA to do its magic. It looks something lke this :-

General Idea 

var rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(p);

To use a nuget package called PemUtil. 

This allows you to read a PEM file into a RSAParameters. Go and install a nuget package called PemUtil and then you can use the following code. Then you can plug it into code above.

 public static RSAParameters ReadPEMFileB(string PemFilePath)
        {
            RSAParameters rsaParameters;
            using (var stream = File.OpenRead(PemFilePath))
            using (var reader = new PemReader(stream))
            {
                rsaParameters = reader.ReadRsaKey();
            }

            return rsaParameters;
        }



A sample PEMfile might look like this :-

-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCxSxqCESCdpeXWCprQGMrpwlEg
91EF4Qt1R247gTGaUMgEjFEq9c/UffmtYyccmoIq3n8m/ZcsiCNJNFfGx5h/YEdR
ZyzQjSLg9zt3zu5JbBNx4zb63vvhpCGoMf65y/RvMs+XjSBl8ybl0vbbrC62YH1I
7ZJYSbKhXr3ILHyUSwIDAQAB
-----END PUBLIC KEY-----


Comments

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm