Share Pastebin
Guest
Public paste!

swarmapps

By: a guest | Nov 23rd, 2009 | Syntax: C++ | Size: 4.12 KB | Hits: 764 | Expires: Never
Copy text to clipboard
  1. // BasicCryptoPPWrap example code
  2. // By: Michael R. Rich, 2009
  3. // swarmapps.wordpress.com
  4. // This example code shows the use of all the BasicCryptoPPWrap functions
  5. // This code is released to the public domain
  6.  
  7. #include <iostream>
  8. #include <fstream>
  9. #include "BasicCryptoPPWrap.h"
  10. #include <string>
  11. #include <sstream>
  12. #include <stdio.h>
  13.  
  14. using namespace std;
  15.  
  16. int main (int argc, char * const argv[]) {
  17.         bool err;
  18.         string errMsg;
  19.        
  20.         // Encrypt and decrypt a string with a random key and initialization vector (iv)
  21.         string key1 = BasicCryptoPPWrap::BitGen(128);   // We'll make a random 128 bit key
  22.         string initVec1 = BasicCryptoPPWrap::ByteGen(16);       // AES uses 16 byte blocks, so the initVector must be 16 bytes
  23.        
  24.         // Here's what they look like
  25.         cout << "key1: " << BasicCryptoPPWrap::HexEncode(key1) << "\n";
  26.         cout << "initVec1: " << BasicCryptoPPWrap::HexEncode(initVec1) << "\n";
  27.        
  28.         string plainText = "I don't want anyone to see this!!";
  29.        
  30.         // encrypt it with the generic AES encryptor
  31.         string cipherText = BasicCryptoPPWrap::EncryptStringAES(plainText, key1, initVec1,  err, errMsg);
  32.         if (err) {
  33.                 cout << errMsg << "\n";
  34.                 return -1;
  35.         }
  36.        
  37.         // Here's what it looks like
  38.         cout << "Encrypted message: " << BasicCryptoPPWrap::HexEncode(cipherText) << "\n";
  39.        
  40.         // Now let's recover it!
  41.         string recoveredText = BasicCryptoPPWrap::DecryptStringAES(cipherText, key1, initVec1,  err, errMsg);
  42.         if (err) {
  43.                 cout << errMsg << "\n";
  44.                 return -1;
  45.         }
  46.         cout << "Plaintext Message 1: " << plainText << "\n";
  47.         cout << "Recovered Message 1: " << recoveredText << "\n";
  48.        
  49.         // 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
  50.         // 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
  51.         // First lets generate a key from a plain text password
  52.         string key2 = BasicCryptoPPWrap::hashSHA256("This is my password");
  53.         plainText = "Four eyes only";
  54.         cipherText = BasicCryptoPPWrap::MREncryptStringAES(plainText, key2,  err, errMsg);
  55.         if (err) {
  56.                 cout << errMsg << "\n";
  57.                 return -1;
  58.         }
  59.        
  60.         // Now recover the text with the matching decryption method
  61.         recoveredText = BasicCryptoPPWrap::MRDecryptStringAES(cipherText, key2,  err, errMsg);
  62.         if (err) {
  63.                 cout << errMsg << "\n";
  64.                 return -1;
  65.         }
  66.        
  67.         cout << "Plaintext Message 2: " << plainText << "\n";
  68.         cout << "Recovered Message 2: " << recoveredText << "\n";
  69.        
  70.         // Now let's try files!
  71.         // We'll skip the generic encryption method and just use MR.  MR stores the iv as the first 16 bytes of the file.
  72.        
  73.         // Generate a file
  74.         ofstream recipe("SecretSteakRecipe.txt");
  75.         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";
  76.         recipe << "Step 2: Grill steak, flipping every 5 minutes until desired doneness is attained";
  77.         recipe.close();
  78.        
  79.         // Now encrypt it with the MR methods
  80.         // First open the infile
  81.         ifstream infile("SecretSteakRecipe.txt", ios::binary); // always open your files in binary mode!
  82.        
  83.         // Now the outfile, this can be any outstream, but we'll save to disk first
  84.         ofstream outfile("SuperSecretSteakRecipe.crypt", ios::binary);
  85.        
  86.         // encrypt it!
  87.         BasicCryptoPPWrap::MREncryptFileAES(infile, outfile, key2,  err, errMsg);
  88.         if (err) {
  89.                 cout << errMsg << "\n";
  90.                 return -1;
  91.         }
  92.        
  93.         infile.close();
  94.         outfile.close();
  95.        
  96.         // delete the old text file
  97.         remove("SecretSteakRecipe.txt");
  98.                
  99.         // Now we'll decrypt, but do it to memory so we leave no trace!
  100.         stringstream outBuffer;
  101.         infile.open("SuperSecretSteakRecipe.crypt", ios::binary);
  102.        
  103.         // Decrypt it!
  104.         BasicCryptoPPWrap::MRDecryptFileAES(infile, outBuffer, key2,  err, errMsg);
  105.         if (err) {
  106.                 cout << errMsg << "\n";
  107.                 return -1;
  108.         }
  109.        
  110.         // close and delete the encrypted file (you don't want me leaving files all over your hard drive do you?)
  111.         infile.close();
  112.         remove("SuperSecretSteakRecipe.crypt");
  113.        
  114.         // Now look at our buffer
  115.         string aLine;
  116.         while (getline(outBuffer, aLine)) {
  117.                 cout << aLine << "\n";
  118.         }
  119.        
  120.         // And that's about it!
  121.         return 0;
  122. }