OpenSSL AES-CBC-256, Raw data to hex?

hello, I have a AES-256 function using openSSL's EVP library, the output however, comes out as raw ascii characters, how can I convert this to be readable hex characters to compare it with the online php script?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <openssl/evp.h>

int aes_init(unsigned char * key_data, int key_data_len, EVP_CIPHER_CTX * e_ctx, EVP_CIPHER_CTX * d_ctx) {
   int i, nrounds = 5;
   unsigned char key[32], iv[32];
   i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), NULL, key_data, key_data_len, nrounds, key, iv);
   if (i != 32) {
      printf("Key size is %d bits - should be 256 bits\n", i);
      return -1;
   }

   EVP_CIPHER_CTX_init(e_ctx);
   EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), NULL, key, iv);
   EVP_CIPHER_CTX_init(d_ctx);
   EVP_DecryptInit_ex(d_ctx, EVP_aes_256_cbc(), NULL, key, iv);

   return 0;
}

unsigned char * aes_encrypt(EVP_CIPHER_CTX * e, unsigned char * plaintext, int * len) {
   int c_len = *len + AES_BLOCK_SIZE, f_len = 0;
   unsigned char *ciphertext = (unsigned char *)malloc(c_len);

   EVP_EncryptInit_ex(e, NULL, NULL, NULL, NULL);
   EVP_EncryptUpdate(e, ciphertext, &c_len, plaintext, *len);
   EVP_EncryptFinal_ex(e, ciphertext+c_len, &f_len);

   *len = c_len + f_len;
   return ciphertext;
}

unsigned char * aes_decrypt(EVP_CIPHER_CTX * e, unsigned char * ciphertext, int * len) {
   int p_len = *len, f_len = 0;
   unsigned char *plaintext = (unsigned char *)malloc(p_len);
  
   EVP_DecryptInit_ex(e, NULL, NULL, NULL, NULL);
   EVP_DecryptUpdate(e, plaintext, &p_len, ciphertext, *len);
   EVP_DecryptFinal_ex(e, plaintext+p_len, &f_len);

   *len = p_len + f_len;
   return plaintext;
}

int do_aesOPS(char * inputText, char * inputKey) {
   EVP_CIPHER_CTX en, de;

   unsigned char * key_data;
   int key_data_len;

   key_data = (unsigned char *)inputKey;
   key_data_len = strlen(inputKey);
  
   if (aes_init(key_data, key_data_len, &en, &de)) {
      printf("Couldn't initialize AES cipher\n");
      return -1;
   }
   char *plaintext;
   unsigned char *ciphertext;
   int olen, len;
    
   olen = len = strlen(inputText)+1;
    
   ciphertext = aes_encrypt(&en, (unsigned char *)inputText, &len);
   plaintext = (char *)aes_decrypt(&de, ciphertext, &len);

   if (strncmp(plaintext, inputText, olen)) { 
      printf("FAIL: enc/dec failed for \"%s\"\n", inputText);
   }
   else { 
	  printf("AES Encrypted: \"%s\"\n", (char *)ciphertext);
	  printf("AES Decrypted: \"%s\"\n", plaintext);
   }    
   free(ciphertext);
   free(plaintext);
   EVP_CIPHER_CTX_cleanup(&en);
   EVP_CIPHER_CTX_cleanup(&de);

   return 0;
}


Decryption works fine, it comes back out to the starting value, but the encrypt comes out to ascii characters.
Last edited on
Bumping this.


I've tried this bit of code here:

1
2
3
4
5
6
7
8
    std::string hexString;
    //declare the scope of cipher

   	CryptoPP::StringSink* SSINK = new CryptoPP::StringSink(hexString);
	CryptoPP::HexEncoder* hex = new CryptoPP::HexEncoder(SSINK, false);
	CryptoPP::StringSource source(str, true, hex);
	
	return hexString;


using CryptoPP's hex encoder filter, but it just outputs null. This is the very last piece of the system I need (AES-256-CBC), and I just need to convert these ascii characters to HEX in order to complete it.

Any help here would be great, thanks.
Hmm, do you mean you need something like this?
1
2
3
4
5
6
7
8
9
10
11
char * restrict const hexOutput = (char*)malloc(len*2 + 1);
char tmpBuf[4];
int pos=0;

hexOutput[0] = '\0';
for(;pos<len;pos++){
 tmpBuf[0] = '\0';
 sprintf(tmpBuf, "%02x", (int)plainText[pos]);
 strcat(hexOutput,tmpBuf);
}
printf("%s\n",hexOutput);

(i assumed you want C code)
Sorry, but that code doesn't compile for my program, and it only crashes when I call it (I made edits so it compiled).

I was looking for C++ too, but thanks for offering your help. :P
maybe I should provide this:

This is the output I get from the program:

it's an unsigned char * variable.

Çó9úŠö<? ûÞÎ/+O_œÆ…š&ÂÇE-MÛïÍÍýýýýÝÝ€¿'~¿ 0ç

I need to convert that to readable hex.
Last edited on
Hi phantom,

did you found a solution for your convert2hex problem already.

I'm sure, it's easy, but I'm fighting with the same problem and can't fix it.
Topic archived. No new replies allowed.