aes/decrypt.hpp

85 lines
2.0 KiB
C++
Raw Normal View History

2016-01-20 19:25:02 +00:00
#ifndef _DECRYPT
#define _DECRYPT
#include "aes.hpp"
class decrypt : public aes {
private:
EVP_CIPHER_CTX* aesDecryptCtx;
2016-01-20 19:25:02 +00:00
public:
2016-01-23 21:57:15 +00:00
decrypt(std::string filename, unsigned char* aesKey=0, unsigned char* aesIV=0);
decrypt(const decrypt& a);
2016-01-23 21:57:15 +00:00
void init_all(bool initSuper=true);
int aesDecrypt(unsigned char *encMsg, size_t encMsgLen, unsigned char** decMsg);
~decrypt();
2016-01-20 19:25:02 +00:00
};
2016-01-23 21:57:15 +00:00
decrypt::decrypt(std::string filename, unsigned char* aesKey, unsigned char* aesIV) : aes(filename) {
aesDecryptCtx = EVP_CIPHER_CTX_new();
//aesDecryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
2016-01-20 19:25:02 +00:00
if(aesDecryptCtx == NULL) {
exit(FAILURE);
}
2016-01-23 21:57:15 +00:00
if(aesKey && aesIV) {
setAesKey(aesKey);
setAesIV(aesIV);
init_all(false);
}
else
init_all();
2016-01-20 19:25:02 +00:00
}
2016-01-23 21:57:15 +00:00
decrypt::decrypt(const decrypt& _a) : aes::aes() {
this->clone(_a);
aesDecryptCtx = EVP_CIPHER_CTX_new();
2016-01-20 19:25:02 +00:00
if(aesDecryptCtx == NULL) {
exit(FAILURE);
}
2016-01-23 21:57:15 +00:00
int res = EVP_CIPHER_CTX_copy(aesDecryptCtx, _a.aesDecryptCtx);
if(!res)
exit(FAILURE);
2016-01-20 19:25:02 +00:00
}
2016-01-23 21:57:15 +00:00
void decrypt::init_all(bool initSuper) {
setExtension(".dec");
if(initSuper)
aes::init_all();
2016-01-20 19:25:02 +00:00
EVP_CIPHER_CTX_init(aesDecryptCtx);
}
int decrypt::aesDecrypt(unsigned char *encMsg, size_t encMsgLen, unsigned char **decMsg) {
size_t decLen = 0;
size_t blockLen = 0;
*decMsg = (unsigned char*)malloc(encMsgLen);
if(*decMsg == NULL) return FAILURE;
2016-01-20 19:25:02 +00:00
if(!EVP_DecryptInit_ex(aesDecryptCtx, EVP_aes_256_cbc(), NULL, aes::aesKey, aes::aesIV)) {
return FAILURE;
}
if(!EVP_DecryptUpdate(aesDecryptCtx, (unsigned char*)*decMsg, (int*)&blockLen, encMsg, (int)encMsgLen)) {
return FAILURE;
}
decLen += blockLen;
if(!EVP_DecryptFinal_ex(aesDecryptCtx, (unsigned char*)*decMsg + decLen, (int*)&blockLen)) {
return FAILURE;
}
decLen += blockLen;
EVP_CIPHER_CTX_cleanup(aesDecryptCtx);
return (int)decLen;
}
decrypt::~decrypt() {
aes::clear_all();
EVP_CIPHER_CTX_cleanup(aesDecryptCtx);
EVP_CIPHER_CTX_free(aesDecryptCtx);
2016-01-20 19:25:02 +00:00
}
#endif