Tyler Durden wrote:
Hello, I write the next c programming code that allow to do a test of
encryption and decryption.
I understand that I do bad, because the EVP_DecryptionFinal return 0
therefore the decrypted data are not match that the original data.

Please help me. Thaks very much

The code is this. I am using Microsoft windows with VC 6.

#include <stdio.h>

#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/x509v3.h>

#define DATA_LENGTH (int) 29

int encrypt( unsigned char *key, unsigned char *originalData, int length,
                                           unsigned char **cipheredData )
{
        
        EVP_CIPHER_CTX ctx;
        const EVP_CIPHER *des3 = EVP_des_ede3();

        *cipheredData = new unsigned char[length + EVP_CIPHER_block_size( des3 
) - 1];
        unsigned char *cipheredDataAux = *cipheredData;
        int cipheredDataLength = 0;


EVP_CIPHER_CTX_init( &ctx );

        if( EVP_EncryptInit( &ctx, des3, key, NULL ) == 0 )
                ERR_print_errors_fp( stderr );

        
        int cipheredNum = 0;


printf( "The original Data is: %s \nand the size is: %d\n\n", originalData,
length );



while( cipheredDataLength < length ) { if( EVP_EncryptUpdate( &ctx, &cipheredDataAux[cipheredNum], &cipheredNum, originalData, length - cipheredNum ) == 0 ) ERR_print_errors_fp( stderr ); cipheredDataLength += cipheredNum;

}

don't use the while-loop, on call to EVP_EncryptUpdate is enough here



printf( "The ciphered Data is: %s \nand the size is: %d\n\n", cipheredDataAux,
cipheredDataLength );

cipheredNum = 0;


        if( EVP_EncryptFinal( &ctx, &cipheredDataAux[cipheredDataLength],
&cipheredNum ) == 0 )
                ERR_print_errors_fp( stderr );
        

        cipheredDataLength += cipheredNum;

printf( "The ciphered Data is: %s \nand the size is: %d\n\n", cipheredDataAux,
cipheredDataLength );
EVP_CIPHER_CTX_cleanup (& ctx);
return cipheredDataLength;
}




int decrypt( unsigned char *key, unsigned char *cipheredData, int length,
                                           unsigned char **decipheredData )
{
        
        EVP_CIPHER_CTX ctx;
        const EVP_CIPHER *des3 = EVP_des_ede3();

        *decipheredData = new unsigned char[length + EVP_CIPHER_block_size( 
des3 )];
        unsigned char *decipheredDataAux = *decipheredData;

        int decipheredDataLength = 0;


EVP_CIPHER_CTX_init( &ctx );

        if( EVP_DecryptInit( &ctx, des3, key, NULL ) == 0 )
                ERR_print_errors_fp( stderr );

        
        int decipheredNum = 0;


printf( "The ciphered Data is: %s \nand the size is: %d\n\n", cipheredData,
length );



while( decipheredDataLength < length ) { if( EVP_DecryptUpdate( &ctx, &decipheredDataAux[decipheredNum], &decipheredNum, cipheredData, length - decipheredNum ) == 0 ) ERR_print_errors_fp( stderr ); decipheredDataLength += decipheredNum;

}

again, the while-loop is unneccesary. One invocation of EVP_DecryptUpdate + EVP_DecryptFinal is enough

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

Reply via email to