Advertisement
Kyojin96

CryptoPP

Jan 31st, 2017
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include <stdlib.h>
  2.  
  3. #include "osrng.h"
  4. using CryptoPP::AutoSeededRandomPool;
  5.  
  6. #include <iostream>
  7. using std::cout;
  8. using std::cerr;
  9. using std::endl;
  10.  
  11. #include <string>
  12. using std::string;
  13.  
  14. #include <cstdlib>
  15. using std::exit;
  16.  
  17. #include "cryptlib.h"
  18. using CryptoPP::Exception;
  19.  
  20. #include "hex.h"
  21. using CryptoPP::HexEncoder;
  22. using CryptoPP::HexDecoder;
  23.  
  24. #include "filters.h"
  25. using CryptoPP::StringSink;
  26. using CryptoPP::StringSource;
  27. using CryptoPP::StreamTransformationFilter;
  28.  
  29. #include "aes.h"
  30. using CryptoPP::AES;
  31.  
  32. #include "modes.h"
  33. using CryptoPP::ECB_Mode;
  34.  
  35. int main()
  36. {
  37.     AutoSeededRandomPool prng;
  38.  
  39.     byte key[16];
  40.     prng.GenerateBlock(key, sizeof(key));
  41.  
  42.     string plain = "mypassword";
  43.     string cipher, encoded, recovered;
  44.  
  45.     /*********************************\
  46.     \*********************************/
  47.  
  48.     encoded.clear();
  49.     StringSource(key, sizeof(key), true,
  50.         new HexEncoder(
  51.             new StringSink(encoded)
  52.         )
  53.     );
  54.     cout << "key: " << encoded << endl;
  55.  
  56.     /*********************************\
  57.     \*********************************/
  58.  
  59.     try
  60.     {
  61.         cout << "plain text: " << plain << endl;
  62.  
  63.         ECB_Mode< AES >::Encryption e;
  64.         e.SetKey(key, sizeof(key));
  65.  
  66.         StringSource(plain, true,
  67.             new StreamTransformationFilter(e,
  68.                 new StringSink(cipher)
  69.             )      
  70.         );
  71.     }
  72.     catch (const CryptoPP::Exception& e)
  73.     {
  74.         cerr << e.what() << endl;
  75.         system("pause");
  76.         exit(1);
  77.     }
  78.  
  79.     /*********************************\
  80.     \*********************************/
  81.  
  82.     // Pretty print
  83.     encoded.clear();
  84.     StringSource(cipher, true,
  85.         new HexEncoder(
  86.             new StringSink(encoded)
  87.         )
  88.     );
  89.     cout << "cipher text: " << encoded << endl;
  90.  
  91.     /*********************************\
  92.     \*********************************/
  93.  
  94.     try
  95.     {
  96.         ECB_Mode< AES >::Decryption d;
  97.         d.SetKey(key, sizeof(key));
  98.  
  99.         StringSource s(cipher, true,
  100.             new StreamTransformationFilter(d,
  101.                 new StringSink(recovered)
  102.             )
  103.         );
  104.  
  105.         cout << "recovered text: " << recovered << endl;
  106.     }
  107.     catch (const CryptoPP::Exception& e)
  108.     {
  109.         cerr << e.what() << endl;
  110.         system("pause");
  111.         exit(1);
  112.     }
  113.    
  114.     system("pause");
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement