【发布时间】:2016-12-01 14:02:25
【问题描述】:
我正在尝试在 C# 程序集(以 .NET 4.0 为目标)中动态(以编程方式)生成自签名证书,以用作生成其他证书的根 CA。证书不需要保存在 Windows 证书存储中,我将其导出为文件。
阅读this question(尤其是@dthorpe's answer)后,我决定尝试CLR Security。
CLR Security 库在CngKey class 上放置了一个扩展方法以生成自签名证书,但我无法成功创建CngKey 的实例:
var key = CngKey.Create(CngAlgorithm.Sha1); //same with Sha256, Sha512 and MD5
//or
var key = CngKey.Create(CngAlgorithm.Sha1, null, new CngKeyCreationParameters()
{
ExportPolicy = CngExportPolicies.AllowExport,
KeyUsage = CngKeyUsages.AllUsages,
KeyCreationOptions = CngKeyCreationOptions.MachineKey,
});
以下任何一行都会引发异常:
System.Security.Cryptography.CryptographicException 未处理
HResult=-2146893783
Message=不支持请求的操作。
Source=System.Core
StackTrace:
at System.Security.Cryptography.NCryptNative.CreatePersistedKey(SafeNCryptProviderHandle provider, String algorithm, String name, CngKeyCreationOptions options)
at System.Security.Cryptography.CngKey.Create(CngAlgorithm algorithm, String keyName, CngKeyCreationParameters creationParameters)
at System.Security.Cryptography.CngKey.Create(CngAlgorithm algorithm)
at Tests.Program.Main(String[] args) at Program.cs:line 51
通过 SO 和互联网搜索,我检查了以下内容:
- 我正在运行一个 Windows 7 机器(因此它支持 RPC 根据MSDN)
- 在 Windows Server 2012 机器上尝试过,同样的错误
- 该进程以管理员身份运行(因此无论如何它都可以访问所有证书存储)
-
CNG Key Isolation和Remote Procedure Call (RPC)服务正在运行
任何帮助将不胜感激。
【问题讨论】:
-
PKI 中不存在基于 SHA 的密钥 (
CngAlgorithm.Sha1)。 RSA、ECDSA 等都是有效的选项。
标签: c# .net .net-4.0 certificate self-signed