71 lines
1.7 KiB
C++
71 lines
1.7 KiB
C++
#ifndef _DECRYPT
|
|
#define _DECRYPT
|
|
|
|
#include "aes.hpp"
|
|
|
|
class decrypt : public aes {
|
|
private:
|
|
EVP_CIPHER_CTX* aesDecryptCtx;
|
|
public:
|
|
decrypt(std::string filename);
|
|
decrypt(const aes& a);
|
|
void init_all();
|
|
int aesDecrypt(unsigned char *encMsg, size_t encMsgLen, unsigned char **decMsg);
|
|
~decrypt();
|
|
};
|
|
|
|
|
|
decrypt::decrypt(std::string filename) : aes(filename) {
|
|
aesDecryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
|
|
if(aesDecryptCtx == NULL) {
|
|
exit(FAILURE);
|
|
}
|
|
init_all();
|
|
}
|
|
|
|
decrypt::decrypt(const aes& _a) : aes(_a) {
|
|
aesDecryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
|
|
if(aesDecryptCtx == NULL) {
|
|
exit(FAILURE);
|
|
}
|
|
*aesDecryptCtx = (*(static_cast<decrypt>(_a)).aesDecryptCtx);
|
|
}
|
|
|
|
void decrypt::init_all() {
|
|
aes::init_all();
|
|
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;
|
|
|
|
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);
|
|
free(aesDecryptCtx);
|
|
}
|
|
|
|
#endif |