Rafael Cividanes wrote:
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);

instead of doing something like while (...) { fread(); EVP_CIPHER_CTX_init(); EVP_EncryptInit_ex(); EVP_EncryptUpdate(); EVP_EncryptFinal_ex(); fwrite(); }

you better try something like
        EVP_CIPHER_CTX_init();
        EVP_EncryptInit_ex();
        while (...) {
                fread();
                EVP_EncryptUpdate();
                fwrite();
        }
        EVP_EncryptFinal_ex();
        fwrite();

see `man EVP_CipherInit`

Nils
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to