该类的Export
功能X509Certificate2
允许您将带有私钥的证书导出到字节数组。
以下代码演示了使用私钥导出证书:
X509Store store = new X509Store(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2 cert = store.Certificates[1];
// Export the certificate including the private key.
byte[] certBytes = cert.Export(X509ContentType.Pkcs12);
要保护您导出的证书,请使用以下Export
函数重载:
byte[] certBytes = cert.Export(X509ContentType.Pkcs12, "SecurePassword");
开始编辑
要导入证书,请使用以下代码:
X509Certificate2 certToImport = new X509Certificate2(arr, "SecurePassword");
// To mark it as exportable use the following constructor:
X509Certificate2 certToImport = new X509Certificate2(arr, "SecurePassword", X509KeyStorageFlags.Exportable);
// certToImport.HasPrivateKey must be true here!!
X509Store store2 = new X509Store(StoreName.TrustedPublisher,
StoreLocation.CurrentUser);
store2.Open(OpenFlags.MaxAllowed);
store2.Add(certToImport);
store2.Close();
结束编辑