// BasicCryptoPPWrap example code
// By: Michael R. Rich, 2009
// swarmapps.wordpress.com
// This example code shows the use of all the BasicCryptoPPWrap functions
// This code is released to the public domain
#include <iostream>
#include <fstream>
#include "BasicCryptoPPWrap.h"
#include <string>
#include <sstream>
#include <stdio.h>
using namespace std;
int main (int argc, char * const argv[]) {
bool err;
string errMsg;
// Encrypt and decrypt a string with a random key and initialization vector (iv)
string key1 = BasicCryptoPPWrap::BitGen(128); // We'll make a random 128 bit key
string initVec1 = BasicCryptoPPWrap::ByteGen(16); // AES uses 16 byte blocks, so the initVector must be 16 bytes
// Here's what they look like
cout << "key1: " << BasicCryptoPPWrap::HexEncode(key1) << "\n";
cout << "initVec1: " << BasicCryptoPPWrap::HexEncode(initVec1) << "\n";
string plainText = "I don't want anyone to see this!!";
// encrypt it with the generic AES encryptor
string cipherText = BasicCryptoPPWrap::EncryptStringAES(plainText, key1, initVec1, err, errMsg);
if (err) {
cout << errMsg << "\n";
return -1;
}
// Here's what it looks like
cout << "Encrypted message: " << BasicCryptoPPWrap::HexEncode(cipherText) << "\n";
// Now let's recover it!
string recoveredText = BasicCryptoPPWrap::DecryptStringAES(cipherText, key1, initVec1, err, errMsg);
if (err) {
cout << errMsg << "\n";
return -1;
}
cout << "Plaintext Message 1: " << plainText << "\n";
cout << "Recovered Message 1: " << recoveredText << "\n";
// Now let's use the "MR" branded encryption and decryption. This just stores the iv in the encrypted string, so we don't have to manage it
// if you lose the iv, it's just like losing the key! "MR" methods generate a random, meaningless iv that is worthless without the key
// First lets generate a key from a plain text password
string key2 = BasicCryptoPPWrap::hashSHA256("This is my password");
plainText = "Four eyes only";
cipherText = BasicCryptoPPWrap::MREncryptStringAES(plainText, key2, err, errMsg);
if (err) {
cout << errMsg << "\n";
return -1;
}
// Now recover the text with the matching decryption method
recoveredText = BasicCryptoPPWrap::MRDecryptStringAES(cipherText, key2, err, errMsg);
if (err) {
cout << errMsg << "\n";
return -1;
}
cout << "Plaintext Message 2: " << plainText << "\n";
cout << "Recovered Message 2: " << recoveredText << "\n";
BasicCryptoPPWrap::cleanup();
// Now let's try files!
// We'll skip the generic encryption method and just use MR. MR stores the iv as the first 16 bytes of the file.
// Generate a file
ofstream recipe("SecretSteakRecipe.txt");
recipe << "Mike's Secret Steak Recipe\n1 ribeye\n1 bottle of Dale's Sauce\nStep 1: Marinate steak in ample amounts of Dale's Sauce for 15 minutes\n";
recipe << "Step 2: Grill steak, flipping every 5 minutes until desired doneness is attained";
recipe.close();
// Now encrypt it with the MR methods
// First open the infile
ifstream infile("SecretSteakRecipe.txt", ios::binary); // always open your files in binary mode!
// Now the outfile, this can be any outstream, but we'll save to disk first
ofstream outfile("SecretSteakRecipe.crypt", ios::binary);
// encrypt it!
BasicCryptoPPWrap::MREncryptFileAES(infile, outfile, key2, err, errMsg);
if (err) {
cout << errMsg << "\n";
return -1;
}
infile.close();
outfile.close();
// delete the old text file
remove("SecretSteakRecipe.txt");
// Now we'll decrypt, but do it to memory so we leave no trace!
stringstream outBuffer;
ifstream incryptfile("SecretSteakRecipe.crypt", ios::binary);
// Decrypt it!
BasicCryptoPPWrap::MRDecryptFileAES(incryptfile, outBuffer, key2, err, errMsg);
if (err) {
cout << errMsg << "\n";
return -1;
}
// close and delete the encrypted file (you don't want me leaving files all over your hard drive do you?)
incryptfile.close();
remove("SecretSteakRecipe.crypt");
// Now look at our buffer
string aLine;
while (getline(outBuffer, aLine)) {
cout << aLine << "\n";
}
BasicCryptoPPWrap::cleanup();
// And that's about it!
return 0;
}