-
Notifications
You must be signed in to change notification settings - Fork 1
/
aes.cpp
53 lines (36 loc) · 1.67 KB
/
aes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "aes.h"
bool AES::aes_init(const char * key, const char * salt, int nrounds){
return this->aes_init((unsigned char*)key,(unsigned char*)salt,nrounds);
}
bool AES::aes_init(unsigned char *key, unsigned char *salt, int nrounds){
unsigned char keytmp[32], iv[32]; // 32*8 = 256 bits
if( EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), salt, key, strlen((const char*)key), nrounds, keytmp, iv) != 32 )
return false;
EVP_CIPHER_CTX_init(&encrypter);
EVP_EncryptInit_ex(&encrypter, EVP_aes_256_cbc(), NULL, keytmp, iv);
EVP_CIPHER_CTX_init(&decrypter);
EVP_DecryptInit_ex(&decrypter, EVP_aes_256_cbc(), NULL, keytmp, iv);
return true;
}
unsigned char * AES::decrypt(const char * msg){
int mlen = strlen((const char*)msg), final_len;
unsigned char * returns = (unsigned char*)malloc(mlen);
EVP_DecryptInit_ex(&decrypter, NULL, NULL, NULL, NULL);
EVP_DecryptUpdate(&decrypter, (unsigned char*)msg, &mlen, returns, mlen);
EVP_DecryptFinal_ex(&decrypter, (unsigned char*)msg+mlen, &final_len);
return returns;
}
unsigned char * AES::encrypt(const char * msg){
int mlen = strlen((const char*)msg), final_len;
unsigned char * returns = (unsigned char*)malloc(mlen); // char is 1 byte.
EVP_EncryptInit_ex(&encrypter, NULL, NULL, NULL, NULL);
EVP_EncryptUpdate(&encrypter, (unsigned char*)msg, &mlen, returns, mlen);
EVP_EncryptFinal_ex(&decrypter, (unsigned char*)msg+mlen, &final_len);
return returns;
}
AES::AES(const char * key, const char * salt)
{
// Load the necessary cipher
EVP_add_cipher(EVP_aes_256_cbc());
this->error = !this->aes_init((unsigned char*)key, (unsigned char*)salt);
}