0

I have the working code below using OPENSSL AES 256 CBC to encrypt/decrypt. It is working but I am missing something really important that is to CONVERT the Encryption result to readable text and STORE it to a STRING variable if possible (for later use). For example, I need to see something like this: UkV8ecEWh+b1Dz0ZdwMzFVFieCI5Ps3fxYrfqAoPmOY= Trying hard to find how to do that and what format OPENSSL is throwing out from Encryption process. (binary format ??) See image attached.

ps. Don't worry about the hashes below. They are not in production.

Thanks in Advance!!

Here is my code so far:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <openssl/evp.h>
#include <openssl/aes.h>
#include <openssl/rand.h>



using namespace std;


// HEX PRINT
static void hex_print(const void* pv, size_t len)
{
   const unsigned char* p = (const unsigned char*)pv;
   if (NULL == pv)
       printf("NULL");
   else
   {
       size_t i = 0;
       for (; i < len; ++i)
           printf("%02X ", *p++);
   }
   printf("\n");
}


// Starting MAIN function
int main()
{
   int keylength = 256;
   unsigned char aes_key[] =  "1Tb2lYkqstqbh9lPAbeWpQOs3seHk6cX";

   // Message we want to encrypt
   unsigned char aes_input[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz";
   
   size_t inputslength = sizeof(aes_input)-1;   // -1 because we don't want to encrypt the \0 character
   
   // initialization vector  IV - same for Encryption and Decryption
   unsigned char iv_enc[] =  "JxebB512Gl3brfx4" ;
   unsigned char iv_dec[] =  "JxebB512Gl3brfx4" ;
   
   // buffers for encryption and decryption
   const size_t encslength = inputslength ;
   unsigned char enc_out[257];
   unsigned char dec_out[257];
   memset(enc_out, 0, sizeof(enc_out));
   memset(dec_out, 0, sizeof(dec_out));

   //Encryption START
   AES_KEY enc_key, dec_key;
   AES_set_encrypt_key(aes_key, keylength, &enc_key);
   AES_cbc_encrypt(aes_input, enc_out, inputslength, &enc_key, iv_enc, AES_ENCRYPT);
   
   //Decryption START
   AES_set_decrypt_key(aes_key, keylength, &dec_key);
   AES_cbc_encrypt(enc_out, dec_out, encslength, &dec_key, iv_dec, AES_DECRYPT);
       
   // Printing Results
   printf("original: \t");
   hex_print(aes_input, sizeof(aes_input));
   cout << aes_input << endl;

   printf("encrypted: \t");
   hex_print(enc_out, sizeof(enc_out));
   cout << enc_out << endl;
   

   printf("decrypt: \t");
   hex_print(dec_out, sizeof(dec_out));
   cout << dec_out << endl;
   
   
 
   return 0;
}

Image of the Process

  • 4
    Have you heard of [base 64 encoding](https://en.wikipedia.org/wiki/Base64)? – Paul Sanders Nov 24 '20 at 18:23
  • OpenSSL only knows about binary data, it has no concept of text. If you want encrypted data to be in a textual format, you need to *encode* the encrypted binary yourself, such as a hex string, or a base64 string (which is what your `UkV8ecEWh+b1Dz0ZdwMzFVFieCI5Ps3fxYrfqAoPmOY=` example is). – Remy Lebeau Nov 24 '20 at 18:34
  • @RemyLebeau Nice tip! I could research and find a nice solution for my case. I Appreciate the help! – Fabricio Guzzy Dec 02 '20 at 16:23
  • @PaulSanders Nice tip! I could research and find a nice solution for my case. I Appreciate the help! – Fabricio Guzzy Dec 02 '20 at 16:24

1 Answers1

0

All Right. Thanks for the tips @RemyLebeau and @PaulSanders !! I could resolve the issue using another tip from here --> Base64 C++

Working REALLY fine now!!

Thanks Much!!

Here is the code for "encode" and "decode" Base64, just in case someone wants to do the same. Very usefull!!

typedef unsigned char uchar;
static const string b = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

static string base64_encode(const string &in) {
    string out;

    int val=0, valb=-6;
    for (uchar c : in) {
        val = (val<<8) + c;
        valb += 8;
        while (valb>=0) {
            out.push_back(b[(val>>valb)&0x3F]);
            valb-=6;
        }
    }
    if (valb>-6) out.push_back(b[((val<<8)>>(valb+8))&0x3F]);
    while (out.size()%4) out.push_back('=');
    return out;
}

static string base64_decode(const string &in) {

    string out;

    vector<int> T(256,-1);
    for (int i=0; i<64; i++) T[b[i]] = i;

    int val=0, valb=-8;
    for (uchar c : in) {
        if (T[c] == -1) break;
        val = (val<<6) + T[c];
        valb += 6;
        if (valb>=0) {
            out.push_back(char((val>>valb)&0xFF));
            valb-=8;
        }
    }
    return out;
}