Hi,
I'm trying to do a program to encrypt and decrypt a file using EVP API. I read the input file (plaintext) in binary mode using a buffer of 100 bytes to encrypt. The function restult_aes is the encryption / decryption funcion. I'm calling the funcions EVP_Encrypt and EVP_Decrypt for each 100 bytes in the buffer. Then padding is added for each operation in the loop. I think this is a better solution than store all bytes in a big buffer and then call the funcions of EVP API, but I'm not sure about it. Here is my code:
unsigned char *buffer = (unsigned char *) malloc(100); // buffer of 100 bytes
FILE *fp = fopen(path_input, "rb");
FILE *new_file = fopen("path_output", "ab");
while (!feof(fp))
{
fread(buffer,sizeof(buffer),1, fp); //read from buffer
out_file = result_aes(mode, key, buffer); // encrypt or decrypt the file in binary mode
fwrite(out_file, sizeof(out_file),1 , new_file); //store the encrypted data in a binary file
}
fclose(fp);
fclose(new_file);
unsigned char *result_aes(int mode, unsigned char *key, unsigned char *string_text)
{
EVP_CIPHER_CTX *ctx;
unsigned char *output = NULL;
unsigned char key1[] = {0,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};
unsigned char iv1[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; ctx = setup_encrypt_decrypt(mode, key1, iv1);
if (mode == 1)//encrypt output = encrypt(ctx, string_text, 100);//string_text = plaintext
if (mode == 2)//decrypt output = decrypt(ctx, string_text, 112);//string_text = ciphertext
return output;
}
EVP_CIPHER_CTX *setup_encrypt_decrypt(int mode, unsigned char *key, unsigned char *iv)
{
EVP_CIPHER_CTX *ctx = (EVP_CIPHER_CTX *)malloc(sizeof(EVP_CIPHER_CTX));
EVP_CIPHER const *cipher = EVP_aes_256_cbc( );
EVP_CIPHER_CTX_init(ctx); //initialize the context for encryption
if (mode==1) //prepare for encryption EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv);
if (mode==2) //prepare for decryption EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv);
return ctx; }
unsigned char *encrypt(EVP_CIPHER_CTX *ctx, unsigned char *plaintext,int ptlen)
{
unsigned char *outbuf = NULL;
int outlen, tmplen;
int mem = ptlen + EVP_CIPHER_CTX_block_size(ctx);
outbuf = (unsigned char *) malloc(mem);
EVP_EncryptUpdate(ctx, outbuf, &outlen, plaintext, ptlen);
EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen);
outlen += tmplen;
return outbuf;
}
unsigned char *decrypt(EVP_CIPHER_CTX *ctx, unsigned char *ciphertext,int ctlen)
{
unsigned char *recover_msg = NULL;
int size_recover_msg, size_final;
recover_msg =(unsigned char *) malloc(ctlen + EVP_CIPHER_CTX_block_size(ctx)+1);
EVP_DecryptUpdate(ctx, recover_msg, &size_recover_msg, ciphertext, ctlen);
EVP_DecryptFinal_ex(ctx, recover_msg + size_recover_msg, &size_final);
return recover_msg;
}
In the encryption I use 100 bytes, in the decryption I'm using 112 byter for buffer because of padding. Each 100bytes outputs 112 encrypted bytes in AES 256 CBC mode.
I will be very grateful for any help.
Thanks in advance,
Rafael Cividanes
-- Rafael Cividanes Instituto Tecnológico de Aeronáutica - ITA Divisão de Ciência da Computação - IEC Pça. Mal.Eduardo Gomes, 50 Vila das Acácias CTA-ITA-IEP 12.228-900 São José dos Campos,SP Prédio da Guerra Eletrônica - Sala 235 Tel 12-39476891 E-mail: [EMAIL PROTECTED]
______________________________________________________________________ OpenSSL Project http://www.openssl.org User Support Mailing List openssl-users@openssl.org Automated List Manager [EMAIL PROTECTED]