Advertisement
Guest User

Untitled

a guest
Mar 7th, 2011
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.83 KB | None | 0 0
  1. #include <openssl/blowfish.h>
  2. #include <stdlib.h>
  3. #include <cstdio>
  4. #include <string.h>
  5. #include <iostream>
  6. #include <unistd.h>
  7. using namespace std;
  8.  
  9. const char *enckey = "0000000000000000";
  10. unsigned char *vin = (unsigned char *)"00000000";
  11.  
  12. //
  13. // encrypt a string
  14. // NOTE: you must free this string afterwards
  15. //
  16. unsigned char *encrypt(const unsigned char *inStr, int len)
  17. {
  18.   // blowfish key
  19.   BF_KEY bfKey;
  20.   BF_set_key(&bfKey, strlen(enckey), (const unsigned char*)enckey);
  21.  
  22.   // encrypt
  23.   unsigned char *outStr = (unsigned char *)malloc(sizeof(unsigned char) * len);
  24.   BF_cbc_encrypt((const unsigned char *)inStr, outStr, len, &bfKey, vin, BF_ENCRYPT);
  25.   return outStr;
  26. }
  27.  
  28. //
  29. // decrypt a string
  30. // NOTE: you must free this string afterwards
  31. //
  32. unsigned char *decrypt(const unsigned char *inStr, int len)
  33. {
  34.   // blowfish key
  35.   BF_KEY bfKey;
  36.   BF_set_key(&bfKey, strlen(enckey), (const unsigned char*)enckey);
  37.  
  38.   // decrypt
  39.   unsigned char *buf = (unsigned char *)malloc(sizeof(unsigned char) * len);
  40.   BF_cbc_encrypt((const unsigned char*)inStr, buf, len, &bfKey, vin, BF_DECRYPT);
  41.   return buf;
  42. }
  43.  
  44. //
  45. // MAIN
  46. //
  47. int main(int argc, char **argv)
  48. {
  49.   int c;
  50.   char *encrypt_str = NULL;
  51.  
  52.   while ((c = getopt(argc, argv, "e:")) != -1)
  53.   {
  54.     switch (c)
  55.     {
  56.       case 'e':
  57.         encrypt_str = optarg;
  58.         break;
  59.       default:
  60.         cerr << "unknown argument: -" << c << " supplied\n";
  61.         exit(1);
  62.     }
  63.   }
  64.  
  65.   if (encrypt_str != NULL) {
  66.     int len = strlen(encrypt_str);
  67.     unsigned char *encstr = encrypt((const unsigned char *)encrypt_str, len);
  68.     unsigned char *decstr = decrypt((const unsigned char *)encstr, len);
  69.     std::cout << "decrypted: " << decstr << "\n";
  70.     free(encstr);
  71.     free(decstr);
  72.   } else {
  73.     std::cout << "nothing to do\n";
  74.   }
  75.  
  76.   return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement