【问题标题】:HexDecoder output emptyHexDecoder 输出为空
【发布时间】:2014-10-09 05:27:24
【问题描述】:

我对 cryptopp562 有一点问题(在 Debian 上重要的是)我有一个十六进制字符串,并试图将其转换为十进制整数。我在cryptopp中使用HexDecoder(因为我已经在项目中使用cryptopp来处理其他事情)。由于我不知道一步一步从十六进制字符串直接转到十进制整数的方法,因此我有一个十进制字符串的中间步骤。就这样

十六进制字符串 > 十进制字符串 > 十进制整数

但是我的管道似乎不正确,但我终其一生都无法弄清楚原因。我什至没有正确地将十六进制字符串转换为十进制字符串位,所以我的十进制整数只是不断读取 0。我过去使用过 Base64Encoder(和解码器)和 ZlibCompressor(和解压缩器)没有问题,所以这是有点尴尬,因为应该差不多。

std::string RecoveredDecimalString;
std::string RecoveredHex = "39"; //Hex, so would be 63 in decimal
CryptoPP::StringSource (RecoveredHex, true /*PumpAll*/,
    new CryptoPP::HexDecoder(
        new CryptoPP::StringSink(RecoveredDecimalString) /*StringSink*/
    )/*HexDecoder*/
);/*StringSource*/

但是就像我说的,在运行之后,RecoveredDecimalString.empty() 返回真。起初我以为是因为我错过了泵的所有参数,但添加它没有任何区别,仍然没有任何流动。

A similar question was asked (and answered) a year ago。答案是“阅读 cryptoPP wiki”,但我看不出我的代码与他们 wiki 上的代码有何不同。

我忘记了什么?我知道这将是非常小的东西。

【问题讨论】:

  • 在不了解 CryptoC++ 的情况下,只构造了一个 (temporary) CryptoPP::StringSource 对象。您不需要执行一些操作(即调用一些成员函数)来实际进行转换吗?
  • 我不需要对 CryptoPP 的其他部分这样做,但我想我还是会尝试你的建议,但不幸的是,CryptoPP::StringSource HexSource(RecoveredHex, true /*PumpAll*/, new CryptoPP::HexDecoder(new CryptoPP::StringSink(RecoveredDecimalString))); 仍然产生了空输出。
  • “阅读 cryptoPP wiki” - 我没有这么说;我提供了一个来自 wiki 的工作示例(我还编写了 wiki 示例)。

标签: c++ hex pipeline crypto++


【解决方案1】:
std::string RecoveredDecimalString;
std::string RecoveredHex = "39"; //Hex, so would be 63 in decimal
CryptoPP::StringSource (RecoveredHex, true /*PumpAll*/,
    new CryptoPP::HexDecoder(
        new CryptoPP::StringSink(RecoveredDecimalString) /*StringSink*/
    )/*HexDecoder*/
);/*StringSource*/

命名您的StringSource。在您的代码更新中,请注意 StringSource 被命名为 ss

std::string decoded;
std::string encoded = "39"; //Hex, so would be 63 in decimal
CryptoPP::StringSource ss(encoded, true /*PumpAll*/,
    new CryptoPP::HexDecoder(
        new CryptoPP::StringSink(decoded) /*StringSink*/
    )/*HexDecoder*/
);/*StringSource*/

某些版本的 GCC 在匿名声明方面存在问题。我前段时间追踪到 StringSink 析构函数运行得太快(在数据被抽取之前)。我想提交一份 GCC 错误报告,但我永远无法将其缩减到最小的情况。

你也可以表演:

std::string decoded;
std::string encoded = "39"; //Hex, so would be 63 in decimal

CryptoPP::HexDecoder decoder(new CryptoPP::StringSink(decoded));
decoder.Put(encoded.data(), encoded.size());
decoder.MessageEnd();

【讨论】:

  • 有趣的是,这似乎解决了它。我不知道某些版本的 GCC 有这个问题,但是当我想到,我最后一次这样做是在 Windows(VS 2008)中。这是我几年前迁移到 Debian 后第一次使用匿名声明,所以我需要注意一下。谢谢!
猜你喜欢
  • 2014-11-02
  • 2018-05-08
  • 2012-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-16
  • 2014-05-07
  • 2015-02-24
相关资源
最近更新 更多