27 #ifndef DOZERG_ENCRYPTOR_AES_H_20080306 28 #define DOZERG_ENCRYPTOR_AES_H_20080306 33 #include <openssl/aes.h> 34 #include "tools/ssl.hh" 45 typedef unsigned char __Char;
46 static const size_t kKeyLen = 16;
88 void setKey(
const std::string & key){
118 int encrypt(
const std::vector<char> & input, std::vector<char> & output,
size_t from = 0)
const{
119 return encryptTemplate(input, output, from);
121 int encrypt(
const std::vector<signed char> & input, std::vector<signed char> & output,
size_t from = 0)
const{
122 return encryptTemplate(input, output, from);
124 int encrypt(
const std::vector<unsigned char> & input, std::vector<unsigned char> & output,
size_t from = 0)
const{
125 return encryptTemplate(input, output, from);
127 int encrypt(
const std::string & input, std::string & output,
size_t from = 0)
const{
128 return encryptTemplate(input, output, from);
147 int decrypt(
const std::vector<char> & input, std::vector<char> & output,
size_t from = 0)
const{
148 return decryptTemplate(input, output, from);
150 int decrypt(
const std::vector<signed char> & input, std::vector<signed char> & output,
size_t from = 0)
const{
151 return decryptTemplate(input, output, from);
153 int decrypt(
const std::vector<unsigned char> & input, std::vector<unsigned char> & output,
size_t from = 0)
const{
154 return decryptTemplate(input, output, from);
156 int decrypt(
const std::string & input, std::string & output,
size_t from = 0)
const{
157 return decryptTemplate(input, output, from);
162 std::string md5 = tools::Md5(key_);
163 AES_set_encrypt_key(reinterpret_cast<const __Char *>(&md5[0]), intens_, &enKey_);
164 AES_set_decrypt_key(reinterpret_cast<const __Char *>(&md5[0]), intens_, &deKey_);
166 template<
class Buffer>
167 int encryptTemplate(
const Buffer & input, Buffer & output,
size_t from)
const{
168 size_t inlen = input.size();
172 output.resize(from + (inlen / AES_BLOCK_SIZE + 1) * AES_BLOCK_SIZE);
173 std::copy(input.begin(), input.begin() + from, output.begin());
174 for(;inlen >= AES_BLOCK_SIZE;inlen -= AES_BLOCK_SIZE, from += AES_BLOCK_SIZE)
176 reinterpret_cast<const __Char *>(&input[from]),
177 reinterpret_cast<__Char *
>(&output[from]),
179 unsigned char tmp[AES_BLOCK_SIZE];
180 memset(tmp, AES_BLOCK_SIZE - inlen, AES_BLOCK_SIZE);
181 memcpy(tmp, &input[from], inlen);
182 AES_encrypt(tmp, reinterpret_cast<__Char *>(&output[from]), &enKey_);
185 template<
class Buffer>
186 int decryptTemplate(
const Buffer & input, Buffer & output,
size_t from)
const{
187 size_t inlen = input.size();
188 if(inlen < from || (inlen - from) % AES_BLOCK_SIZE != 0)
190 output.resize(inlen);
191 std::copy(input.begin(), input.begin() + from, output.begin());
192 for(inlen -= from;inlen;inlen -= AES_BLOCK_SIZE, from += AES_BLOCK_SIZE)
194 reinterpret_cast<const __Char *>(&input[from]),
195 reinterpret_cast<__Char *
>(&output[from]),
197 inlen = *output.rbegin();
198 if(inlen == 0 || inlen > AES_BLOCK_SIZE)
200 output.resize(output.size() - inlen);
void setKey(const std::string &key)
Set encryption key (password).
Definition: encrypt_aes.hh:88
void setKeyAndIntensity(const std::string &key, EKeyIntensity intensity)
Set encryption key (password) and key size.
Definition: encrypt_aes.hh:98
void setIntensity(EKeyIntensity intensity)
Set key size.
Definition: encrypt_aes.hh:79
CEncryptorAes()
Construct a default object.
Definition: encrypt_aes.hh:61
EKeyIntensity
Key sizes.
Definition: encrypt_aes.hh:51
CEncryptorAes(const std::string &key, EKeyIntensity intensity)
Construct an object.
Definition: encrypt_aes.hh:69
A convenient interface for Advanced Encryption Standard (AES) algorithm.
Definition: encrypt_aes.hh:43