如何在没有私钥的情况下在证书库中安装证书?
how to install the certificate in certificate store without private key?
我有以下两种方法,
GetCaCertificate
方法读取包含私钥的 PFX 证书。
AddCertToStore
方法将证书添加到证书库中,这里我想在证书库中添加证书with-out私钥,如何避免是吗?
var rootCaCert = GetCaCertificate(@"C:\cert\ca-cert.pfx", "Password@123");
AddCertToStore(rootCaCert, StoreName.Root, StoreLocation.LocalMachine);
private static X509Certificate2 GetCaCertificate(string certPath, string password)
{
return new X509Certificate2(File.ReadAllBytes(certPath), password:password);
}
public static bool AddCertToStore(X509Certificate2 cert, StoreName storeName, StoreLocation storeLocation)
{
bool value;
try
{
using var store = new X509Store(storeName, storeLocation);
store.Open(OpenFlags.ReadWrite);
store.Add(cert);
store.Close();
value = true;
}
catch (Exception exception)
{
Console.WriteLine(exception);
value = false;
}
return value;
}
获取证书的 public 部分的副本并将其添加到商店。即替换这一行:
store.Add(cert);
有了这个:
store.Add(new X509Certificate2(cert.RawData));
在这种情况下,您将只安装 public 部分证书,而不将其与私钥相关联。
我有以下两种方法,
GetCaCertificate
方法读取包含私钥的 PFX 证书。
AddCertToStore
方法将证书添加到证书库中,这里我想在证书库中添加证书with-out私钥,如何避免是吗?
var rootCaCert = GetCaCertificate(@"C:\cert\ca-cert.pfx", "Password@123");
AddCertToStore(rootCaCert, StoreName.Root, StoreLocation.LocalMachine);
private static X509Certificate2 GetCaCertificate(string certPath, string password)
{
return new X509Certificate2(File.ReadAllBytes(certPath), password:password);
}
public static bool AddCertToStore(X509Certificate2 cert, StoreName storeName, StoreLocation storeLocation)
{
bool value;
try
{
using var store = new X509Store(storeName, storeLocation);
store.Open(OpenFlags.ReadWrite);
store.Add(cert);
store.Close();
value = true;
}
catch (Exception exception)
{
Console.WriteLine(exception);
value = false;
}
return value;
}
获取证书的 public 部分的副本并将其添加到商店。即替换这一行:
store.Add(cert);
有了这个:
store.Add(new X509Certificate2(cert.RawData));
在这种情况下,您将只安装 public 部分证书,而不将其与私钥相关联。