我需要将由BouncyCastle生成的EC私钥转换为C#中的CngKey。最终,我试图创建一个可导入到Windows Key Store的PKCS12,并遵循发现的信息和代码示例here。EC私钥在C#中的CngKey#
的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);
搜索在网络上,在上述应该工作,例如,参见here。相反,我收到未知错误例外
(CryptographicException)at
System.Security.Cryptography.NCryptNative.ImportKey()
。如果我通过CngKeyBlobFormat.EccPrivateBlob
到CngKey.Import()
,我得到一个 无效数据异常。
作为.NET,CNG和Cryto的新手,我觉得我忽略了一些东西。任何想法,将不胜感激。
谢谢!