【问题标题】:Importing exported CngKey to RSA and exporting it again将导出的 CngKey 导入 RSA 并再次导出
【发布时间】:2018-07-27 08:33:37
【问题描述】:

我以CngKeyBlobFormat.Pkcs8PrivateBlob 格式导出密钥,然后尝试将此信息导入新的CngKey,然后再次尝试导出新密钥。问题是,当您简单地使用CngKey.Import() 导入密钥时,您无法选择参数,也无法选择密钥的名称,因此我无法再次导出它,因为默认参数不允许您这样做。于是我写了如下代码:

// Import key into RSACng rsa
var key = rsa.Key.Export(CngKeyBlobFormat.Pkcs8PrivateBlob);

CngProvider cp = new CngProvider("NewProvider");
CngKeyCreationParameters ckcp = new CngKeyCreationParameters() { ExportPolicy=CngExportPolicies.AllowPlaintextExport, Provider=cp};
ckcp.Parameters.Add(new CngProperty(CngKeyBlobFormat.Pkcs8PrivateBlob.Format, key, CngPropertyOptions.None));
CngKey cngKey2 = CngKey.Create(CngAlgorithm.Rsa, "OldKey", ckcp);

RSACng rsa2 = new RSACng(cngKey2);
var exportedKey = rsa2.Key.Export(CngKeyBlobFormat.Pkcs8PrivateBlob);
// exportedKey.Equals(key) == true

CngKey.Create 出现错误:

System.Security.Cryptography.CryptographicException: '未知错误“-1073741275”。

【问题讨论】:

    标签: c# .net cryptography cng


    【解决方案1】:

    它报告为未知错误很奇怪。该值为STATUS_NOT_FOUND(“找不到对象。”)。

    最可能的问题是您没有名为"NewProvider" 的注册提供程序。这不是它要求的密钥容器的名称,而是它应该将密钥发送到哪个库。

    99% 的时间你想要CngProvider.MicrosoftSoftwareKeyStorageProvider。 1% 的时间你想要CngProvider.MicrosoftSmartCardKeyStorageProvider。几乎无法估量的小,你想要一些其他的价值。

    【讨论】:

      猜你喜欢
      • 2017-09-29
      • 2012-03-13
      • 2020-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多