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

问题描述 投票:1回答:1

这是以下问题的后续问题:

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相同,请告诉我是否应该。

c++ encryption openssl aes
1个回答
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");
}
© www.soinside.com 2019 - 2024. All rights reserved.