2017-07-14 173 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沒有公鑰。任何幫助將不勝感激。

謝謝!