Decoding Hex encoded value with Crypto ++

I am new to Cryptopp and I wanted to encode and decode text in order to understand how it works. The encoding part works fine, but I can't get the string to decode? The decoded string is always empty. I asked on the Crypto mailing list and someone said this code should work, but it doesn't.

I would like to know what is wrong. As a newbie to crypto, I can't see what is wrong.

Code:

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

      

+1


source to share


2 answers


There are a number of examples on the Crypto ++ wiki, including the use of HexEncoder and HexDecoder .

From the wiki:



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


source


The model used in the above answer is called the so called "pipelined" pattern in Crypto ++. See the Crypto ++ article on pipelining



Pay attention to the rules that apply to object ownership - if you pass a pointer to an object in the constructor, that object will be owned by the newly created object and deleted in its destructor. If an object is passed to the constructor by reference, the object must remain at the same level for the life of the newly created object, i.e. You retain ownership and must not remove it from under the nose of the new property!

0


source







All Articles