SwarmApps Code Blog

November 24, 2009

A simplification wrapper for the Crypto++ library

Filed under: c++, Cryptography — Tags: , , , , , , , , , — swarmapps @ 2:07 am

While working on a client-server based app, I suddenly came face to face with the need to secure and authenticate certain commands between the client and server.  I was certain there was a library out there to meet my needs and I was right.  I rapidly came across the Crypto++ library, a powerful set of cryptographic tools written and tested by experts.  I was in heaven!  Think of the time this has saved me!

There was only one problem.  The Crypto++ library is indeed a very powerful set of cryptographic tools, but I found the learning curve to get my first apps running with it to be very high.  I examined all the sample code I could find, worked through the API documentation, and examined the validation and test code before I finally started catching on.  Then I had to “grep” my way through the files to identify which headers I needed to include for a given function.

DISCLAIMER: All of this brainache is undoubtedly due to my own lack of skill and formal training.

Nevertheless, I overcame my deficiencies and learned how to encrypt and decrypt at will.  But, I was unhappy with the amount of extra code I had to put everywhere I wanted to encrypt and decrypt something.  So, I ended up writing a simplification wrapper to make my work easier.  My wrapper uses only a small subset of functionality and algorithms in Crypto++, but it makes using that subset very easy.

As a very small example, here is how you encrypt a given plaintext string:

string cipherText = BasicCryptoPPWrap::EncryptStringAES(“Encrypt me”, key, initVector, errFlag, errMsg);

The wrapper also implements a custom Advanced Encryption Standard protocol that stores the initial vector with the encrypted data, so you don’t have to track it yourself.  I have used this wrapper very successfully in my own work, I think you’ll find it useful too!

Wrapper functions:

  • Implements symmetrical AES encryption and decryption
  • Hex encoding of binary data
  • Hex decoding of hex-coded strings
  • Generic string and file encryption
  • Custom protocol string and file encryption
  • Random bit and byte generators
  • SHA-256 Hash generator

The wrapper is not a standalone solution.  It requires your code to link to libcryptopp and know where the cryptopp header files are found.  The Crypto++ website can help you set up your system.  Once you get the validation suite to run successfully, you will be able to use this wrapper.

Get the files here:

  • BasicCryptoPPWrap.h – The header file that makes it all happen
  • example.cpp – An example file that takes you through all the functions available in the wrapper

Good luck!  I release this wrapper into the public domain, but if you happen to use the code I wouldn’t mind hearing about it!  Credit might be nice too!

Mike

November 23, 2009

An easy, light-weight, dynamically adjustable, global logging class for C++

Filed under: c++, General Code — Tags: , , , , , , , — swarmapps @ 3:49 am

Every programmer eventually finds a need to display warnings, debug information, or just status updates as they build their code and watch it run.  Lots of times this takes the form of streaming text to std::cout. But, what if you only want certain information to print at certain times, or you’d like to suddenly change from logging to a terminal to logging to a file?

After several tries, I came up with my logger class.  Basically, using static class members and c-style macros, I came up with a logging method that turns on and off easily, allows easy redirection, and is easy to use everywhere in your code.

At it’s simplest, this logger is used as such:

#include <iostream>
#include "logger.h"

using namespace std;	

int main (int argc, char * const argv[]) {
	bool someLogType = true;
	LOG(someLogType) << "This will be logged to clog\n";
	return 0
}

But the logger class also offers globally accessible log types and the ability to redirect the output wherever you want. The following snippet shows this use.

#include <iostream>
#include <sstream>
#include "logger.h"

using namespace std;	

int main (int argc, char * const argv[]) {
	// Set these to turn various logs on or off dynamically
	NORMAL = true;
	DEBUG = false;
	VERBOSE = true;

	LOG(NORMAL) << "This will log\n";
	LOG(DEBUG) << "But this one won't\n";
	LLOG(VERBOSE) << "This one will log, but with the name of the logtype prepended to the message\n";

	// Now we redirect the log output to a custom location.  This could be a file or any ostream
	stringstream newLog;
	logger::setOutstream(newLog);

	LOG(NORMAL) << "This will log\n";
	LOG(DEBUG) << "But this one won't\n";
	LLOG(VERBOSE) << "This one will log, but with the name of the logtype prepended to the message\n";

	string results = newLog.str();
	cout << "In the new log: \n" << results << "\n";

	// Always reset the logger to clog before the program exits or you will get a BAD_ACCESS error
	// I'm still trying to work out that problem, but this is an easy fix for now
	logger::setOutstream(clog);
	return 0;

}

Get the code here:

Good luck and good coding!

Theme: WordPress Classic. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.