我正在尝试在 C# 程序集中(以编程方式)动态生成自签名证书(目标.NET 4.0
),作为根CA来生成其他证书。该证书不需要保留在 Windows 证书存储中,我会将其导出为文件。
通读这个问题 https://stackoverflow.com/q/13806299/3136474(特别是,@dthorpe的回答 https://stackoverflow.com/a/13830861/3136474),我决定尝试一下公共运行库安全 http://clrsecurity.codeplex.com/.
The CLR Security
库添加了扩展方法CngKey类 https://msdn.microsoft.com/en-us/library/system.security.cryptography.cngkey(v=vs.110).aspx生成自签名证书,但我无法成功创建实例CngKey
with:
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
消息=不支持请求的操作。
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 https://msdn.microsoft.com/en-us/library/ms838991.aspx)
- 在 Windows Server 2012 机器上尝试过,同样的错误
- 该进程以管理员身份运行(因此它可以访问所有证书存储)
- 服务
CNG Key Isolation
and Remote Procedure Call (RPC)
正在跑步
任何帮助,将不胜感激。