I'm doing a little program to encrypt and decrypt a string using the EVP API. I couldn't understand if I have to use EVP_DecryptFinal_ex( ) or just EVP_DecryptUpdate( ) in the decryption operation. The second function return the entire recovered plaintext, then I can't understand why EVP_DecryptFinal_ex( ) exist. Just for checking error?

     Here is my code:

int main(int argc, char *argv[])
{
EVP_CIPHER_CTX ctx;
char key[EVP_MAX_KEY_LENGTH];
char iv[EVP_MAX_IV_LENGTH];
char intext[] = "The problem will be solved until12345"; //32 unsigned char outbuf[1024];
unsigned char recover_msg[1024];
unsigned char final[1024];
int size_recover_msg, size_final;
int outlen, tmplen;
int i;


       printf("\n START!!! \n");
       RAND_screen();
       RAND_bytes(key, EVP_MAX_KEY_LENGTH);
       RAND_bytes (iv, EVP_MAX_IV_LENGTH);

       //initialization
       for (i=0; i<1024; i++){
           outbuf[i]='\0';
           recover_msg[i]='\0';
           final[i]='\0';
       }

       //encryption
       EVP_CIPHER_CTX_init(&ctx);
       EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc( ), NULL, key, iv);

printf("\n EVP_CIPHER_CTX_key_length(&ctx): %d ", EVP_CIPHER_CTX_key_length(&ctx));
printf("\n Size of plaintext: %d \n", strlen(intext));


if(!EVP_EncryptUpdate(&ctx, outbuf, &outlen, intext, strlen(intext))) printf("\n ERROR! \n");
// Buffer passed to EVP_EncryptFinal() must be after data just
// encrypted to avoid overwriting it.
if(!EVP_EncryptFinal_ex(&ctx, outbuf + outlen, &tmplen)) printf("\n ERROR!! \n");
outlen += tmplen;


       //print the ciphertext
       printf("\n ciphertext: \n");
       for (i=0;i<outlen;i++) printf("%02x",outbuf[i]);
       printf("\n");

       EVP_CIPHER_CTX_cleanup(&ctx);

       //Decryption
       if (!EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc( ), NULL, key, iv))
           printf("\n ERROR!! \n");

if (!EVP_DecryptUpdate(&ctx, recover_msg, &size_recover_msg, outbuf, outlen))
printf("\n ERROR!! \n");


       if (!EVP_DecryptFinal_ex(&ctx, final, &size_final))
           printf("\n ERROR!! \n");

       //print the recovered plaintext as a string
       printf("\n recovered_plaintext1: \n %s \n", recover_msg);

       //print the recovered plaintext as an array
       printf("\n recovered_plaintext2: \n ");
       for (i=0;i<=size_recover_msg;i++) printf("%c",recover_msg[i]);

       //print the resultt of finalization phase
       printf("\n\n final: %s \n", final);
       printf("\n size_final = %d \n", size_final);

       EVP_CIPHER_CTX_cleanup(&ctx);

   }//end main

Another thing I coundn't understand is why "recovered_plaintext1" sometimes ataches trash when printed in the screen, and sometimes doesn't.

     Thanks for any help!

Rafael

--
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]

Reply via email to