在C+中解密AES+;例子 我需要一些帮助,用开放的SSL库使用AES解密来解密C++中的字符数组。我已经完成了加密模式,工作正常,但解密不起作用

在C+中解密AES+;例子 我需要一些帮助,用开放的SSL库使用AES解密来解密C++中的字符数组。我已经完成了加密模式,工作正常,但解密不起作用,c++,encryption,openssl,cryptography,C++,Encryption,Openssl,Cryptography,这是加密函数: 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 *)K

这是加密函数:

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;
}
字符串加密(字符*密钥、字符*消息、整数大小)
{
静态字符*Res;
静态常量字符*常量lut=“0123456789ABCDEF”;
字符串输出;
AES_键和enc_键;
Res=(字符*)malloc(大小);
AES加密密钥((无符号字符*)密钥、128和enc密钥);
对于(int-vuelta=0;vuelta>4]);
输出。推回(lut[c&15]);
}
免费(Res);
返回输出;
}
这是解密函数(不工作):

char*解密(char*密钥、char*消息、int-size)
{
静态字符*Res;
AES_键dec_键;
Res=(字符*)malloc(大小);
AES_set_decrypt_key((无符号字符*)key、128和dec_key);

对于(int vuelta=0;vuelta,我想回答我是如何解决它的:我的示例的问题是,我试图使用十六进制字符串的解密函数,应该使用带值的ASCII字符串​​由加密功能提供

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

必须使用如下字符串对其进行解密:@(%\u!\ 35;$

之后,解密将以ASCII值传递。它们必须传递到十六进制,最后传递到字符串

下面是一个对我有用的例子:

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;
}
string Decrypt\u string(字符*键,字符串十六进制消息,整数大小)
{
静态常量字符*常量lut=“0123456789ABCDEF”;
int i=0;
字符*Res;
AES_键dec_键;
字符串auxString,output,newString;
对于(i=0;i4]);
输出。推回(lut[c&15]);
}
int len=output.length();
对于(int i=0;i
感谢您的回答,我上传了一个调用函数的主函数示例。我有一个问题,就是如何将解密响应显示为字符串。
解密(key,enc_message,sizeof(enc_message));
可能是错误的。密码文本的大小应该由
Encrypt
函数返回。它应该是16或32字节,取决于您的消息(并且只看一眼代码)也许你应该看看OpenSSL维基。还有C++库,比如,你不应该使用<代码> AESYNECTRYP> <代码>和朋友。这只是一个软件实现,所以你将不喜欢硬件支持,比如AES-NI。你应该使用<代码> EVP**<代码>函数。ld可能正在使用经过身份验证的加密,因为它提供了机密性和真实性。请参阅OpenSSL wiki上的。
#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;
}
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;
}