2017-07-14 170 views
0

我需要将由BouncyCastle生成的EC私钥转换为C#中的CngKey。最终,我试图创建一个可导入到Windows Key Store的PKCS12,并遵循发现的信息和代码示例hereEC私钥在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.EccPrivateBlobCngKey.Import(),我得到一个 无效数据异常。

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

谢谢!

回答

0

事实证明,传递给CngKey.Import()方法的私钥的pkcs8内容应该编码私钥和公钥以使方法成功。这是符合为CngKeyBlobFormat.Pkcs8PrivateBlob性质的言论发现here

因此,新的问题是如何在BouncyCastle的产生,其中包括两个键私钥的PKCS8字节数组编码。 Pkcs8Generator不会这样做,因为AsymmetricKeyParameter没有公钥。任何帮助将不胜感激。

谢谢!