Encrypt character string in AES using OpenSSL in C++

Viewed 683

I have this AES encryption function with the OpenSSL library that receives 3 values ​​as parameters, the first is the key (16 ASCII characters), the second is the message to be encrypted (also in ASCII) and the third is the size of the message, the function returns the encrypted string in string format.

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>

#include "openSSL/aes.h"

using namespace std;

string encAES(char* Key, char* Msg, int size)
{
    static char* Res;
    string output;
    AES_KEY enc_key;

    static const char* const lut = "0123456789ABCDEF";

    Res = (char *)malloc(size);

    AES_set_encrypt_key((unsigned char *)Key, 128, &enc_key);

    for(int aux = 0; aux <= size; aux += 16)
    {
        AES_ecb_encrypt((unsigned char *)Msg + aux, (unsigned char *)Res + aux, &enc_key, AES_ENCRYPT);
    }        

    output.reserve(2 * size);

    for (size_t i = 0; i < size; ++i)
    {
        const unsigned char c = Res[i];
        output.push_back(lut[c >> 4]);
        output.push_back(lut[c & 15]);
    }

    free(Res);

    return output;
}

This is how i call and use the function:

int main(int argc, char const *argv[])
{
    char charKey[16 + 1] = {0};
    char charMes[800 + 1] = {0};

    //16 KEY ASCII
    string str_key = "ABCDEFGHIJKLMNOP";
    memset(charKey, 0, sizeof(charKey));
    strcpy(charKey, str_key.c_str());

    //Variable MESSAGE ASCII
    string str_mes = "Hello World! This is a test chain.";
    memset(charMes, 0, sizeof(charMes));
    strcpy(charMes, str_mes.c_str());

    string enc_mes = encAES(charKey, charMes, sizeof(charMes));

    return 0;
}

What I want is that the function encAES() now receive two values: the key and the message in string, try the following but it generates errors (I do not know since the program closes unexpectedly):

string encAES2(string enc_key, string enc_message)
{
    int enc_message_len = (enc_message.length() / 2);
    unsigned char key1[16] = {0};
    unsigned char in[800] = {0};
    unsigned char out[800] = {0};
    AES_KEY aes_key;

    memset(key1, 0, sizeof(key1));
    memset(in, 0, sizeof(in));
    memset(out, 0, sizeof(out));

    for(int aux = 0, str_len = enc_key.length(); aux != str_len; aux += 2)
    {   
        int valor;
        stringstream ss;

        ss << std::hex << enc_key.substr(aux, 2);
        ss >> valor;

        key1[aux/2] = valor;
    }

    for(int aux = 0, str_len = enc_message.length(); aux != str_len; aux += 2)
    {   
        int valor;
        stringstream ss;

        ss << std::hex << enc_message.substr(aux, 2);
        ss >> valor;

        in[aux/2] = valor;
    }

    AES_set_encrypt_key((unsigned char *)key1, 128, &aes_key);

    for(int aux = 0; aux <= 320; aux += 16)
    {
        AES_ecb_encrypt((unsigned char *)(in + aux), (unsigned char *)(out + aux), &aes_key, AES_ENCRYPT);
    }

    string encrypt_str = unsignedcharHex_to_stringHex(out, 320);

    return encrypt_str;
}
0 Answers
Related