我需要將由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的新手,我覺得我忽略了一些東西。任何想法,將不勝感激。
謝謝!