在C 示例中解密AE

Decrypt AES in C++ Example

本文关键字:解密 AE      更新时间:2023-10-16

我需要使用开放式SSL库中的AES解密在C 中解密A 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;
}

这是解密函数(不工作(:

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

这是调用该方法的主要函数的一个示例,问题是我如何在解密函数中打印" res"变量,它始终显示随机的ascii值,我喜欢在像Encrypt函数之类的字符串:

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

在这种方法中有任何改进吗?

我想对我的解决方法提出答案:示例的问题是我试图用十六进制字符串使用解密功能,应该使用ASCII完成带有加密函数传递的值的字符串。

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

必须用这样的字符串解密:@(%_!#$

之后,解密将以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;
}