forgetIt/Untracked/Doxygen/CryptClass/AESCrypt.cpp
2015-03-21 06:55:46 +01:00

110 lines
2.4 KiB
C++

/**
* @file AESCrypt.cpp
* @brief AESCrypt class definitions
* @author manzerbredes
* @date 8 Mars 2015
*
* Contain all definitions of AESCrypt class.
*
*/
#include "AESCrypt.hpp"
//Constructor
AESCrypt::AESCrypt(){
this->hash=HASHCrypt(); //Init hash attribute
}
//Destructor
AESCrypt::~AESCrypt(){
}
//Encrypt string
std::string AESCrypt::encrypt(std::string key, std::string data){
//Generate SHA-256
byte digest[32];
hash.getSHA_256(key, digest, (int)sizeof(digest));
//Add padding for AES
int pad=0;
int tmpL=data.length();
while(tmpL % 128 != 0){
tmpL++;
pad++;
}
std::cout <<pad;
while(data.length() % 128 != 0){
data+=pad;
}
//Contain data encrypted
std::string cipher;
//Use try, catch to be ensure no problems happening
try{
//Create encoder to encrypt data
CryptoPP::ECB_Mode<CryptoPP::AES>::Encryption encoder;
encoder.SetKey( digest, sizeof(digest) );
//Encrypt data with StreamTransformationFilter with NO PADDING
CryptoPP::StringSource ss1(data, true,
new CryptoPP::StreamTransformationFilter( encoder,
new CryptoPP::StringSink( cipher ),
CryptoPP::StreamTransformationFilter::PKCS_PADDING
)
);
}
catch( CryptoPP::Exception& e )
{
std::cerr << e.what() << std::endl;
exit(EXIT_FAILURE);
}
//return encrypted data
return cipher;
}
//Decrypt string
std::string AESCrypt::decrypt(std::string key, std::string data){
byte digest[32];
hash.getSHA_256(key, digest, (int)sizeof(digest));
//Contain data decrypted
std::string cipher;
//Use try, catch to be ensure no problems happening
try {
//Create decoder to encrypt data
CryptoPP::ECB_Mode< CryptoPP::AES >::Decryption decoder;
decoder.SetKey( digest, sizeof(digest) );
//Decrypt data with StreamTransformationFilter with NO PADDING
CryptoPP::StringSource ss3( data, true,
new CryptoPP::StreamTransformationFilter( decoder,
new CryptoPP::StringSink( cipher ),
CryptoPP::StreamTransformationFilter::PKCS_PADDING
)
);
}
catch( CryptoPP::Exception& e )
{
std::cerr << e.what() << std::endl;
exit(1);
}
//return decrypted data
return cipher;
}