无法在openssl c+;中解密aes-128加密数据+; 我在C++中使用OpenSSL AES加密-128算法,主要方法是加密/解密。但当我调用两个单独的加密和解密方法时,加密工作正常,但无法解密。每次结果都不一样 #include

无法在openssl c+;中解密aes-128加密数据+; 我在C++中使用OpenSSL AES加密-128算法,主要方法是加密/解密。但当我调用两个单独的加密和解密方法时,加密工作正常,但无法解密。每次结果都不一样 #include ,c++,openssl,C++,Openssl,无法在openssl c+;中解密aes-128加密数据+; 我在C++中使用OpenSSL AES加密-128算法,主要方法是加密/解密。但当我调用两个单独的加密和解密方法时,加密工作正常,但无法解密。每次结果都不一样 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/aes.h> #include <openssl/md

无法在openssl c+;中解密aes-128加密数据+; 我在C++中使用OpenSSL AES加密-128算法,主要方法是加密/解密。但当我调用两个单独的加密和解密方法时,加密工作正常,但无法解密。每次结果都不一样

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/md5.h>
#include <openssl/rand.h>
#include "Base64.cpp"
#include <iostream>

using namespace std;

/* Print Encrypted and Decrypted data packets */
void print_data(const char *tittle, const void* data, int len);

unsigned char* decrypt(char* encryptedText, unsigned char key[],int inputSize, int keySize);
char* encrypt(unsigned char plainText[] , unsigned char key[],int inputSize,int keySize);
static unsigned char iv[AES_BLOCK_SIZE];
static unsigned char iiv[AES_BLOCK_SIZE];
static unsigned char iiiv[AES_BLOCK_SIZE];
static unsigned char iiiiv[AES_BLOCK_SIZE];
int main(int argc, char * argv[])
{

    unsigned char aes_key[16]={0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};
    unsigned char aes_input[]={0x0,0x1,0x2,0x3,0x4,0x5,0x4,0x4,0x4,0x4,0x4,0x4,0x4};

    /* Init vector */
        if(!RAND_bytes(iv,sizeof(iv)))
                cout<<"Problem in Initilizing IV";
        memcpy(iiv, iv, sizeof(iv));
        memcpy(iiiv, iiv, sizeof(iiv));
        memcpy(iiiiv, iiiv, sizeof(iiiv));

        /* Buffers for Encryption and Decryption */
        unsigned char enc_out[sizeof(aes_input)];
        unsigned char dec_out[sizeof(aes_input)];

        /* AES-128 bit CBC Encryption */
        AES_KEY enc_key, dec_key;
        AES_set_encrypt_key(aes_key, sizeof(aes_key)*8, &enc_key);
        AES_cbc_encrypt(aes_input, enc_out, sizeof(aes_input), &enc_key, iv, AES_ENCRYPT);

        /* AES-128 bit CBC Decryption */
        AES_set_decrypt_key(aes_key, sizeof(aes_key)*8, &dec_key); // Size of key is in bits
        AES_cbc_encrypt(enc_out,dec_out, sizeof(aes_input), &dec_key, iiv, AES_DECRYPT);

        /* Printing and Verifying */
        print_data("\n Original : ",aes_input, sizeof(aes_input)); // you can not print data as a string, because after Encryption its not ASCII
        print_data("\n Encrypted: ",enc_out, sizeof(enc_out));
        print_data("\n Decrypted: ",dec_out, sizeof(dec_out));


        char* baseOut =  encrypt(aes_input, aes_key, sizeof(aes_input), sizeof(aes_key));
        decrypt(baseOut, aes_key, sizeof(aes_input), sizeof(aes_key));
    return 0;
}

char* encrypt(unsigned char plainText[], unsigned char key[], int inputSize, int keySize)
{
     unsigned char* encryptedText;
     unsigned char enc_out[inputSize];
     unsigned char iv[AES_BLOCK_SIZE];
     unsigned char allBytes[AES_BLOCK_SIZE+inputSize];
     AES_KEY enc_key;
     AES_set_encrypt_key(key, keySize*8, &enc_key);
     AES_cbc_encrypt(plainText,enc_out, inputSize, &enc_key, iiiv, AES_ENCRYPT);
     encryptedText = enc_out;
     print_data("\n Encryptedd: ",enc_out,sizeof(enc_out));
     char* base64EncodeOutput;
     Base64Encode(encryptedText, sizeof(enc_out), &base64EncodeOutput);
     return base64EncodeOutput;
}


unsigned char* decrypt(char* encryptedText, unsigned char key[],int inputSize, int keySize)
{
    unsigned char* base64Decoded;
    unsigned char* decryptedText;
    size_t test;
    AES_KEY dec_key;
    unsigned char iv[AES_BLOCK_SIZE];
    Base64Decode(encryptedText, &base64Decoded, &test);
    cout<<inputSize;


    unsigned char temp[inputSize];
    /* AES-128 bit CBC Decryption */
        AES_set_decrypt_key(key, keySize*8, &dec_key); // Size of key is in bits
        AES_cbc_encrypt(base64Decoded, temp, inputSize, &dec_key, iiiiv, AES_DECRYPT);
    print_data("\n Decryptedd: ",temp, inputSize);

}


void print_data(const char *tittle, const void* data, int len)
{
    cout<< tittle;
    const unsigned char * p = (const unsigned char*)data;
    int i = 0;

    for (; i<len; ++i)
        printf("%02X ", *p++);

    printf("\n");
}

在第二种方法中使用了一些Base64Encode/Base64Decode函数,而在第一种方法中没有使用。您是否已对这些功能的使用情况进行了三次检查,是否确定它们按预期工作?您不应使用
AES\u encrypt
和friends。这是一个仅限软件的实现,因此您将无法享受硬件支持,如AES-NI。您应该使用
EVP.*
函数。请参见OpenSSL wiki上的。事实上,您可能应该使用经过身份验证的加密,因为它同时提供机密性和真实性。请参阅OpenSSL wiki。是的,我打印并测试了base64编码和解码值在我的两个函数中都是相同的:-dimI不知道aes这个词-ni我刚刚读到这个我正在尝试这样做,但是你能告诉我上面的代码中有什么错误导致了不同的输出吗?:jww
 Original : 00 01 02 03 04 05 04 04 04 04 04 04 04 

 Encrypted: CB 2C 3E 55 3C 99 80 AB B0 08 9D 5C DB 
 Decrypted: 00 01 02 03 04 05 04 04 04 04 04 04 04 

 Encrypted in my method: CB 2C 3E 55 3C 99 80 AB B0 08 9D 5C DB 
 Decrypted in my method: 33 99 BD 6D 27 38 3C 6F 7D B5 2E FA 90