Advertisement
Guest User

Untitled

a guest
Jun 11th, 2013
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.19 KB | None | 0 0
  1.  
  2.  
  3. // g++ -g3 -ggdb -O0 -DDEBUG -I/usr/include/cryptopp Driver.cpp -o Driver.exe -lcryptopp -lpthread
  4. // g++ -g -O2 -DNDEBUG -I/usr/include/cryptopp Driver.cpp -o Driver.exe -lcryptopp -lpthread
  5. #include <cstdio>
  6. #include <iostream>
  7. #include <osrng.h>
  8. using CryptoPP::AutoSeededRandomPool;
  9.  
  10. #include <iostream>
  11. using std::cout;
  12. using std::cerr;
  13. using std::endl;
  14.  
  15. #include <string>
  16. using std::string;
  17.  
  18. #include <cstdlib>
  19. using std::exit;
  20.  
  21. #include <cryptlib.h>
  22. using CryptoPP::Exception;
  23.  
  24. #include <hex.h>
  25. using CryptoPP::HexEncoder;
  26. using CryptoPP::HexDecoder;
  27.  
  28. #include <filters.h>
  29. using CryptoPP::StringSink;
  30. using CryptoPP::StringSource;
  31. using CryptoPP::StreamTransformationFilter;
  32.  
  33. #include <des.h>
  34. using CryptoPP::DES_EDE2;
  35.  
  36. #include <modes.h>
  37. using CryptoPP::CBC_Mode;
  38.  
  39. #include <secblock.h>
  40. using CryptoPP::SecByteBlock;
  41. #include <iostream>
  42. #include <string>
  43. #include <modes.h>
  44. #include <aes.h>
  45. #include <filters.h>
  46. /*
  47. CryptoPP::SecByteBlock HexDecodeString(const char *hex)
  48. {
  49. CryptoPP::StringSource ss(hex, true, new CryptoPP::HexDecoder);
  50. CryptoPP::SecByteBlock result((size_t)ss.MaxRetrievable());
  51. ss.Get(result, result.size());
  52. return result;
  53. }*/
  54.  
  55.  
  56. int main(int argc, char* argv[]) {
  57.  
  58.  //HMODULE DLL = LoadLibrary(_T("cryptopp.dll"));
  59. //
  60. // Key and IV setup
  61. //AES encryption uses a secret key of a variable length (128-bit, 196-bit or 256-
  62. //bit). This key is secretly exchanged between two parties before communication
  63. //begins. DEFAULT_KEYLENGTH= 16 bytes
  64. std::string key = "0123456789abcdef";
  65. std::string iv = "aaaaaaaaaaaaaaaa";
  66.  string plain = "CBC Mode Test";
  67.  string cipher, encoded, recovered;
  68.  
  69.  
  70. std::string plaintext = "name macmilan age 24 ciy bonn country germany";
  71. std::string ciphertext;
  72. std::string decryptedtext;
  73.  
  74. std::cout << "Plain Text (" << plaintext.size() << " bytes)" << std::endl;
  75. std::cout << plaintext;
  76. std::cout << std::endl << std::endl;
  77.  
  78. CryptoPP::AES::Encryption aesEncryption((byte *)key.c_str(), CryptoPP::AES::DEFAULT_KEYLENGTH);
  79. CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, (byte *)iv.c_str() );
  80.  
  81. CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( ciphertext ) );
  82. stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plaintext.c_str() ), plaintext.length() + 1 );
  83. stfEncryptor.MessageEnd();
  84. cout << "cipher text plain: " << ciphertext << endl;
  85. std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;
  86. cout << endl;
  87. cout << endl;
  88. std::cout <<"cipher text In HEX FORM:: ";
  89.  for( int i = 0; i < ciphertext.size(); i++ ) {
  90.  
  91. std::cout << "0x" << std::hex << (0xFF & static_cast<byte>(ciphertext[i])) << " ";
  92. }
  93. cout << endl;
  94. cout << endl;
  95.  /*********************************\
  96. \*********************************/
  97.  
  98.  // Pretty print
  99. encoded.clear();
  100.  StringSource(ciphertext, true,
  101.  new HexEncoder(
  102.  new StringSink(encoded)
  103. ) // HexEncoder
  104. ); // StringSource
  105. cout << "cipher text In HEX FORM (Modified):: " << encoded << endl;
  106. cout << endl;
  107. cout << endl;
  108.  char *name2;
  109. name2 = (char*) malloc(encoded.length() + 1); // don't forget to free!!!!
  110.  //s2 = Database_row_count; // I forget if the string class can implicitly be converted to char*
  111.  //s2[0] = '1';
  112. strcpy(name2, encoded.c_str());
  113.  
  114.  const char* hex_str = name2;
  115.  
  116. std::string result_string ;
  117.  unsigned int ch ;
  118.  for( ; std::sscanf( hex_str, "%2x", &ch ) == 1 ; hex_str += 2 )
  119. result_string += ch ;
  120. cout << "HEX FORM to cipher text :: " ;
  121. std::cout << result_string << '\n' ;
  122. cout << endl;
  123. cout << endl;
  124.  /*********************************\
  125. \*********************************/
  126.  
  127.  
  128. CryptoPP::AES::Decryption aesDecryption((byte *)key.c_str(), CryptoPP::AES::DEFAULT_KEYLENGTH);
  129. CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, (byte *)iv.c_str() );
  130.  
  131. CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
  132. stfDecryptor.Put( reinterpret_cast<const unsigned char*>( result_string.c_str() ), result_string.size() );
  133. stfDecryptor.MessageEnd();
  134. std::cout << "Decrypted Text: " << std::endl;
  135. std::cout << decryptedtext;
  136. std::cout << std::endl << std::endl;
  137.  
  138.  system("pause");
  139.  
  140. return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement