【问题标题】:Decoding Hex Encoded Value with Crypto++使用 Crypto++ 解码十六进制编码值
【发布时间】:2013-06-25 20:19:22
【问题描述】:

我是 Cryptopp 的新手,我想对文本进行编码并重新解码以了解它的工作原理。 编码部分工作正常,但我无法解码字符串?解码后的字符串始终为空。我在 Crypto mailing 中问过,有人说这段代码应该可以工作,但不能。

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

代码:

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)));

【问题讨论】:

    标签: c++ crypto++


    【解决方案1】:

    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
    

    【讨论】:

      【解决方案2】:

      上述答案中使用的模型在 Crypto++ 中被称为“管道”模式。见Crypto++ article on pipelining

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-28
        • 2012-06-26
        • 2013-06-10
        • 2020-06-15
        • 2019-03-20
        • 1970-01-01
        • 2019-12-27
        • 2019-11-22
        相关资源
        最近更新 更多