#include <cryptopp/osrng.h> #include <cryptopp/cryptlib.h> #include <cryptopp/aes.h> #include <cryptopp/filters.h> #include <cryptopp/files.h> #include <cryptopp/modes.h> #include <cryptopp/hex.h> using namespace CryptoPP;
#ifdef _DEBUG #pragma comment(lib,"cryptopp/vs2015/debug/cryptlib.lib") #else #pragma comment(lib,"cryptopp/vs2015/release/cryptlib.lib") #endif
unsigned char key[AES::DEFAULT_KEYLENGTH]; unsigned char iv[AES::BLOCKSIZE];
memset(key, 0x00, sizeof(key)); memset(iv, 0x00, sizeof(iv));
AutoSeededRandomPool rnd; rnd.GenerateBlock(key, AES::DEFAULT_KEYLENGTH);
rnd.GenerateBlock(iv, AES::BLOCKSIZE);
string strKey, strIv; vector<char> vBuf; byte2StrA(key, AES::DEFAULT_KEYLENGTH, vBuf); strKey = &vBuf[0];
byte2StrA(iv, AES::BLOCKSIZE, vBuf); strIv = &vBuf[0];
CTR_Mode<AES>::Encryption enc; enc.SetKeyWithIV(key, sizeof(key), iv); string encText; StreamTransformationFilter encFilter(enc, new StringSink(encText));
string strPlain("Attack at dawn!");
encFilter.Put((const unsigned char*)strPlain.c_str(), strPlain.size()); encFilter.MessageEnd();
CTR_Mode<AES>::Decryption dec; dec.SetKeyWithIV(key, sizeof(key), iv);
string decText; StreamTransformationFilter decFilter(dec, new StringSink(decText)); decFilter.Put((const unsigned char*)encText.c_str(), encText.size()); encFilter.MessageEnd();
|