Click here to Skip to main content
11,521,728 members (64,922 online)
Click here to Skip to main content

Hex Encoder and Decoder using Crypto++

, 20 Nov 2000 CPOL 95.2K 31
Rate this:
Please Sign up or sign in to vote.
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
Software Developer (Senior) MYOB
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.
Follow on   Twitter   Google+   LinkedIn

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
emeka4-Feb-02 21:52
memberemeka4-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    Rant Rant    Admin Admin   

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150604.1 | Last Updated 21 Nov 2000
Article Copyright 2000 by Shaun Wilde
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid