1

我需要将 BouncyCastle 生成的 EC 私钥转换为 C# 中的 CngKey。最终,我正在尝试创建一个可以导入 Windows 密钥存储的 PKCS12,并遵循此处找到的信息和代码示例。

EC密钥对生成如下:

    var ecKeyPairGenerator = new ECKeyPairGenerator("ECDSA");
    ECKeyGenerationParameters ecKeyGenParams = new ECKeyGenerationParameters(SecObjectIdentifiers.SecP384r1, new SecureRandom());
    AsymmetricCipherKeyPair pair = ecKeyPairGenerator.GenerateKeyPair();

创建一个 CngKey:

PrivateKeyInfo privKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(pair.Private);
CngKey cngPrivKey = CngKey.Import(privKeyStruct.GetDerEncoded(), CngKeyBlobFormat.Pkcs8PrivateBlob);

在网上搜索,以上应该可以工作,例如,请参见此处。相反,我收到一个未知错误异常

(加密异常)在 System.Security.Cryptography.NCryptNative.ImportKey()。如果我传递 CngKeyBlobFormat.EccPrivateBlobCngKey.Import(),我会得到一个无效的数据异常。

作为 .NET、CNG 和 Cryto 的新手,我觉得我忽略了一些东西。任何想法,将不胜感激。

谢谢!

4

0 回答 0