Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert AES encrypted string to hex in C++

Tags:

c++

I have a char* string that I have encoded using AES encryption. This string contains a wide range of hex characters, not just those viewable by ASCII. I need to convert this string so I can send it through HTTP, which does not accept all of the characters generated by the encryption algorithm.

What is the best way to convert this string? I have used the following function but there are a lot of blanks (0xFF), it cant convert all of the characters.

char *strToHex(char *str){
   char *buffer = new char[(dStrlen(str)*2)+1];
   char *pbuffer = buffer;
   int len = strlen( str );
   for(int i = 0; i < len ; ++i ){
      sprintf(pbuffer, "%02X", str[i]);
      pbuffer += 2;
   }
   return buffer;
}

Thank you, Justin

like image 744
Justin M Avatar asked May 01 '26 03:05

Justin M


1 Answers

I don't know if there is a lib in c++ for it, but the best way is to encode the bytes into base64. It's pretty trivial to write your own encoder, if there isn't a standard one around (but I suspect there will be).

like image 133
Noon Silk Avatar answered May 03 '26 17:05

Noon Silk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!