0

I need some help with decrypt a char array in C++ using AES decrypt with Open SSL library. I already done encryption mode and works fine, but decryption is not working.

This is the Encrypt Function:

string Encrypt(char *Key, char *Msg, int size)
{
    static char* Res;
    static const char* const lut = "0123456789ABCDEF";
    string output;
    AES_KEY enc_key;

    Res = (char *)malloc(size);

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

    for(int vuelta = 0; vuelta <= size; vuelta += 16)
    {
        AES_ecb_encrypt((unsigned char *)Msg + vuelta, (unsigned char *)Res + vuelta, &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 the Decrypt Function (not working):

char * Decrypt( char *Key, char *Msg, int size)
{
    static char* Res;
    AES_KEY dec_key;

    Res = ( char * ) malloc( size );

    AES_set_decrypt_key(( unsigned char * ) Key, 128, &dec_key);

    for(int vuelta= 0; vuelta<=size; vuelta+=16)
    {
        AES_ecb_encrypt(( unsigned char * ) Msg+vuelta, ( unsigned char * ) Res+vuelta, &dec_key, AES_DECRYPT); 
    }

    return (Res);
}

This is an Example of the Main function that call the methods, the problem is thar no mather how i print the "Res" variable in the Decrypt function, it always show random ASCII values, and i like to show the result in a string like the Encrypt function:

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

#include "openSSL/aes.h"

using namespace std;

int main(int argc, char const *argv[])
{
    char key[16];
    char message[128];
    char enc_message[128];

    string s_key = "THIS_IS_THE_KEY_";
    string s_message = "Hello World !!!";

    memset(key, 0, sizeof(key));
    strcpy(key, s_key.c_str()); 

    memset(message, 0, sizeof(message));
    strcpy(message, s_message.c_str()); 

    string response = Encrypt(key, message, sizeof(message));

    cout<<"This is the Encrypted Message: "<<response<<endl;

    memset(enc_message, 0, sizeof(enc_message));
    strcpy(enc_message, response.c_str());

    Decrypt(key, enc_message, sizeof(enc_message));

    return 0;
}

Any improve in this methods?

Noe Cano
  • 495
  • 2
  • 8
  • 22
  • Thanks for your answer, I uploaded an example of the main function invoking functions. I have probelmas to show the decryption response as a string. – Noe Cano Apr 21 '17 at 21:32
  • 1
    `Decrypt(key, enc_message, sizeof(enc_message));` is probably wrong. The size of the cipher text should be returned by your `Encrypt` function. It should be 16 or 32 bytes based on your message (and only glancing at the code). Maybe you should have a look at [EVP Symmetric Encryption and Decryption | C++ Programs](https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption#C.2B.2B_Programs) on the OpenSSL wiki. There are also C++ libraries available, like [Botan](https://github.com/randombit/botan) and [Crypto++](https://github.com/weidai11/cryptopp). – jww Apr 23 '17 at 03:32
  • 1
    You should *not* use `AES_encrypt` and friends. That's a software-only implementation, so you will not enjoy hardware support, like AES-NI. You should be using `EVP_*` functions. See [EVP Symmetric Encryption and Decryption](http://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption) on the OpenSSL wiki. In fact, you should probably be using authenticated encryption because it provides *both* confidentiality and authenticity. See [EVP Authenticated Encryption and Decryption](http://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption) on the OpenSSL wiki. – jww Apr 23 '17 at 03:32

1 Answers1

1

I wanted to put the answer to how I solved it: The problem with my example was that I was trying to use the decrypt function with a HEXADECIMAL STRING and it should be done with an ASCII STRING with the values ​​as delivered by the encryption function.

That is, instead of trying to decrypt a string like this: 461D019896EFA3

It must be decrypted with a string like this: @(%_!#$

After that, the decryption will be delivered in ASCII values. They must be passed to Hexadecimal and finally to a String.

Here is the example that worked for me:

string Decrypt_string(char *Key, string HEX_Message, int size)
{
    static const char* const lut = "0123456789ABCDEF";
    int i = 0;
    char* Res;
    AES_KEY dec_key;
    string auxString, output, newString;

    for(i = 0; i < size; i += 2)
    {
        string byte = HEX_Message.substr(i, 2);
        char chr = (char) (int)strtol(byte.c_str(), NULL, 16);
        auxString.push_back(chr);
    }

    const char *Msg = auxString.c_str();
    Res = (char *)malloc(size);

    AES_set_decrypt_key((unsigned char *)Key, 128, &dec_key);

    for(i = 0; i <= size; i += 16)
    {
        AES_ecb_encrypt((unsigned char *)Msg + i, (unsigned char *)Res + i, &dec_key, AES_DECRYPT);
    }

    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]);
    }

    int len = output.length();

    for(int i = 0; i < len; i += 2)
    {
        string byte = output.substr(i, 2);
        char chr = (char) (int)strtol(byte.c_str(), NULL, 16);
        newString.push_back(chr);
    }

    free(Res);

    return newString;
}
Phi
  • 467
  • 5
  • 16
Noe Cano
  • 495
  • 2
  • 8
  • 22