Click here to Skip to main content
Licence CPOL
First Posted 20 Nov 2000
Views 86,722
Bookmarked 29 times

Hex Encoder and Decoder using Crypto++

By | 20 Nov 2000 | Article
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)

About the Author

Shaun Wilde

Architect

United Kingdom United Kingdom

Member

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.
 
Been involved in programming from the early '80s. First on my Spectrum, then an Amstrad. Did lots of Fortran while at University before becoming involved in Forth, Prolog, Pascal, Occam, C and eventually C++. Eventually started programming on the Windows platform using Borland’s OWL framework, during my postgraduate years. When I started work in '94, I learnt MFC followed by COM and ATL and never looked back. Now working exclusively in .NET, working on WinForms, ASP.NET and the Compact Framework using C# and VB.NET. Using every bit of the .NET framework I can such as WebServices and writing my own controls for my .NET Portal, and for the Compact Framework. Still lots I don't know, but the end of the universe hasn't happened yet - I wonder if I'll have time?
 
I was a permanent employee for a number of companies until Dec 2000 before going independent after the DotCom I ws involved with went DotBomb. Now supplying consulting services to various institutions in London and surrounding regions i.e. UK Smile | :) .
 
Now living and working in Australia, trying to be involved in the local .NET and Agile communities. Also maintaining an open source code coverage tool called PartCover whilst thinking of developing a new one.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionFunction HexEncoder::Close not found [modified] Pinmemberluozhaotian17:16 20 Dec '08  
AnswerRe: Function HexEncoder::Close not found PinmemberShaun Wilde0:25 21 Dec '08  
GeneralCrypto++ Integration Article PinmemberJeffrey Walton18:33 8 Dec '06  
GeneralRe: Crypto++ Integration Article PinmemberShaun Wilde19:34 8 Dec '06  
QuestionHelp me please?? Pinmemberxxhimanshu23:12 4 Nov '03  
AnswerRe: Help me please?? PinmemberShaun Wilde7:38 5 Nov '03  
Generalwe need more help Pinmemberemeka21:52 4 Feb '02  
GeneralRe: we need more help PinmemberSAWilde9:21 5 Feb '02  

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.

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