163 lines
4.1 KiB
C++
163 lines
4.1 KiB
C++
#include "aesclass.h"
|
|
|
|
AESClass::AESClass()
|
|
{
|
|
memset(key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH);
|
|
memset(iv, 0x00, CryptoPP::AES::BLOCKSIZE);
|
|
}
|
|
|
|
AESClass::~AESClass()
|
|
{
|
|
memset(key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH);
|
|
memset(iv, 0x00, CryptoPP::AES::BLOCKSIZE);
|
|
}
|
|
|
|
void AESClass::setKey(byte* _key)
|
|
{
|
|
memcpy(key, _key, CryptoPP::AES::DEFAULT_KEYLENGTH);
|
|
}
|
|
|
|
void AESClass::setKey(const char *_key)
|
|
{
|
|
memcpy(key, _key, CryptoPP::AES::DEFAULT_KEYLENGTH);
|
|
}
|
|
|
|
void AESClass::setKey(std::string _key)
|
|
{
|
|
memcpy(key, _key.c_str() , CryptoPP::AES::DEFAULT_KEYLENGTH);
|
|
}
|
|
|
|
void AESClass::setIv(byte *_iv)
|
|
{
|
|
memcpy(iv, _iv, CryptoPP::AES::BLOCKSIZE);
|
|
}
|
|
|
|
void AESClass::setIv(const char *_iv)
|
|
{
|
|
memcpy(iv, _iv, CryptoPP::AES::BLOCKSIZE);
|
|
}
|
|
|
|
void AESClass::setIv(std::string _iv)
|
|
{
|
|
memcpy(iv, _iv.c_str() , CryptoPP::AES::BLOCKSIZE);
|
|
}
|
|
|
|
const int AESClass::getKeyLength() const
|
|
{
|
|
return CryptoPP::AES::DEFAULT_KEYLENGTH;
|
|
}
|
|
|
|
const int AESClass::getBlockSize() const
|
|
{
|
|
return CryptoPP::AES::BLOCKSIZE;
|
|
}
|
|
|
|
const byte* AESClass::getKey() const
|
|
{
|
|
return key;
|
|
}
|
|
|
|
const byte* AESClass::getIv() const
|
|
{
|
|
return iv;
|
|
}
|
|
|
|
|
|
std::string AESClass::AESEncrypt(std::string plaintext)
|
|
{
|
|
std::string ciphertext;
|
|
CryptoPP::AES::Encryption
|
|
aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
|
|
CryptoPP::CBC_Mode_ExternalCipher::Encryption
|
|
cbcEncryption(aesEncryption, iv);
|
|
|
|
CryptoPP::StreamTransformationFilter
|
|
stfEncryptor(cbcEncryption, new CryptoPP::StringSink( ciphertext));
|
|
stfEncryptor.Put(reinterpret_cast<const unsigned char*>
|
|
(plaintext.c_str()), plaintext.size() + 1);
|
|
stfEncryptor.MessageEnd();
|
|
|
|
return ciphertext;
|
|
}
|
|
|
|
std::string AESClass::AESEncrypt(char *_plaintext)
|
|
{
|
|
std::string plaintext(_plaintext);
|
|
return AESEncrypt(plaintext);
|
|
}
|
|
|
|
std::string AESClass::AESDecrypt(byte* _ciphertext)
|
|
{
|
|
std::string ciphertext((char *)_ciphertext);
|
|
return AESDecrypt(ciphertext);
|
|
}
|
|
|
|
std::string AESClass::AESDecrypt(std::string _ciphertext)
|
|
{
|
|
std::string decryptedtext;
|
|
CryptoPP::AES::Decryption aesDecryption(key,
|
|
CryptoPP::AES::DEFAULT_KEYLENGTH);
|
|
CryptoPP::CBC_Mode_ExternalCipher::Decryption
|
|
cbcDecryption(aesDecryption, iv );
|
|
|
|
CryptoPP::StreamTransformationFilter
|
|
stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decryptedtext));
|
|
stfDecryptor.Put( reinterpret_cast<const unsigned char*>
|
|
(_ciphertext.c_str()), _ciphertext.size());
|
|
stfDecryptor.MessageEnd();
|
|
|
|
return decryptedtext;
|
|
}
|
|
|
|
std::string AESClass::toBase64(byte* _ciphertext)
|
|
{
|
|
std::string ciphertext((char *)_ciphertext);
|
|
return toBase64(ciphertext);
|
|
}
|
|
|
|
std::string AESClass::toBase64(std::string _ciphertext)
|
|
{
|
|
std::string base64encodedciphertext;
|
|
CryptoPP::StringSource(_ciphertext, true,
|
|
new CryptoPP::Base64Encoder(
|
|
new CryptoPP::StringSink(base64encodedciphertext)
|
|
) // Base64Encoder
|
|
); // StringSource
|
|
return base64encodedciphertext;
|
|
}
|
|
|
|
void AESClass::base64toByte(std::string text, byte* out)
|
|
{
|
|
std::string base64decryptedciphertext;
|
|
CryptoPP::StringSource(text, true,
|
|
new CryptoPP::Base64Decoder(
|
|
new CryptoPP::StringSink( base64decryptedciphertext)
|
|
) // Base64Encoder
|
|
);
|
|
|
|
memcpy(out, base64decryptedciphertext.c_str(), base64decryptedciphertext.size());
|
|
}
|
|
std::string AESClass::base64toString(std::string text)
|
|
{
|
|
std::string out;
|
|
CryptoPP::StringSource(text, true,
|
|
new CryptoPP::Base64Decoder(
|
|
new CryptoPP::StringSink(out)
|
|
) // Base64Encoder
|
|
);
|
|
return out;
|
|
}
|
|
|
|
void AESClass::hex2byte(const char *in, unsigned int len, byte *out)
|
|
{
|
|
for (unsigned int i = 0; i < len; i+=2) {
|
|
char c0 = in[i+0];
|
|
char c1 = in[i+1];
|
|
byte c = (
|
|
((c0 & 0x40 ? (c0 & 0x20 ? c0-0x57 : c0-0x37) : c0-0x30)<<4) |
|
|
((c1 & 0x40 ? (c1 & 0x20 ? c1-0x57 : c1-0x37) : c1-0x30))
|
|
);
|
|
out[i/2] = c;
|
|
}
|
|
}
|