使用加密货币加载ECDSA私钥
我正在尝试使用Crypto ++加载作为字节阵列给出的EC密钥。这是关键:
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIPQLO9zyl40X3lh1wbSR6S88aCsUvJr9R5n2pA3DbD9+oAoGCCqGSM49
AwEHoUQDQgAEs+nDydkW5F07yZPb/c05TSjzRJXCvD8Ni76ppfWJFOEOdM/WuHU6
zBMcdIzoY+LuqdZ8LgVlMBsnx8NwNvvFAA==
-----END EC PRIVATE KEY-----
这是与字节数组相同的密钥(假设我没有弄乱转换):
uint8_t server_priv_key_[] = {
0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0xf4, 0x0b, 0x3b, 0xdc, 0xf2,
0x97, 0x8d, 0x17, 0xde, 0x58, 0x75, 0xc1, 0xb4, 0x91, 0xe9, 0x2f, 0x3c,
0x68, 0x2b, 0x14, 0xbc, 0x9a, 0xfd, 0x47, 0x99, 0xf6, 0xa4, 0x0d, 0xc3,
0x6c, 0x3f, 0x7e, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xb3, 0xe9, 0xc3,
0xc9, 0xd9, 0x16, 0xe4, 0x5d, 0x3b, 0xc9, 0x93, 0xdb, 0xfd, 0xcd, 0x39,
0x4d, 0x28, 0xf3, 0x44, 0x95, 0xc2, 0xbc, 0x3f, 0x0d, 0x8b, 0xbe, 0xa9,
0xa5, 0xf5, 0x89, 0x14, 0xe1, 0x0e, 0x74, 0xcf, 0xd6, 0xb8, 0x75, 0x3a,
0xcc, 0x13, 0x1c, 0x74, 0x8c, 0xe8, 0x63, 0xe2, 0xee, 0xa9, 0xd6, 0x7c,
0x2e, 0x05, 0x65, 0x30, 0x1b, 0x27, 0xc7, 0xc3, 0x70, 0x36, 0xfb, 0xc5,
0x00,
};
最后,我正在加载这样的密钥:
ArraySource server_priv_key_source { server_priv_key_, sizeof(server_priv_key_), true };
ECDSA<ECP, SHA256>::PrivateKey server_priv_key;
server_priv_key.Load(server_priv_key_source);
但是,调用load 导致“ BER解码错误”异常。我在做什么错?
I'm trying to load an EC key given as a byte array using Crypto++. Here is the key:
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIPQLO9zyl40X3lh1wbSR6S88aCsUvJr9R5n2pA3DbD9+oAoGCCqGSM49
AwEHoUQDQgAEs+nDydkW5F07yZPb/c05TSjzRJXCvD8Ni76ppfWJFOEOdM/WuHU6
zBMcdIzoY+LuqdZ8LgVlMBsnx8NwNvvFAA==
-----END EC PRIVATE KEY-----
And here is the same key as a byte array (assuming I didn't mess up the conversion):
uint8_t server_priv_key_[] = {
0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0xf4, 0x0b, 0x3b, 0xdc, 0xf2,
0x97, 0x8d, 0x17, 0xde, 0x58, 0x75, 0xc1, 0xb4, 0x91, 0xe9, 0x2f, 0x3c,
0x68, 0x2b, 0x14, 0xbc, 0x9a, 0xfd, 0x47, 0x99, 0xf6, 0xa4, 0x0d, 0xc3,
0x6c, 0x3f, 0x7e, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xb3, 0xe9, 0xc3,
0xc9, 0xd9, 0x16, 0xe4, 0x5d, 0x3b, 0xc9, 0x93, 0xdb, 0xfd, 0xcd, 0x39,
0x4d, 0x28, 0xf3, 0x44, 0x95, 0xc2, 0xbc, 0x3f, 0x0d, 0x8b, 0xbe, 0xa9,
0xa5, 0xf5, 0x89, 0x14, 0xe1, 0x0e, 0x74, 0xcf, 0xd6, 0xb8, 0x75, 0x3a,
0xcc, 0x13, 0x1c, 0x74, 0x8c, 0xe8, 0x63, 0xe2, 0xee, 0xa9, 0xd6, 0x7c,
0x2e, 0x05, 0x65, 0x30, 0x1b, 0x27, 0xc7, 0xc3, 0x70, 0x36, 0xfb, 0xc5,
0x00,
};
Finally, I'm loading the key like this:
ArraySource server_priv_key_source { server_priv_key_, sizeof(server_priv_key_), true };
ECDSA<ECP, SHA256>::PrivateKey server_priv_key;
server_priv_key.Load(server_priv_key_source);
However, the call to Load
causes a "BER decode error" exception. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的私钥具有SEC1格式,但仅支持PKCS#8格式(请参阅 and 在这里),因此必须使用openssl转换钥匙,
例如在(PEM编码)中:
或作为字节数组(DER编码):
以这种格式,可以用已发布的代码导入密钥。
测试:
在以下代码中,导入了私钥,签署了一条消息,公共密钥是从导入的私钥派生的,然后成功验证了该消息:
Your private key has the SEC1 format, but only the PKCS#8 format is supported (see here and here), so the key has to be converted, e.g. with OpenSSL:
This results in (PEM encoded):
or as byte array (DER encoded):
In this format the key can be imported with the posted code.
Test:
In the following code, the private key is imported, a message is signed, the public key is derived from the imported private key, and the message is then successfully verified: