Matthew
Matthew

Reputation: 4607

Importing the Private Key from the .pfx file

How can I import the private key of a certificate from a .pfx file? I have this code:

        X509Certificate2 cert = new X509Certificate2("C:/amazon.pfx", "hello", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
        string private_key = cert.PrivateKey.ToString();
        Console.WriteLine(private_key);

However, the output is:

System.Security.Cryptography.RSACryptoServiceProvider

How can I get the private key in string format?

Upvotes: 2

Views: 3455

Answers (1)

Kai
Kai

Reputation: 2013

I haven't tested it, but the MSDN documentation says that you're getting the private key with the following code

string private_key = cert.PrivateKey.ToXmlString(false);
Console.WriteLine(private_key);

http://msdn.microsoft.com/de-de/library/system.security.cryptography.x509certificates.x509certificate2.privatekey.aspx

Upvotes: 6

Related Questions