vendredi 11 décembre 2020

AES_encrypt UTF-8

this code is working I think but the problem is returning the Encrypted String that is NOT UTF-8 to a Calling that is in UTF-8, as I need to store this in UTF-8. then,

1- Do you think there is a way to "transform" the Encrypted String to UTF-8 or somehow says to AES_encrypt to generate in UTF-8 ? -2 If not , maybe a solution is transform it like this :

printf("original:\t");
for(i=0;*(text+i)!=0x00;i++)
    printf("%X ",*(text+i));
printf("\nencrypted:\t");
for(i=0;*(enc_out+i)!=0x00;i++)
    printf("%X ",*(enc_out+i));
printf("\ndecrypted:\t");
for(i=0;*(dec_out+i)!=0x00;i++)
    printf("%X ",*(dec_out+i));
printf("\n");

where the output is :

original:       31 32 33 34 35 36 37 38 39 30 31 32
encrypted:      CE 64 C9 54 61 9E B3 AA A9 8B 32 1C AF 39 F5 7A 9
decrypted:      31 32 33 34 35 36 37 38 39 30 31 32

then the idea maybe is

  1. The String Encrypted, transform it to :CE 64 C9 54 61 9E B3 AA A9 8B 32 1C AF 39 F5 7A 9

  2. Pass back to my UTF-8 Program.

  3. to Decrypt, transform CE 64 C9 54 61 9E B3 AA A9 8B 32 1C AF 39 F5 7A 9 to Encrypted String again.

The question is , how can I do to pass from CE 64 C9 54 61 9E B3 AA A9 8B 32 1C AF 39 F5 7A 9 to Encrypted String again ?

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};
    char *ptr;

    unsigned char enc_out[80];
    unsigned char dec_out[80];
 

    AES_KEY enc_key, dec_key;

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

    AES_set_decrypt_key(key,128,&dec_key);
    AES_decrypt(enc_out, dec_out, &dec_key);

    ptr = (char *)enc_out;

    return ptr;
}

Aucun commentaire:

Enregistrer un commentaire