Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用C+中的OpenSSL加密AES中的字符串+;_C++_Encryption_Openssl - Fatal编程技术网

C++ 使用C+中的OpenSSL加密AES中的字符串+;

C++ 使用C+中的OpenSSL加密AES中的字符串+;,c++,encryption,openssl,C++,Encryption,Openssl,我有一个带有OpenSSL库的AES加密函数,它接收3个值​​作为参数,第一个是密钥(16个ASCII字符),第二个是要加密的消息(也是ASCII格式),第三个是消息的大小,函数以string格式返回加密字符串 #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <stdarg.h> #include "open

我有一个带有OpenSSL库的AES加密函数,它接收3个值​​作为参数,第一个是密钥(16个ASCII字符),第二个是要加密的消息(也是ASCII格式),第三个是消息的大小,函数以
string
格式返回加密字符串

#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;
}
我想要的是函数
encAES()
现在接收两个值:
string
中的键和消息,请尝试以下操作,但它会生成错误(我不知道,因为程序意外关闭):

string encase2(字符串加密键、字符串加密消息)
{
int enc_message_len=(enc_message.length()/2);
无符号字符key1[16]={0};
[800]={0}中的无符号字符;
无符号字符输出[800]={0};
AES_密钥AES_密钥;
memset(键1,0,sizeof(键1));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
对于(int aux=0,str_len=enc_key.length();aux!=str_len;aux+=2)
{   
英勇;
细流ss;
党卫军英勇;
键1[aux/2]=valor;
}
对于(int aux=0,str_len=enc_message.length();aux!=str_len;aux+=2)
{   
英勇;
细流ss;
党卫军英勇;
in[aux/2]=勇气;
}
AES设置加密密钥((无符号字符*)密钥1、128和AES密钥);

对于(int aux=0;aux 1.您工作太辛苦了。OpenSSL不需要每个块调用,它将在一次调用中处理整个消息,并根据需要添加填充。2.请注意,AES是基于字节数组的,而不是基于字符的,因此
strcpy
将不起作用,因为输出是包含0x00的随机字节。3.ECB模式在最低限度上是不安全的有随机IV或GCM模式的CBC模式可供选择,适合您的需要。
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;
}
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;
}