AES-CBC and SHA-512 hash Encryption with C++ produces odd output

  aes, c++, encryption, java, sha

I am currently working on a project in C++ that requires encryption. Normally, I would use Java for this task, however, due to software requirements I have chose C++. After creating an Encryption class with the openssl library, I ran a simple test with AES-CBC 256. The test was a Hello World message encrypted by a hex string key and IV followed by the encrypted result being decrypted. The output below shows the results.

enter image description here

After encryption the binary data contains the original string in plain text as well as the hex value present in the encrypted hex string. After decryption the original hex value for the message is shown in the output as if the process worked.

I am also having problems with creating a SHA-512 hash. Creating a hash in Java differs from the one created in C++. Creating a SHA-256 Hmac hash, however, produces the same output in both languages.

Below is the C++ code I am using in the encryption class.

std::string Encryption::AES::cbc256(const char* data, ssize_t len, const char* key, const char* iv, bool encrypt) {

    std::string keyStr = key;
    std::string ivStr = iv;
    std::string dataStr = data;

    std::string _keyStr = Encryption::Utils::fromHex(keyStr.c_str(), 64);
    std::string _ivStr = Encryption::Utils::fromHex(ivStr.c_str(), 32);
    std::string _dataStr = Encryption::Utils::fromHex(dataStr.c_str(), dataStr.size());

    size_t inputLength = len;

    char aes_input[_dataStr.size()];
    char aes_key[32];

    memset(aes_input, 0, _dataStr.size());
    memset(aes_key, 0, sizeof(aes_key));

    strcpy(aes_input, _dataStr.c_str());
    strcpy(aes_key, _keyStr.c_str());

    char aes_iv[16];
    memset(aes_iv, 0x00, AES_BLOCK_SIZE);
    strcpy(aes_iv, _ivStr.c_str());

    const size_t encLength = ((inputLength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE);

    if(encrypt) {

        char res[inputLength];

        AES_KEY enc_key;
        AES_set_encrypt_key((unsigned char*) aes_key, 256, &enc_key);
        AES_cbc_encrypt((unsigned char*) aes_input, (unsigned char *) res, inputLength, &enc_key, (unsigned char *) aes_iv, AES_ENCRYPT);

        return Encryption::Utils::toHex((unsigned char *) res, strlen(res));

    } else {

        char res[inputLength];

        AES_KEY enc_key;
        AES_set_decrypt_key((unsigned char*) aes_key, 256, &enc_key);
        AES_cbc_encrypt((unsigned char*) aes_input, (unsigned char *) res, inputLength, &enc_key, (unsigned char *) aes_iv, AES_DECRYPT);

        return Encryption::Utils::toHex((unsigned char *) res, strlen(res));



std::string Encryption::SHA::hash512(const char *source) {

    std::string input = source;

    unsigned char hash[64];
    SHA512_CTX sha512;
    SHA512_Update(&sha512, input.c_str(), input.size());
    SHA512_Final(hash, &sha512);

    std::stringstream ss;

    for(int i=0; i<sizeof(hash); i++) {

        ss << std::hex << std::setw(2) << std::setfill('0') << (int) hash[i];


    return ss.str();


Can anyone explain to me, or offer their help, as to why I am getting the output I am getting?

Source: Windows Questions C++