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;

        }

        
        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;

        }


        printf( "The deciphered Data is: %s \nand the size is: %d\n\n", 
                        decipheredDataAux,
                        decipheredDataLength );

        decipheredNum = 0;

        if( EVP_DecryptFinal( &ctx, &decipheredDataAux[decipheredDataLength],
&decipheredNum ) == 0 )
        //      ERR_print_errors_fp( stderr );
        

        decipheredDataLength += decipheredNum;

        printf( "The deciphered Data is: %s \nand the size is: %d\n\n", 
                        decipheredDataAux,
                        decipheredDataLength );
        
        EVP_CIPHER_CTX_cleanup (& ctx);
        return decipheredDataLength;
}
/**/

int main( int argc, char **argv )
{

        OpenSSL_add_all_algorithms();
        ERR_load_crypto_strings();

        unsigned char *key = new unsigned char[196];

        int it = 0;
        
        for( ; it < 196; it ++ )
                key[it] = 'A';


        unsigned char *data = new unsigned char[DATA_LENGTH];

        for( it = 0; it < DATA_LENGTH; it ++ )
                data[it] = 'P';

        unsigned char *cipheredData;
        int cipheredDataLength = encrypt( key, data, DATA_LENGTH, &cipheredData 
);

        unsigned char *decipheredData;
        decrypt( key, cipheredData, cipheredDataLength, &decipheredData );

        return 0;
}




The prints of the console that I get, are:

The original Data is: PPPPPPPPPPPPPPPPPPPPPPPPPPPPPÂÂÂÂ
and the size is: 29

The ciphered Data is: 
Ã9%{Ã<nâÃ9%{Ã<nâÃ9%{Ã<nâÃ9%{Ã<nâââââÂÂÂÂQ
and the size is: 32

The ciphered Data is: 
Ã9%{Ã<nâÃ9%{Ã<nâÃ9%{Ã<nâÃ9%{Ã<nâÂâ/âÃH~\Q
and the size is: 40

The ciphered Data is: 
Ã9%{Ã<nâÃ9%{Ã<nâÃ9%{Ã<nâÃ9%{Ã<nâÂâ/âÃH~\Q
and the size is: 40

The deciphered Data is: 
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPââââââPPPPPPPPÂÂÂÂ
and the size is: 40

The deciphered Data is: 
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPââââââPPPPPPPPÂÂÂÂ
and the size is: 40

Press any key to continue
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to