Skip to content

Instantly share code, notes, and snippets.

@cdecl
Last active August 29, 2015 14:17
Show Gist options
  • Save cdecl/02adc3a2af96cd85311b to your computer and use it in GitHub Desktop.
Save cdecl/02adc3a2af96cd85311b to your computer and use it in GitHub Desktop.
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1 // 컴파일러에서 정의하라고 나온다.
// Crypto++ Includes
#include "cryptlib.h"
#include "osrng.h"
#include "hex.h"
#include "Base64.h"
#include "aes.h"
#include "seed.h"
#include "des.h"
#include "modes.h"
#include "filters.h"
#pragma comment(lib, "cryptlib") // 라이브러리 참조, 프로젝트 링크 세팅에서 해도 됨
template <class TyMode>
std::string Encrypt(TyMode &Encryptor, const std::string &PlainText)
{
std::string EncodedText;
try {
CryptoPP::StringSource( PlainText, true,
new CryptoPP::StreamTransformationFilter(Encryptor,
new CryptoPP::Base64Encoder(
new CryptoPP::StringSink( EncodedText ), false
), CryptoPP::BlockPaddingSchemeDef::ZEROS_PADDING
)
);
}
catch (...) {}
return EncodedText;
}
template <class TyMode>
std::string Decrypt(TyMode &Decryptor, const std::string &EncodedText)
{
std::string RecoveredText;
try {
CryptoPP::StringSource( EncodedText, true,
new CryptoPP::Base64Decoder(
new CryptoPP::StreamTransformationFilter( Decryptor,
new CryptoPP::StringSink( RecoveredText ),
CryptoPP::BlockPaddingSchemeDef::ZEROS_PADDING
)
)
);
}
catch (...) {}
return RecoveredText;
}
template <class Ty>
std::string CBC_Encrypt(byte *KEY, byte *IV, const std::string &PlainText)
{
CryptoPP::CBC_Mode<typename Ty>::Encryption Encryptor(KEY, typename Ty::DEFAULT_KEYLENGTH, IV);
return Encrypt(Encryptor, PlainText);
}
template <class Ty>
std::string CBC_Decrypt(byte *KEY, byte *IV, const std::string &PlainText)
{
CryptoPP::CBC_Mode<typename Ty>::Decryption Decryptor(KEY, typename Ty::DEFAULT_KEYLENGTH, IV);
return Decrypt(Decryptor, PlainText);
}
template <class Ty>
std::string ECB_Encrypt(byte *KEY, const std::string &PlainText)
{
CryptoPP::ECB_Mode<typename Ty>::Encryption Encryptor(KEY, typename Ty::DEFAULT_KEYLENGTH);
return Encrypt(Encryptor, PlainText);
}
template <class Ty>
std::string ECB_Decrypt(byte *KEY, const std::string &PlainText)
{
CryptoPP::ECB_Mode<typename Ty>::Decryption Decryptor(KEY, typename Ty::DEFAULT_KEYLENGTH);
return Decrypt(Decryptor, PlainText);
}
template <class CryptoType>
void Test()
{
using namespace std;
const std::string sText = "Plain Text";
std::string sEnc, sDec;
byte KEY[ CryptoType::DEFAULT_KEYLENGTH ] = {0, };
byte IV[ CryptoType::BLOCKSIZE ] = {0x01, };
// CBC 모드
sEnc = CBC_Encrypt<CryptoType>(KEY, IV, sText);
sDec = CBC_Decrypt<CryptoType>(KEY, IV, sEnc);
cout << CryptoType::StaticAlgorithmName() << " : " << "CBC_MODE" << endl;
cout << sText << "\n -> " << sEnc << "\n -> " << sDec << endl;
// ECB 모드
sEnc = ECB_Encrypt<CryptoType>(KEY, sText);
sDec = ECB_Decrypt<CryptoType>(KEY, sEnc);
cout << CryptoType::StaticAlgorithmName() << " : " << "ECB_MODE" << endl;
cout << sText << "\n -> " << sEnc << "\n -> " << sDec << endl;
cout << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
// SEED 알고리즘
Test<CryptoPP::SEED>();
// AES 알고리즘
Test<CryptoPP::AES>();
// DES 알고리즘
Test<CryptoPP::DES>();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment