Recent Posts
None | 17 sec ago
None | 28 sec ago
None | 41 sec ago
None | 43 sec ago
None | 56 sec ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By Aaron Polley on the 3rd of Feb 2010 11:14:55 PM Download | Raw | Embed | Report
  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.         BasicCryptoPPWrap::cleanup();
  71.  
  72.         // Now let's try files!
  73.         // We'll skip the generic encryption method and just use MR.  MR stores the iv as the first 16 bytes of the file.
  74.        
  75.         // Generate a file
  76.         ofstream recipe("SecretSteakRecipe.txt");
  77.         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";
  78.         recipe << "Step 2: Grill steak, flipping every 5 minutes until desired doneness is attained";
  79.         recipe.close();
  80.        
  81.         // Now encrypt it with the MR methods
  82.         // First open the infile
  83.         ifstream infile("SecretSteakRecipe.txt", ios::binary); // always open your files in binary mode!
  84.        
  85.         // Now the outfile, this can be any outstream, but we'll save to disk first
  86.         ofstream outfile("SecretSteakRecipe.crypt", ios::binary);
  87.        
  88.         // encrypt it!
  89.         BasicCryptoPPWrap::MREncryptFileAES(infile, outfile, key2,  err, errMsg);
  90.         if (err) {
  91.                 cout << errMsg << "\n";
  92.                 return -1;
  93.         }
  94.        
  95.         infile.close();
  96.         outfile.close();
  97.        
  98.         // delete the old text file
  99.         remove("SecretSteakRecipe.txt");
  100.                
  101.         // Now we'll decrypt, but do it to memory so we leave no trace!
  102.         stringstream outBuffer;
  103.         ifstream incryptfile("SecretSteakRecipe.crypt", ios::binary);
  104.        
  105.         // Decrypt it!
  106.         BasicCryptoPPWrap::MRDecryptFileAES(incryptfile, outBuffer, key2,  err, errMsg);
  107.         if (err) {
  108.                 cout << errMsg << "\n";
  109.                 return -1;
  110.         }
  111.        
  112.         // close and delete the encrypted file (you don't want me leaving files all over your hard drive do you?)
  113.         incryptfile.close();
  114.         remove("SecretSteakRecipe.crypt");
  115.        
  116.         // Now look at our buffer
  117.         string aLine;
  118.         while (getline(outBuffer, aLine)) {
  119.                 cout << aLine << "\n";
  120.         }
  121.        
  122.         BasicCryptoPPWrap::cleanup();
  123.  
  124.        
  125.         // And that's about it!
  126.         return 0;
  127. }
Submit a correction or amendment below. [ previous version ] | [ difference ] | Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: