Parses OpenSSL public and private key components and returns a X509Certificate2 with RSA/RSACryptoServiceProvider. (Based on http://www.jensign.com/opensslkey/opensslkey.cs (Archive Link))
Project | NuGet |
---|---|
OpenSSL.PrivateKeyDecoder | |
OpenSSL.PublicKeyDecoder | |
OpenSSL.X509Certificate2.Provider |
Support for the following frameworks:
- .NET 2.0
- .NET 3.5
- .NET 4.5 and up
- .NET Standard 1.3 (also NET Core 1.1)
- .NET Standard 2.0 (also NET Core 2.0 and 2.1)
Support for decoding RSA Private Key
, Private Key
and Public Key
.
Generate public certificate + privatekey using:
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout private.key -out certificate_pub.crt
If you just want to decode the private key into RSAParameters, use the following code:
string privateKeyText = File.ReadAllText("private.key");
IOpenSSLPrivateKeyDecoder decoder = new OpenSSLPrivateKeyDecoder();
RSAParameters parameters = decoder.DecodeParameters(privateKeyText);
// do something with the parameters ...
If you want to decode the private key into a RSACryptoServiceProvider, use the following code:
string privateKeyText = File.ReadAllText("private.key");
IOpenSSLPrivateKeyDecoder decoder = new OpenSSLPrivateKeyDecoder();
RSACryptoServiceProvider cryptoServiceProvider = decoder.Decode(privateKeyText);
// Example: sign the data
byte[] hello = new UTF8Encoding().GetBytes("Hello World");
byte[] hashValue = cryptoServiceProvider.SignData(hello, CryptoConfig.MapNameToOID("SHA256"));
// Example: use the PrivateKey from above for signing a JWT token using Jose.Jwt:
string token = Jose.JWT.Encode(payload, cryptoServiceProvider, JwsAlgorithm.RS256);
string certificateText = File.ReadAllText("certificate_pub.crt");
string privateKeyText = File.ReadAllText("private.key");
ICertificateProvider provider = new CertificateFromFileProvider(certificateText, privateKeyText);
X509Certificate2 certificate = provider.Certificate;
// Example: use the PrivateKey from the certificate above for signing a JWT token using Jose.Jwt:
string token = Jose.JWT.Encode(payload, certificate.PrivateKey, JwsAlgorithm.RS256);
If you just want to decode the rsa public key into RSAParameters, use the following code:
Export the public key from the private key with openssl
openssl rsa -in private.key -out public.key -pubout
string publicKeyText = File.ReadAllText("public.key");
IOpenSSLPublicKeyDecoder decoder = new OpenSSLPublicKeyDecoder();
RSAParameters parameters = decoder.DecodeParameters(publicKeyText);