使用Crypto ++解码十六进制编码值

时间:2013-06-25 20:19:23

标签: c++ crypto++

我是Cryptopp的新手,我想对文本和解码进行编码以了解其工作原理。 编码部分工作正常,但我无法解码字符串?始终解码的字符串为空。我在Crypto邮件中询问过,有人说这段代码应该有效,但事实并非如此。

我想知道出了什么问题。 作为加密新手,我看不出有什么问题。

代码:

std::string encoded = m_pkey->GetValue().ToStdString();//here under debugger its ok
std::string decoded;
CryptoPP::StringSource(encoded, true, new CryptoPP::HexDecoder(new CryptoPP::StringSink(decoded)));

2 个答案:

答案 0 :(得分:2)

Crypto ++ wiki有很多例子,包括使用HexEncoderHexDecoder类。

来自维基:

byte decoded[] = { 0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88, 
                   0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00 };
string encoded;

StringSource ss(decoded, sizeof(decoded), true,
    new HexEncoder(
        new StringSink(encoded)
    ) // HexEncoder
); // StringSource

cout << encoded << endl;
...

$ ./cryptopp-test.exe
FFEEDDCCBBAA99887766554433221100

答案 1 :(得分:0)

上述答案中使用的模型是Crypto ++中所谓的“管道”模式。见Crypto++ article on pipelining

请注意适用于对象所有权的规则 - 如果将指向对象的指针传递给构造函数,则该对象将由新构造的对象拥有并在其析构函数中删除。如果通过引用将对象传递给构造函数,则对象必须在新构造的对象的生命周期内保持存在,即您保留所有权并且不得从新对象的鼻子下删除它!