BER尝试使用公钥验证签名时的解码错误

问题描述 投票:1回答:1

我正在编写一个有关MANET(移动自组织网络)中AODV路由协议的项目,我要做的一件事就是在一个协议数据包的一个字段中添加数字签名。

我正在使用NS3来模拟基于C ++的网络。对于签名,我正在使用CryptoPP的RSA库。基本上,一个节点会生成一个公钥和私钥对来对数据包进行签名,将其公钥包含在数据包中,然后将其发送到另一个节点。然后,接收节点使用数据包中提供的公钥来验证签名。数据包中的公钥被编码为std :: 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
  CryptoPP::AutoSeededRandomPool rng;

  // generate keys
  CryptoPP::RSA::PrivateKey privateKey;
  privateKey.GenerateRandomWithKeySize(rng, aKeySize);
  CryptoPP::RSA::PublicKey publicKey(privateKey);

  // save keys
  publicKey.Save( CryptoPP::HexEncoder(
                    new CryptoPP::StringSink(keyPair.publicKey)).Ref());
  privateKey.Save(CryptoPP::HexEncoder(
                    new CryptoPP::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)
  CryptoPP::RSA::PrivateKey privateKey;
  privateKey.Load(CryptoPP::StringSource(aPrivateKeyStrHex, true,
                                         new CryptoPP::HexDecoder()).Ref());

  // sign message
  std::string signature;
  CryptoPP::RSASS<CryptoPP::PKCS1v15, CryptoPP::SHA>::Signer signer(privateKey);
  CryptoPP::AutoSeededRandomPool rng;

  CryptoPP::StringSource ss(aMessage, true,
                            new CryptoPP::SignerFilter(rng, signer,
                              new CryptoPP::HexEncoder(
                                new CryptoPP::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)


  CryptoPP::RSA::PublicKey publicKey;
  publicKey.Load(CryptoPP::StringSource(aPublicKeyStrHex, true,
                                        new CryptoPP::HexDecoder()).Ref());


  // decode signature
  std::string decodedSignature;
  CryptoPP::StringSource ss(aSignatureStrHex, true,
                            new CryptoPP::HexDecoder(
                              new CryptoPP::StringSink(decodedSignature)));

  // verify message
  bool result = false;
  CryptoPP::RSASS<CryptoPP::PKCS1v15, CryptoPP::SHA>::Verifier verifier(publicKey);
  CryptoPP::StringSource ss2(decodedSignature + aMessage, true,
                             new CryptoPP::SignatureVerificationFilter(verifier,
                               new CryptoPP::ArraySink((byte*)&result,
                                                       sizeof(result))));

  return result;
}

我使用的密钥大小为384。这是公共密钥:304A300D06092A864886F70D01010105000339003036023100CC112A0E007C6329F813BC96498AFEDF580EE4F708C2A923F6C6257FA4CC5FE2BA711C75CDE02036839E67C9B62720B3020111]

签名消息没有问题。但是,当从字符串中加载密钥时,我总是收到错误消息。

我得到的错误是:terminate called after throwing instance of 'CryptoPP::BerDecodeErr' what(): BER decode error

我正在编写一个有关MANET(移动自组织网络)中AODV路由协议的项目,我旨在做的一件事情是在协议的一个数据包中的字段上添加数字签名。 ...

c++ rsa digital-signature crypto++
1个回答
0
投票

Crypto ++代码看起来不错。我认为私钥格式不正确:

© www.soinside.com 2019 - 2024. All rights reserved.