【问题标题】:The requested operation is not supported in CngKey.CreateCngKey.Create 不支持请求的操作
【发布时间】: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 IsolationRemote Procedure Call (RPC) 服务正在运行

任何帮助将不胜感激。

【问题讨论】:

  • PKI 中不存在基于 SHA 的密钥 (CngAlgorithm.Sha1)。 RSA、ECDSA 等都是有效的选项。

标签: c# .net .net-4.0 certificate self-signed


【解决方案1】:

小题外话:在谷歌搜索这个问题时发现a site with HRESULT descriptions和SO和MSDN上方便的搜索工具(我只是用谷歌搜索你的HRESULT代码-2146893783


我发现了一个topic on MSDN,其中包含与HRESULT类似的代码失败,作者提供了一个link to MSDN article about CNG

NCRYPT_ALGORITHM_GROUP_PROPERTY L“算法组”
一个以 null 结尾的 Unicode 字符串,其中包含对象的算法组的名称。此属性仅适用于键。 Microsoft 密钥存储提供程序返回以下标识符:

  • NCRYPT_RSA_ALGORITHM_GROUP
    “RSA”,RSA 算法组。
  • NCRYPT_DH_ALGORITHM_GROUP
    “DH”,Diffie-Hellman 算法组。
  • NCRYPT_DSA_ALGORITHM_GROUP
    “DSA”,DSA 算法组。
  • NCRYPT_ECDSA_ALGORITHM_GROUP
    “ECDSA”,elliptic curve DSA 算法组。
  • NCRYPT_ECDH_ALGORITHM_GROUP
    “ECDH”,elliptic curve Diffie-Hellman 算法组。

我还在 MSDN 上找到了一篇关于 CNG Key Storage Providers 的文章,其中包含类似的算法列表:

  • 迪菲-赫尔曼 (DH)
    秘密协议和密钥交换,512 to 4096 in 64-bit increments
  • 数字签名算法 (DSA) 签名,512 to 1024 in 64-bit increments
  • 椭圆曲线 Diffie-Hellman (ECDH) 秘密协议和密钥交换,P256, P384, P521
  • 椭圆曲线数字签名算法 (ECDSA) 签名,P256, P384, P521
  • RSA 非对称加密和签名,512 to 16384 in 64-bit increments

所以,正如您所说,您只尝试了Sha1Sha256Sha512MD5,也许您只是使用另一个algorithm from list available?你可以找到上面提到的那些:

Here other developers successfully created 其中之一并且能够将其导出:

var cngKey = CngKey.Create(CngAlgorithm.ECDiffieHellmanP256, null,
    new CngKeyCreationParameters { ExportPolicy = CngExportPolicies.AllowPlaintextExport });

【讨论】:

  • 这确实是我的问题。谢谢你。您链接的 CNG 密钥存储提供程序页面指出“Microsoft 从 Windows Vista 和 Windows Server 2008 开始安装以下 KSP。供应商可以创建和安装其他提供程序。”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 2016-02-14
  • 2022-09-27
  • 2017-11-09
相关资源
最近更新 更多