【发布时间】:2019-12-06 15:47:01
【问题描述】:
有一些简单的代码可以使用 .net core 2.2 将带有私钥的证书导入 Windows 证书存储区:
using (var store = new X509Store(StoreName.Root,StoreLocation.CurrentUser))
{
store.Open(OpenFlags.ReadWrite);
store.Add(cert);
store.Close();
}
还有一些简单的代码可以再次读取它:
using (var store = new X509Store(StoreName.Root,StoreLocation.CurrentUser))
{
store.Open(OpenFlags.ReadOnly);
var certCollection = store.Certificates.Find(X509FindType.FindBySubjectName, commonName, validOnly);
store.Close();
return certCollection;
}
但是,尽管证书已成功检索到 certCollection,但它的私钥为 null 且 hasPrivateKey 为 false,即使它们在之前的 Add 调用中不为 null 且为 true。这是为什么呢?
更新:
using (RSA rsa = RSA.Create(keySize)) {
CertificateRequest certRequest = new CertificateRequest(
subjectName,
rsa,
HashAlgorithmName.SHA512,
RSASignaturePadding.Pkcs1);
certRequest.CertificateExtensions
.Add(newX509SubjectKeyIdentifierExtension(certRequest.PublicKey, false));
return certRequest;
}
【问题讨论】:
-
Windows 不希望根存储中的证书具有关联的私钥(如果有,则将有一个副本知道我的存储中的私钥)。有可能 Windows 只是在将相关的密钥知识保存到存储中时将其擦除。
-
很高兴看到第一个示例中
cert的来源。 -
@Crypt32 Cert 只是内存中的 X509Certificate2;使用 C# 创建。
-
-
“使用 C# 创建” - 如何创建?从带有 EphemeralKeySet 标志的 PFX 打开?使用证书请求? (如果是后者,您可能没有创建持久密钥,并且临时密钥不会在存储添加中持久)
标签: c# .net-core certificate x509certificate2