6

I want to get the x509 certificate as a string (certString) so that I can use it like

var cert = new X509Certificate2(Convert.FromBase64String(certString));

to generate a CertObject in Code.

I have tried around with certUtil but I dont know exactly which string I need.

Which string do I need to extract from the pfx data to be able to generate the X509 Certificate object in Code?

EngelbertCoder
  • 777
  • 2
  • 9
  • 29
  • 2
    Something like this I think: `var certString = Convert.ToBase64String(File.ReadAllBytes(@"c:\whateverpath.pfx"));` – Alexander Derck Apr 12 '16 at 08:11
  • I think that your question is not very clear. Please add more explanation. What exactly is the input? a PFX file on the file system? – Yacoub Massad Apr 12 '16 at 08:27

2 Answers2

12

Here is the full code sample:

var cert = new X509Certificate2(@"c:\myCert.pfx", "password");
var certBytes = cert.RawData;
var certString = Convert.ToBase64String(certBytes);
Samuel S.
  • 141
  • 4
5

All you need to do is converting it to byte[] then base64 string:

ConvertCertToBase64(cert.RawData);

private string ConvertCertToBase64(byte[] certRawData)
{
    return Convert.ToBase64String(certRawData);
}
Orkun Bekar
  • 1,447
  • 1
  • 15
  • 36