Skip to main content

Parses an OpenSSL private (rsa) key component and returns a RSACryptoServiceProvider or RSAParameters.

Total Downloads
2.1M
2,132,600
Published
Jun 13, 2025
1y ago

Get Started

dotnet add package OpenSSL.PrivateKeyDecoder --version 1.3.0.1
ProductVersions
.NET Standard
netstandard1.3netstandard2.0
Other
.NETFramework2.0.NETFramework3.5.NETFramework4.5

README

OpenSSL X509Certificate2 Provider

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))

Example

Generate public certificate + privatekey

Generate public certificate + privatekey using:

openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout private.key -out certificate_pub.crt
Code example 1 - decode private key into RSAParameters

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 ...
Code example 2 - decode private key into a RSACryptoServiceProvider

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);
Code example 3 - Create a X509 certificate and add private key
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);
Code example 4 - decode openssl RSA public key into RSAParameters

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);

Sponsors

Entity Framework Extensions and Dapper Plus are major sponsors and proud to contribute to the development of OpenSSL.PrivateKeyDecoder, OpenSSL.PublicKeyDecoder and OpenSSL.X509Certificate2.Provider.

Entity Framework Extensions

Dapper Plus

Downloads by Version

Similar & Alternative Packages

Hand-picked matching NuGet libraries based on tags & ecosystem

Depended On By

Find packages that declare this package as a dependency. NuGet does not provide a reverse-dependency endpoint, so candidates are checked on demand.

Adoption Assessment

Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.

Compatibility metadata lists 5 target frameworks; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.