Author Topic: PFX - Extract RSA private key / certificate  (Read 114 times)

echodreamz

  • Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
PFX - Extract RSA private key / certificate
« on: May 24, 2018, 03:32:34 PM »
Is it possible with the PFX portion of the .net library to extract the RSA private key as well as the website specific certificate? Currently we have to use 3 OpenSSL commands...

Extract the private key
Code: [Select]
openssl pkcs12 -in filename.pfx -nocerts -nodes -out key.pem
Convert to the older RSA format
Code: [Select]
openssl pkcs8 -in key.pem -out rsakey.pem
Extract the certificate
Code: [Select]
openssl pkcs12 -in filename.pfx -nokeys -out cert.pem
Would be great if we could do this directly with code instead of relying on starting processes.

echodreamz

  • Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: PFX - Extract RSA private key / certificate
« Reply #1 on: May 24, 2018, 04:07:30 PM »
Code: [Select]
           
            Chilkat.PrivateKey privKey = null;
            Chilkat.Cert cert = pfx.GetCert(0);

            cert.ExportCertPemFile(@"C:\cert.crt");
            privKey = cert.ExportPrivateKey();

            privKey.SaveRsaPemFile(@"C:\rsapem.key");
            privKey.SavePkcs1File(@"C:\pkcs1.key");
            privKey.SavePemFile(@"C:\pem.key");

            Console.WriteLine(privKey.GetRsaPem());

I noticed that SaveRsaPemFile and GetRsaPem have different outputs. The file has BEGIN PRIVATE KEY, while the GetRsaPem has BEGIN RSA PRIVATE KEY