繁体   English   中英

如何在没有私钥的情况下在证书存储中安装证书?

[英]how to install the certificate in certificate store without private key?

我有以下两种方法,

GetCaCertificate方法读取包含私钥的 PFX 证书。

AddCertToStore方法将证书添加到证书存储中,这里我想将证书添加到证书存储中,没有私钥,我们如何避免呢?

在此处输入图像描述

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;
    }

获取证书公共部分的副本并将其添加到存储中。 也就是说,替换这一行:

store.Add(cert);

有了这个:

store.Add(new X509Certificate2(cert.RawData));

在这种情况下,您将只安装证书的公共部分,而不将其与私钥相关联。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM