4

My steps are:

  1. Create X509Certificate2 with public key:

    X509Certificate2 clientCertificate = new X509Certificate2("public key certificate blob as byte[]");
    
  2. How do I want to load the private key blob to clientCertificate?

poupou
  • 43,413
  • 6
  • 77
  • 174
Megi Ben Nun
  • 405
  • 3
  • 6
  • 20
  • What kind of blobs do you have? Where they come from? – Dmitriy Konovalov Dec 08 '11 at 07:49
  • Does it metter? My public key is string in Base64 format. I convert this string to Byte array. The private key is the asymmetric key of the public key. The private key is in the same format as the public key (Base64) and i like to add it to my X509Certificate – Megi Ben Nun Dec 08 '11 at 08:29

3 Answers3

2

It looks like you can not import RSA private key with just .net framework tools.

Check out this thread How to read a PEM RSA private key from .NET.

Community
  • 1
  • 1
Dmitriy Konovalov
  • 1,777
  • 14
  • 14
  • Doesn't work... I guess the key is not RSACryptoServiceProvider. I need a general solution for this issue. My issue is - I am working with WLAN configuration, when my user configure wireless profile, he gives me public key in one file, and private key in another. – Megi Ben Nun Dec 08 '11 at 10:02
  • BTW - The certificate is RSA certificate. but when i am executing the next line - csp.ImportCspBlob(privateKeyBlob); , i am getting an exception... :( – Megi Ben Nun Dec 08 '11 at 10:05
  • Can you post it? And what file format is used as private key container? – Dmitriy Konovalov Dec 08 '11 at 10:40
  • 1
    I can't post the private key :(. – Megi Ben Nun Dec 08 '11 at 11:12
  • Oh, no! I mean exception text! You wrote: "but when i am executing the next line - csp.ImportCspBlob(privateKeyBlob); , i am getting an exception... :(". Can you post Exception details? – Dmitriy Konovalov Dec 08 '11 at 11:56
  • {System.Security.Cryptography.CryptographicException: Bad Version of provider. at System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr) at System.Security.Cryptography.Utils._ImportCspBlob(Byte[] keyBlob, SafeProvHandle hProv, CspProviderFlags flags, SafeKeyHandle& hKey) at System.Security.Cryptography.Utils.ImportCspBlobHelper(CspAlgorithmType keyType, Byte[] keyBlob, Boolean publicOnly, CspParameters& parameters, Boolean randomKeyContainer, SafeProvHandle& safeProvHandle, SafeKeyHandle& safeKeyHandle) .... – Megi Ben Nun Dec 08 '11 at 13:03
1

Just in case anyone like me and up looking at this old post when searching for how to generate X509Certificate2 from pem fil/private key:

The .Net 5.0 framework has a very simplified approach to this:

var certPem = File.ReadAllText("cert.pem");
var eccPem = File.ReadAllText("ecc.pem");
var cert = X509Certificate2.CreateFromPem(certPem, eccPem);

(source: https://www.scottbrady91.com/C-Sharp/PEM-Loading-in-dotnet-core-and-dotnet)

WeekendHacker
  • 196
  • 1
  • 6
0

Also see: How to import PKCS#8 RSA privateKey (created by OpenSSL) in C#

It includes a link to Mono's source code which can read PKCS#8 / PEM files and return an RSA instance from it.

Community
  • 1
  • 1
poupou
  • 43,413
  • 6
  • 77
  • 174