lundi 14 décembre 2020

AES_encrypt : initialization of one of the variables

I have this function that in the first call, is giving me back the Correct Encrypted Value

120692dbcdca656394fc10147e2418f2

But all that comes after are incorrect :

764e1a39b43c42f30da2e9e327d4ed22
b93b46dbc936ae3b06f571ffe1a59cac
b93b46dbc936ae3b06f571ffe1a59cac
a71787b35326e282f8c1bf3a0a034620

I'm new with C++ and I think it is a matter of initialization of one of the variables. I did many Tests but I'm not able to point out where is the Error. Could someone help me please ? Thank You -

#include <openssl/aes.h>
#include <iostream>
#include <iomanip>
using namespace std;


char* pxEnAndDeCrypt(char* pStr )
{
    
    static const unsigned char key[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
    char *ptr = NULL;

    unsigned char enc_out[80]= {};
    unsigned char dec_out[80]= {};
    
    int i,j,lenHexa ;
    char enc_out_HEXA[200]= {};
    unsigned char enc_out_TRANSF[200]= {};
    unsigned char enc_out_BACK[200]= {};

    AES_KEY enc_key, dec_key;

        // ENCRYPT :                              Input =*pStr             Output = enc_out

        AES_set_encrypt_key(key, 128, &enc_key);
        AES_encrypt((const unsigned char *)pStr, enc_out, &enc_key);  

        // TRANSFORM OUTPUT OF ENCRYPT TO HEXA :  Input =enc_out           Output = enc_out_HEXA

        int len  = strlen((char*)enc_out);

        for (i = 0, j = 0; i < len; ++i, j += 2)
        { 
              sprintf(enc_out_HEXA  + j, "%02x", enc_out[i] & 0xff);
        }
        ptr = (char *) enc_out_HEXA;

// OUTPUT 

    return ptr;
}

Aucun commentaire:

Enregistrer un commentaire