在C#中导入ECC密钥

Import ECC key in C#
2021-06-10
  •  译文(汉语)
  •  原文(英语)

我正在尝试在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);
速聊1:
在调试器中查看pubkey时,是否有回车符?
速聊2:
不是在"解包器"之后
解决过程1

如果\n开始时没有,请更改:

pubkey = pubkey.Substring(27, pubkey.Length-27-25);

对此:

pubkey = pubkey.Substring(26, pubkey.Length-26-25);

随着27第一个角色被切断.

速聊1:
我忘记了第二个参数.现在试试.
速聊2:
在解码还是在密钥导入期间?因为这System.Convert.FromBase64String(pubkey);对我有用.
速聊3:
与以前相同,它是关键导入.
速聊4:
没有内在的异常吗?另外,请在调试器下验证字节是否正确(足以在此处检查第一个和最后一个字节).
速聊5:
也不例外.子串中有26:第一个字节为48,129,155,48,16,6,7最后一个字节为176、23、11、204、23

I'm trying to import a private and public signing key in C#. They are both elliptic curve secp521. The public key looks like:

-----BEGIN PUBLIC KEY-----
MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQA7WeRuZKFy1T3i4kmoNY2xAaWwAu7YI6aZUvWg/Hm
7Tf+0n7StfGtHlf0jkMLLtjF1Yv1FCKOGlJ+vIt0K1hI9wkA2BjmrjpMUqplBkmRklDOp5TEF9ss
laaPkr0wWf3WdmwvGTNIP1R3uvbSTEgvI02VS4Bz/f8Z4qzf3ZbAsBcLzBc=
-----END PUBLIC KEY-----

Tried importing with:

pubkey = pubkey.Substring(27, pubkey.Length-27-25); // remove wrapping
byte[] pkcs8Blob = Base64.Decode(pubkey);
CngKey k = CngKey.Import(pkcs8Blob, CngKeyBlobFormat.EccPublicBlob);

But this gives me the error "The parameter is incorrect" and nothing more. Any one got a idea, how to fix this?

Update: Realized it's only the private key that is pkcs8 so now trying with this:

X509Certificate cert = new X509Certificate(pkcs8Blob);

In java it's done like this:

X509EncodedKeySpec spec = new X509EncodedKeySpec(pkcs8Blob);
KeyFactory fact = KeyFactory.getInstance(algorithm, "SC");
ECPublicKey pubKey = (ECPublicKey) fact.generatePublic(spec);
Talk1:
Does pubkey have carriage returns in it when you look at it in the debugger?
Talk2:
Not after the "dewrapper"
Solutions1

If there is no \n at the beginning then change:

pubkey = pubkey.Substring(27, pubkey.Length-27-25);

to this:

pubkey = pubkey.Substring(26, pubkey.Length-26-25);

With 27 the first character gets cut off.

Talk1:
I forgot about the second parameter. Try now.
Talk2:
In decoding or during key import? Because this System.Convert.FromBase64String(pubkey); works for me.
Talk3:
It's key import, same as earlier.
Talk4:
No inner exception? Also please verify under the debugger that bytes are ok (it's enough to check the first and the last byte here).
Talk5:
No same exception. With 26 in the substring: first bytes are 48,129,155,48,16,6,7 last 176, 23, 11, 204, 23
转载于:https://stackoverflow.com/questions/19081164/import-ecc-key-in-c-sharp

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入