解密文件时出现 AES_256_CBC return "bad decrypt" 错误

Decrypting a file with AES_256_CBC return "bad decrypt" error

这是这个问题的后续问题:

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. 我在 Windows 上使用相同版本的 OpenSsl,我有 2 个 Visual Studio 项目,因此不兼容的 OpenSsl 库应该不会有问题。

如果有人有任何指示,请告诉我。我以前从未使用过加密,所以有些东西很难理解。

PS:我没有包含 EncryptDecrypt 方法,它们与 Openssl Wiki website 中的方法相同,如果需要请告诉我。

您的代码中存在许多问题...仅举几例:

  • ofstream out("d:/temp.YML"); 应该以二进制模式打开。
  • out << temp << endl; 会通过添加不必要的换行符来破坏二进制(加密)数据。
  • 输出缓冲区应包含足够的 space 以适应(输入缓冲区 + block_size)。
  • Encryption/decryption 块中必须遵循 update/final 模式。你不能 encrypt/decrypt 独立分块。
  • 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");
}