Shaun Wilde">
Click here to Skip to main content
15,356,336 members
Articles / Programming Languages / C++
Article
Posted 20 Nov 2000

Stats

117K views
33 bookmarked

Hex Encoder and Decoder using Crypto++

Rate me:
Please Sign up or sign in to vote.
3.31/5 (8 votes)
20 Nov 2000CPOL
Encode binary data to and from hexadecimal format using the Crypto++ library

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shaun Wilde
Employed (other) Purplebricks
Australia Australia
All articles are supplied as-is, as a howto on a particular task that worked for me in the past. None of the articles are supposed to be out-of-the-box freeware controls and nor should they be treated as such. Caveat emptor.

Now living and working in Australia, trying to be involved in the local .NET and Agile communities when I can.

I spend a good chunk of my spare time building OpenCover and maintaining PartCover both of which are Code Coverage utilities for .NET.

Comments and Discussions

 
QuestionFunction HexEncoder::Close not found [modified] Pin
luozhaotian20-Dec-08 17:16
Memberluozhaotian20-Dec-08 17:16 
AnswerRe: Function HexEncoder::Close not found Pin
Shaun Wilde21-Dec-08 0:25
MemberShaun Wilde21-Dec-08 0:25 
GeneralCrypto++ Integration Article Pin
Jeffrey Walton8-Dec-06 18:33
MemberJeffrey Walton8-Dec-06 18:33 
GeneralRe: Crypto++ Integration Article Pin
Shaun Wilde8-Dec-06 19:34
MemberShaun Wilde8-Dec-06 19:34 
QuestionHelp me please?? Pin
xxhimanshu4-Nov-03 23:12
Memberxxhimanshu4-Nov-03 23:12 
AnswerRe: Help me please?? Pin
Shaun Wilde5-Nov-03 7:38
MemberShaun Wilde5-Nov-03 7:38 
Generalwe need more help Pin
4-Feb-02 21:52
suss4-Feb-02 21:52 
GeneralRe: we need more help Pin
SAWilde5-Feb-02 9:21
MemberSAWilde5-Feb-02 9:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.