我正試圖在C#中導入私鑰和公鑰。它們都是橢圓曲線secp521。公鑰的樣子:在C#中導入ECC密鑰#
-----BEGIN PUBLIC KEY-----
MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQA7WeRuZKFy1T3i4kmoNY2xAaWwAu7YI6aZUvWg/Hm
7Tf+0n7StfGtHlf0jkMLLtjF1Yv1FCKOGlJ+vIt0K1hI9wkA2BjmrjpMUqplBkmRklDOp5TEF9ss
laaPkr0wWf3WdmwvGTNIP1R3uvbSTEgvI02VS4Bz/f8Z4qzf3ZbAsBcLzBc=
-----END PUBLIC KEY-----
試圖用進口:
pubkey = pubkey.Substring(27, pubkey.Length-27-25); // remove wrapping
byte[] pkcs8Blob = Base64.Decode(pubkey);
CngKey k = CngKey.Import(pkcs8Blob, CngKeyBlobFormat.EccPublicBlob);
但是,這給我的錯誤「參數不正確」,僅此而已。任何人有一個想法,如何解決這個問題?
更新: 意識到這是隻有私鑰是PKCS8所以現在這個嘗試:
X509Certificate cert = new X509Certificate(pkcs8Blob);
在java中,它的完成這樣的:
X509EncodedKeySpec spec = new X509EncodedKeySpec(pkcs8Blob);
KeyFactory fact = KeyFactory.getInstance(algorithm, "SC");
ECPublicKey pubKey = (ECPublicKey) fact.generatePublic(spec);
當您在調試器中查看它時,pubkey是否有回車符? – yhw42
不是在「解包器」之後 – user1930848