Introduction
The Crypto++ library is a freeware library of cryptographic
schemes, written by Wei Dai.
However the library also contains other useful classes which one is not made immediately aware of when you use the
library. Two of these are the HexEncoder
and HexDecoder
classes which can be
used to (surprise, surprise) encode and decode data to and from the hexadecimal text format
e.g. 0-9,A-F.
Encoding
Encoding is very simple, say we had some data pData
that was of length dwLen
, that we wished to encode
and store in pData2
which is of length dwLen
*2,
then
#include <hex.h> // from crypto++ library
HexEncoder hexEncoder;
hexEncoder.Put(pData,dwLen);
hexEncoder.Close();
hexEncoder.Get(pData2,dwLen*2);
And there we have it. It is also possible to add multiple blocks of data to
the encoders stream i.e.
HexEncoder hexEncoder;
hexEncoder.Put(pDataA,dwLenA);
hexEncoder.Put(pDataB,dwLenB);
hexEncoder.Put(pDataC,dwLenC);
hexEncoder.Close();
hexEncoder.Get(pData2,(dwLenA+dwLenB+dwLenC)*2);
Decoding
Decoding is equally simple.
HexDecoder hexDecoder;
hexDecoder.Put(pData,dwLen);
hexDecoder.Close();
hexDecoder.Get(pData2,dwLen/2);
Remarks
Thanks to Wei Dai for his permission to write this article in what is hoped
to be a series of articles on the use of his Crypto++ library.