Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. #define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1 // 컴파일러에서 정의하라고 나온다.
  2.  
  3. // Crypto++ Includes
  4. #include "cryptlib.h"
  5. #include "osrng.h"
  6. #include "hex.h"
  7. #include "Base64.h"
  8. #include "aes.h"
  9. #include "seed.h"
  10. #include "des.h"
  11. #include "modes.h"
  12. #include "filters.h"
  13.  
  14. #pragma comment(lib, "cryptlib") // 라이브러리 참조, 프로젝트 링크 세팅에서 해도 됨
  15.  
  16. template <class TyMode>
  17. std::string Encrypt(TyMode &Encryptor, const std::string &PlainText)
  18. {
  19. std::string EncodedText;
  20.  
  21. try {
  22. CryptoPP::StringSource( PlainText, true,
  23. new CryptoPP::StreamTransformationFilter(Encryptor,
  24. new CryptoPP::Base64Encoder(
  25. new CryptoPP::StringSink( EncodedText ), false
  26. ), CryptoPP::BlockPaddingSchemeDef::ZEROS_PADDING
  27. )
  28. );
  29. }
  30. catch (...) {}
  31.  
  32. return EncodedText;
  33. }
  34.  
  35. template <class TyMode>
  36. std::string Decrypt(TyMode &Decryptor, const std::string &EncodedText)
  37. {
  38. std::string RecoveredText;
  39.  
  40. try {
  41. CryptoPP::StringSource( EncodedText, true,
  42. new CryptoPP::Base64Decoder(
  43. new CryptoPP::StreamTransformationFilter( Decryptor,
  44. new CryptoPP::StringSink( RecoveredText ),
  45. CryptoPP::BlockPaddingSchemeDef::ZEROS_PADDING
  46. )
  47. )
  48. );
  49. }
  50. catch (...) {}
  51.  
  52. return RecoveredText;
  53. }
  54.  
  55. template <class Ty>
  56. std::string CBC_Encrypt(byte *KEY, byte *IV, const std::string &PlainText)
  57. {
  58. CryptoPP::CBC_Mode<typename Ty>::Encryption Encryptor(KEY, typename Ty::DEFAULT_KEYLENGTH, IV);
  59. return Encrypt(Encryptor, PlainText);
  60. }
  61.  
  62.  
  63. template <class Ty>
  64. std::string CBC_Decrypt(byte *KEY, byte *IV, const std::string &PlainText)
  65. {
  66. CryptoPP::CBC_Mode<typename Ty>::Decryption Decryptor(KEY, typename Ty::DEFAULT_KEYLENGTH, IV);
  67. return Decrypt(Decryptor, PlainText);
  68. }
  69.  
  70. template <class Ty>
  71. std::string ECB_Encrypt(byte *KEY, const std::string &PlainText)
  72. {
  73. CryptoPP::ECB_Mode<typename Ty>::Encryption Encryptor(KEY, typename Ty::DEFAULT_KEYLENGTH);
  74. return Encrypt(Encryptor, PlainText);
  75. }
  76.  
  77.  
  78. template <class Ty>
  79. std::string ECB_Decrypt(byte *KEY, const std::string &PlainText)
  80. {
  81. CryptoPP::ECB_Mode<typename Ty>::Decryption Decryptor(KEY, typename Ty::DEFAULT_KEYLENGTH);
  82. return Decrypt(Decryptor, PlainText);
  83. }
  84.  
  85.  
  86. template <class CryptoType>
  87. void Test()
  88. {
  89. using namespace std;
  90.  
  91. const std::string sText = "Plain Text";
  92. std::string sEnc, sDec;
  93.  
  94. byte KEY[ CryptoType::DEFAULT_KEYLENGTH ] = {0, };
  95. byte IV[ CryptoType::BLOCKSIZE ] = {0x01, };
  96.  
  97. // CBC 모드
  98. sEnc = CBC_Encrypt<CryptoType>(KEY, IV, sText);
  99. sDec = CBC_Decrypt<CryptoType>(KEY, IV, sEnc);
  100.  
  101. cout << CryptoType::StaticAlgorithmName() << " : " << "CBC_MODE" << endl;
  102. cout << sText << "\n -> " << sEnc << "\n -> " << sDec << endl;
  103.  
  104.  
  105. // ECB 모드
  106. sEnc = ECB_Encrypt<CryptoType>(KEY, sText);
  107. sDec = ECB_Decrypt<CryptoType>(KEY, sEnc);
  108.  
  109. cout << CryptoType::StaticAlgorithmName() << " : " << "ECB_MODE" << endl;
  110. cout << sText << "\n -> " << sEnc << "\n -> " << sDec << endl;
  111. cout << endl;
  112. }
  113.  
  114.  
  115. int _tmain(int argc, _TCHAR* argv[])
  116. {
  117. using namespace std;
  118.  
  119. // SEED 알고리즘
  120. Test<CryptoPP::SEED>();
  121.  
  122. // AES 알고리즘
  123. Test<CryptoPP::AES>();
  124.  
  125. // DES 알고리즘
  126. Test<CryptoPP::DES>();
  127.  
  128. return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement