从.pfx文件导入私钥

时间:2013-04-28 08:18:06

标签: c# encryption certificate key private

如何从.pfx文件导入证书的私钥?我有这段代码:

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

然而,输出是:

System.Security.Cryptography.RSACryptoServiceProvider

如何以字符串格式获取私钥?

1 个答案:

答案 0 :(得分:6)

我还没有测试过,但是MSDN文档说您使用以下代码获取私钥

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