从.pfx文件导入私钥

Mat*_*hew 2 c# encryption private key certificate

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

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

但是,输出是:

System.Security.Cryptography.RSACryptoServiceProvider
Run Code Online (Sandbox Code Playgroud)

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

Kai*_*Kai 6

我没有测试它,但MSDN文档说您正在使用以下代码获取私钥

string private_key = cert.PrivateKey.ToXmlString(false);
Console.WriteLine(private_key);
Run Code Online (Sandbox Code Playgroud)

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