Advertisement
Guest User

Brendan Long

a guest
Mar 29th, 2010
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. /*!
  2.  * Simple AES
  3.  * Brendan Long
  4.  * March 29, 2010
  5.  *
  6.  * Simplified encryption and decryption using OpenSSL's AES library.
  7.  * Remember to compile with -lcrypto and link against the library
  8.  * g++ (your stuff) -lcrypto simpleAes.cpp (or simpleAes.o)
  9.  *
  10.  * Implementation note: Using the default ivec (0) is not secure. For
  11.  *                      the full security that AES offers, use a different
  12.  *                      ivec each time (it does not need to be secret,
  13.  *                      just different.
  14.  *
  15.  * This code is released into the public domain. Yada yada..
  16.  * Read this for details: http://creativecommons.org/licenses/publicdomain/
  17.  *
  18.  * If for some reason public domain isn't good enough, you may use, alter,
  19.  * distribute or do anything else you want with this code with no restrictions.
  20.  */
  21.  
  22. #include <openssl/aes.h>
  23. #include <iostream>
  24. #include <stdlib.h>
  25. #include <time.h>
  26.  
  27. bool seed = true;
  28.  
  29. /*!
  30.  * Encrypts a string using AES
  31.  * Note: If the key is less than 32 bytes, it will be null padded.
  32.  *       If the key is greater than 32 bytes, it will be truncated
  33.  * \param in The string to encrypt
  34.  * \param key The key to encrypt with
  35.  * \return The encrypted data
  36.  */
  37. std::string aes_encrypt(std::string in, std::string key){
  38.  
  39.     // Seed the random number generator once
  40.     if(seed){
  41.         srand( (unsigned int) time(NULL));
  42.         seed = false;
  43.     }
  44.  
  45.     // Generate a random ivec
  46.     unsigned char ivec[16];
  47.     for(int i=0; i<16; i++){
  48.         ivec[i] = (unsigned char) rand();
  49.     }
  50.  
  51.      // Round up to AES_BLOCK_SIZE
  52.     size_t textLength = ((in.length() / AES_BLOCK_SIZE) + 1) * AES_BLOCK_SIZE;
  53.    
  54.     // Always pad the key to 32 bits.. because we can
  55.     if(key.length() < 32){
  56.         key.append(32 - key.length(), '\0');
  57.     }
  58.    
  59.     // Get some space ready for the output
  60.     unsigned char *output = new unsigned char[textLength + 1];
  61.    
  62.     // Generate a key
  63.     AES_KEY *aesKey = new AES_KEY;
  64.     AES_set_encrypt_key((unsigned char*)key.c_str(), 256, aesKey);
  65.    
  66.     // Encrypt the data
  67.     AES_cbc_encrypt((unsigned char*)in.c_str(), output, in.length(), aesKey, ivec, AES_ENCRYPT);
  68.    
  69.     // Make the data into a string
  70.     output[textLength] = '\0';
  71.     std::string ret((char*) output, textLength);
  72.    
  73.     // Add the ivec to the front
  74.     ret = std::string((char*)ivec, 16) + ret;
  75.    
  76.     // Clean up
  77.     delete output;
  78.     delete aesKey;
  79.    
  80.     return ret;
  81. }
  82.  
  83. /*!
  84.  * Decrypts a string using AES
  85.  * Note: If the key is less than 32 bytes, it will be null padded.
  86.  *       If the key is greater than 32 bytes, it will be truncated
  87.  * \param in The string to decrypt
  88.  * \param key The key to decrypt with
  89.  * \return The decrypted data
  90.  */
  91. std::string aes_decrypt(std::string in, std::string key){
  92.  
  93.     // Get the ivec from the front
  94.     unsigned char ivec[16];
  95.     for(int i=0;i<16; i++){
  96.         ivec[i] = in[i];
  97.     }
  98.    
  99.     in = in.substr(16);
  100.  
  101.     // Always pad the key to 32 bits.. because we can
  102.     if(key.length() < 32){
  103.         key.append(32 - key.length(), '\0');
  104.     }
  105.  
  106.     // Create some space for output
  107.     unsigned char *output = new unsigned char[in.length()];
  108.    
  109.     // Generate a key
  110.     AES_KEY *aesKey = new AES_KEY;
  111.     AES_set_decrypt_key((unsigned char*)key.c_str(), 256, aesKey);
  112.    
  113.     // Decrypt the data
  114.     AES_cbc_encrypt((unsigned char*)in.c_str(), output, in.length(), aesKey, ivec, AES_DECRYPT);
  115.    
  116.     // Make the output into a string
  117.     std::string ret((char*) output);
  118.    
  119.     // Clean up
  120.     delete output;
  121.     delete aesKey;
  122.    
  123.     return ret;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement