我正在尝试在 C# 中导入私有和公共签名密钥。它们都是椭圆曲线secp521。公钥如下所示:
-----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);