简体   繁体   中英

Extract file .cer and file .key from a pfx certificate

I use this code to create the .key files starting from a .pfx certificate and everything works fine.

            ////////////////////////////////////////////Create file .key

            string cParK = " pkcs12 -in certificate.pfx -out certificate.key -nocerts -nodes -nomacver -password pass:" + cPass;

            System.Diagnostics.ProcessStartInfo startInfo2 = new System.Diagnostics.ProcessStartInfo("openssl.exe", cParK);
            startInfo2.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo2.CreateNoWindow = true;

            System.Diagnostics.Process oProcess2 = System.Diagnostics.Process.Start(startInfo2);
            oProcess2.StartInfo.CreateNoWindow = true;
            oProcess2.WaitForExit();

Is there any way to create the same files without using openssl but only c #?

As for the first question, I also found the solution for exporting the key, perhaps it will also be useful for those who said it was not easy, instead it is very easy. Just follow this post. Solution of the problem

For now I have found a way to extract the .cer file:

        System.Security.Cryptography.X509Certificates.X509Certificate2 oCert =
        new System.Security.Cryptography.X509Certificates.X509Certificate2("certificate.pfx", "123456");

        Byte[] aCert = oCert.Export(System.Security.Cryptography.X509Certificates.X509ContentType.Cert);
        File.WriteAllBytes("certificato.cer", aCert);

(I deleted this part of the original question)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM