在C ++示例中解密AES

时间:2017-04-21 20:24:25

标签: c++ encryption openssl cryptography

我需要一些帮助,使用带有Open SSL库的AES解密来解密C ++中的char数组。我已经完成加密模式并且工作正常,但解密不起作用。

这是加密功能:

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

这是Decrypt功能(不工作):

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

这是调用方法的Main函数的示例,问题是如何在Decrypt函数中打印“Res”变量,它总是显示随机ASCII值,我想在结果中显示像加密函数一样的字符串:

#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;
}

这种方法有何改进?

1 个答案:

答案 0 :(得分:1)

我想回答一下我是如何解决它的:我的例子的问题是我试图使用HEXADECIMAL STRING的decrypt函数,并且应该使用ASCII STRING完成,其值为已交付通过加密功能。

也就是说,而不是尝试解密这样的字符串: 461D019896EFA3

必须使用以下字符串对其进行解密: @(%_!#$

之后,解密将以ASCII值传递。它们必须传递给十六进制,最后传递给String。

以下是适用于我的示例:

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