无法在openssl c ++中解密aes-128加密数据

时间:2017-01-10 19:21:30

标签: c++ openssl

我正在使用c ++中的openssl aes encryption-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");
}

这是我的输出:

 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 

0 个答案:

没有答案