[英]BERDecodeError when trying to verify a signature using a public key
I am writing a project about the AODV routing protocol in MANETs (mobile ad hoc networks) and one of things I am aiming to do is to add a digital signature to a field in one of the protocol's packets.我正在编写一个关于 MANET(移动自组织网络)中的 AODV 路由协议的项目,我的目标之一是向协议数据包中的一个字段添加数字签名。
I am using NS3 to simulate the network which is based on C++.我正在使用 NS3 来模拟基于 C++ 的网络。 For the signing, I am using Crypto++'s RSA library.对于签名,我使用 Crypto++ 的 RSA 库。 Basically, a node generates a public and private key pair to sign the packet, includes its public key in the packet, then sends it to another node.基本上,节点生成公钥和私钥对来签署数据包,将其公钥包含在数据包中,然后将其发送到另一个节点。 The receiving node then uses the public key provided in the packet to verify the signature.然后接收节点使用数据包中提供的公钥来验证签名。 The public key in the packet is encoded as a hex string with std::string
data type.数据包中的公钥被编码为std::string
数据类型的十六进制字符串。
I have already confirmed that the public key is correct and the problem solely lies on decoding the public key string from hex format.我已经确认公钥是正确的,问题仅在于从十六进制格式解码公钥字符串。
These are the functions I use to generate key pairs, sign, and verify (I got these from http://marko-editor.com/articles/cryptopp_sign_string/ ):这些是我用来生成密钥对、签名和验证的函数(我从http://marko-editor.com/articles/cryptopp_sign_string/得到这些):
struct KeyPairHex {
std::string publicKey;
std::string privateKey;
};
KeyPairHex
RoutingProtocol::RsaGenerateHexKeyPair(unsigned int aKeySize) {
KeyPairHex keyPair;
// PGP Random Pool-like generator
AutoSeededRandomPool rng;
// generate keys
RSA::PrivateKey privateKey;
privateKey.GenerateRandomWithKeySize(rng, aKeySize);
RSA::PublicKey publicKey(privateKey);
// save keys
publicKey.Save( HexEncoder(
new StringSink(keyPair.publicKey)).Ref());
privateKey.Save(HexEncoder(
new StringSink(keyPair.privateKey)).Ref());
return keyPair;
}
std::string
RoutingProtocol::RsaSignString(const std::string &aPrivateKeyStrHex,
const std::string &aMessage) {
// decode and load private key (using pipeline)
RSA::PrivateKey privateKey;
privateKey.Load(StringSource(aPrivateKeyStrHex, true,
new HexDecoder()).Ref());
// sign message
std::string signature;
RSASS<PKCS1v15, SHA>::Signer signer(privateKey);
AutoSeededRandomPool rng;
StringSource ss(aMessage, true,
new SignerFilter(rng, signer,
new HexEncoder(
new StringSink(signature))));
return signature;
}
bool
RoutingProtocol::RsaVerifyString(const std::string &aPublicKeyStrHex,
const std::string &aMessage,
const std::string &aSignatureStrHex) {
// decode and load public key (using pipeline)
RSA::PublicKey publicKey;
publicKey.Load(StringSource(aPublicKeyStrHex, true,
new HexDecoder()).Ref());
// decode signature
std::string decodedSignature;
StringSource ss(aSignatureStrHex, true,
new HexDecoder(
new StringSink(decodedSignature)));
// verify message
bool result = false;
RSASS<PKCS1v15, SHA>::Verifier verifier(publicKey);
StringSource ss2(decodedSignature + aMessage, true,
new SignatureVerificationFilter(verifier,
new ArraySink((byte*)&result, sizeof(result))));
return result;
}
I am using a keysize of 384. Here is the public key:我使用的密钥大小为 384。这是公钥:
304A300D06092A864886F70D01010105000339003036023100CC112A0E007C6329F813BC96498AFE
DF580EE4F708C2A923F6C6257FA4CC5FE2BA711C75CDE02036839E67C9B62720B3020111
There is no problem with signing the message.对消息进行签名没有问题。 However, when loading the key from the string I keep getting an error.但是,从字符串加载密钥时,我不断收到错误消息。
The error I am getting is:我得到的错误是:
terminate called after throwing instance of 'BerDecodeErr' what(): BER decode error
The Crypto++ code looks OK. Crypto++ 代码看起来不错。 I think the private key is ill-formed:我认为私钥格式不正确:
$ echo -n 304A300D06092A864886F70D01010105000339003036023100CC112A0E007C6329F813
BC96498AFEDF580EE4F708C2A923F6C6257FA4CC5FE2BA711C75CDE02036839E67C9B62720B30201
11 | hex -d | dumpasn1 -
0 74: SEQUENCE {
2 13: SEQUENCE {
4 9: OBJECT IDENTIFIER rsaEncryption (1 2 840 113549 1 1 1)
15 0: NULL
: }
17 57: BIT STRING
: 30 36 02 31 00 CC 11 2A 0E 00 7C 63 29 F8 13 BC
: 96 49 8A FE DF 58 0E E4 F7 08 C2 A9 23 F6 C6 25
: 7F A4 CC 5F E2 BA 71 1C 75 CD E0 20 36 83 9E 67
: C9 B6 27 20 B3 02 01 11
: }
And then the inner BIT_STRING
:然后是内部BIT_STRING
:
$ echo -n 3036023100CC112A0E007C6329F813BC96498AFEDF580EE4F708C2A923F6C6257FA4CC
5FE2BA711C75CDE02036839E67C9B62720B3020111 | hex -d | dumpasn1 -
0 54: SEQUENCE {
2 49: INTEGER
: 00 CC 11 2A 0E 00 7C 63 29 F8 13 BC 96 49 8A FE
: DF 58 0E E4 F7 08 C2 A9 23 F6 C6 25 7F A4 CC 5F
: E2 BA 71 1C 75 CD E0 20 36 83 9E 67 C9 B6 27 20
: B3
53 1: INTEGER 17
: }
That looks like a public key, not a private key.这看起来像一个公钥,而不是一个私钥。 A RSA public key is {n,e}
, while a RSA private key in short form is {n,e,d}
. RSA 公钥是{n,e}
,而简短形式的 RSA 私钥是{n,e,d}
。
I'm guessing the problem is somewhere around here in your real code.我猜问题出在您的真实代码中。 aPrivateKeyStrHex
is not a private key: aPrivateKeyStrHex
不是私钥:
// generate keys
RSA::PrivateKey privateKey;
privateKey.GenerateRandomWithKeySize(rng, aKeySize);
RSA::PublicKey publicKey(privateKey);
// save keys
publicKey.Save( HexEncoder(
new StringSink(keyPair.publicKey)).Ref());
privateKey.Save( HexEncoder(
new StringSink(keyPair.privateKey)).Ref());
There are some pages in the Crypto++ wiki that cover RSA, like RSA Cryptography and RSA Signature Schemes . Crypto++ wiki 中有一些页面涉及 RSA,例如RSA Cryptography和RSA Signature Schemes 。 But I think your Crypto++ code is OK.但我认为你的 Crypto++ 代码没问题。
Here is a minimal reproducer, if interested:如果有兴趣,这是一个最小的复制器:
#include "cryptlib.h"
#include "filters.h"
#include "osrng.h"
#include "rsa.h"
#include "hex.h"
#include <iostream>
#include <iomanip>
#include <string>
const std::string strKey =
"304A300D06092A864886F70D01010105000339003036023100CC112A0E00"
"7C6329F813BC96498AFEDF580EE4F708C2A923F6C6257FA4CC5FE2BA711C"
"75CDE02036839E67C9B62720B3020111";
int main(int argc, char* argv)
{
using namespace CryptoPP;
RSA::PrivateKey privateKey;
StringSource key(strKey, true, new HexDecoder);
privateKey.Load(key);
AutoSeededRandomPool prng;
if (privateKey.Validate(prng, 3) != true)
throw std::runtime_error("Failed to validate private key");
std::cout << std::hex << privateKey.GetModulus() << std::endl;
std::cout << std::hex << privateKey.GetPublicExponent() << std::endl;
std::cout << std::hex << privateKey.GetPrivateExponent() << std::endl;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.