使用AES_256_CBC解密文件会返回“错误解密”错误

时间:2019-04-09 08:19:13

标签: c++ encryption openssl aes

这是该问题的后续问题:

OpenSSL EVP_DecryptFinal_ex returns "wrong final block length" error when decrypting a file

我正在尝试解密文件。起初,我将其读取为ASCII文件而不是二进制文件。修复此问题(我希望)并将其读取为二进制文件后,我总是会遇到"bad decrypt"错误:

15208:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:crypto\evp\evp_enc.c:570:

以下是我如何进行加密和解密的示例:

加密:

Cipher cipher;
ifstream f("d:/test.YML");
ofstream out("d:/temp.YML");
byte key[KEY_SIZE] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2};
byte iv[BLOCK_SIZE] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6};

secure_string line;
secure_string temp;
while (getline(f, line)) {
    cipher.Encrypt(key, iv, line, temp);
    out << temp << endl;
}

解密:

Cipher cipher;
    ifstream f("d:/temp.YML", ifstream::binary);
    ofstream out("d:/tempDecrypt.YML");
    byte key[KEY_SIZE] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2};
    byte iv[BLOCK_SIZE] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6};

    secure_string temp;
    vector<char> buffer(1024, 0);

    while (!f.eof()) {
        f.read(buffer.data(), buffer.size());
        streamsize dataSize = f.gcount();
        secure_string chunk = { buffer.begin(), buffer.begin() + dataSize };

        cipher.Decrypt(key, iv, chunk, temp);
    }

现在,我不确定从哪里开始调查:

  1. 是否存在加密问题?加密文件已生成,我没有发现任何问题。

  2. 我如何读取文件块并将其解密有问题吗?同样,我在这里看不到问题。(错误位于EVP_DecryptFinal_ex

  3. 我还听说填充可能存在问题。我没有做任何与填充相关的事情,所以不确定这是否是一个问题。

  4. 我正在使用相同版本的OpenSsl,在Windows上,我有2个Visual Studio项目,因此不兼容的OpenSsl库不会出现问题。

如果有人有任何建议,请告诉我。我之前从未使用过加密技术,因此有些事情很难理解。

PS:我没有包括EncryptDecrypt方法,它们与Openssl Wiki website上的方法相同,请告诉我是否可以。

1 个答案:

答案 0 :(得分:1)

您的代码中有很多问题……仅举几例:

  • ofstream out("d:/temp.YML");应该以二进制模式打开。
  • out << temp << endl;将通过不必要地添加换行符来破坏二进制(加密)数据。
  • 输出缓冲区应包含足够的空间以容纳(输入缓冲区+ block_size)。
  • 分块中的加密/解密必须遵循update / final模式。您不能独立加密/解密块。
  • IV应该是随机的,并且应与密文一起存储。

看看下面的示例应用程序,该示例程序有效:

#include <cstdint>
#include <fstream>
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <string.h>
#include <stdio.h>

static const size_t KEY_SIZE = 256 / 8, BLOCK_SIZE = 128 / 8;

class AESBase {
protected:
    const uint8_t *key, *iv;
    EVP_CIPHER_CTX *ctx;
    AESBase(const uint8_t *key, const uint8_t *iv) : key(key), iv(iv) {
        if (!(ctx = EVP_CIPHER_CTX_new()))
            handleErrors();
    }
    ~AESBase() {
        EVP_CIPHER_CTX_free(ctx);
    }
    static void handleErrors(void) {
        ERR_print_errors_fp(stderr);
        abort();
    }
};

class Encrypt : AESBase {
public:
    Encrypt(const uint8_t *key, const uint8_t *iv) : AESBase(key, iv) {
        if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
            handleErrors();
    }
    int update(const char *plaintext, int plaintext_len, char *ciphertext) {
        int len;
        if (1 != EVP_EncryptUpdate(ctx, (uint8_t*)ciphertext, &len, (const uint8_t*)plaintext, plaintext_len))
            handleErrors();
        return len;
    }
    int final(char *ciphertext) {
        int len;
        if (1 != EVP_EncryptFinal_ex(ctx, (uint8_t*)ciphertext, &len))
            handleErrors();
        return len;
    }
};

class Decrypt : AESBase {
public:
    Decrypt(const uint8_t *key, const uint8_t *iv) : AESBase(key, iv) {
        if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
            handleErrors();
    }
    int update(const char *ciphertext, int ciphertext_len, char *plaintext) {
        int len;
        if (1 != EVP_DecryptUpdate(ctx, (uint8_t*)plaintext, &len, (const uint8_t*)ciphertext, ciphertext_len))
            handleErrors();
        return len;
    }
    int final(char *plaintext) {
        int len;
        if (1 != EVP_DecryptFinal_ex(ctx, (uint8_t*)plaintext, &len))
            handleErrors();
        return len;
    }
};

void test_encrypt(const uint8_t *key, const char* in, const char* out) {
    std::ifstream fin(in, std::ios_base::binary);
    std::ofstream fout(out, std::ios_base::binary);
    uint8_t iv[BLOCK_SIZE];
    RAND_bytes(iv, sizeof(iv));

    char buf[1024], temp[sizeof(buf) + BLOCK_SIZE];
    Encrypt aes(key, iv);
    fout.write((char*)iv, sizeof(iv));
    while (fin) {
        fin.read(buf, sizeof(buf));
        int len = (int)fin.gcount();
        if (len <= 0)
            break;
        len = aes.update(buf, len, temp);
        fout.write(temp, len);
    }
    int len = aes.final(temp);
    fout.write(temp, len);
}

void test_decrypt(const uint8_t *key, const char* in, const char* out) {
    std::ifstream fin(in, std::ios_base::binary);
    std::ofstream fout(out, std::ios_base::binary);
    uint8_t iv[BLOCK_SIZE];
    fin.read((char*)iv, sizeof(iv));

    char buf[1024], temp[sizeof(buf) + BLOCK_SIZE];
    Decrypt aes(key, iv);
    while (fin) {
        fin.read(buf, sizeof(buf));
        int len = (int)fin.gcount();
        if (len <= 0)
            break;
        len = aes.update(buf, len, temp);
        fout.write(temp, len);
    }
    int len = aes.final(temp);
    fout.write(temp, len);
}

int main()
{
    ERR_load_crypto_strings();
    OpenSSL_add_all_algorithms();
    OPENSSL_config(NULL);

    uint8_t key[KEY_SIZE] = { 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2 };
    test_encrypt(key, "main.cpp", "main.cpp.enc");
    test_decrypt(key, "main.cpp.enc", "main.cpp.txt");
}