2

When I do X509Certificate2 x509 = new X509Certificate2(certificateFile); what format does the certificate need to be in?

I have a private key:

-----BEGIN RSA PRIVATE KEY-----
......
-----END RSA PRIVATE KEY-----

and a certificate:

Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:
           ..............
        Signature Algorithm: sha1WithRSAEncryption
        Issuer: C=IL, ST=SS, L=...., O=....., OU=....., CN=...../emailAddress=.....
        Validity
            Not Before: Mar 19 14:45:09 2013 GMT
            Not After : Mar 19 14:45:09 2014 GMT
        Subject: C=IL, ST=SS, L=...., O=....., OU=....., CN=...../emailAddress=.....
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (2048 bit)
                Modulus:
                    ...................
                Exponent: 65537 (0x10001)
        X509v3 extensions:
            X509v3 Subject Key Identifier: 
                .........
            X509v3 Authority Key Identifier: 
                keyid:..........

            X509v3 Basic Constraints: 
                CA:TRUE
    Signature Algorithm: sha1WithRSAEncryption
        .....

Yet I am able to load neither using the C# code above. What am I missing?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
user2191209
  • 125
  • 1
  • 1
  • 5

1 Answers1

1

-----BEGIN RSA PRIVATE KEY----- is a header for an RSA Private Key in PKCS#1 format (unencrypted). This format is used in Private Key PEM files.

The .NET Framework does not offer any ready-made method to directly export a Private Key in this format so you have to implement it yourself.

This document can help you - there is an example of decoding a RSA Private Key. Encoding is just the reverse operation.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Steph
  • 70
  • 2