使用私钥将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();
https://stackoverflow.com/questions/9810887/export-x509certificate2-to-byte-array-with-the-private-key
浙公网安备 33010602011771号