2

我现在正在使用 Botan 库。

我想使用 PKCS7 填充模式使用 AES/CBC 模式加密我的文件。

Botan 提供的 AES/CBC 解密在发生错误时会抛出异常,我不确定它是否容易受到 padding oracle 攻击。

那么我应该如何执行解密过程来防止攻击呢?

更新:

  1. 即使我不返回填充错误,文件也会保持不变,攻击者可以知道这一点。

  2. 我的代码如下:(将适当设置iv和key)

    void encrypt(std::istream &in, std::ostream &out)
    {
        try
        {
            Botan::SymmetricKey key_t(key);
            Botan::InitializationVector iv_t(iv);
            Botan::Pipe encryptor(Botan::get_cipher(cipher_mode, key_t, iv_t, Botan::ENCRYPTION), new Botan::DataSink_Stream(out));
            encryptor.start_msg();
            in >> encryptor;
            encryptor.end_msg(); // flush buffers, complete computations
        }
        catch(...)
        {
            throw;
        }
    }
    
    void decrypt(std::istream &in, std::ostream &out)
    {
        try
        {
            Botan::SymmetricKey key_t(key);
            Botan::InitializationVector iv_t(iv);
            Botan::Pipe decryptor(Botan::get_cipher(cipher_mode, key_t, iv_t, Botan::DECRYPTION), new Botan::DataSink_Stream(out));
            decryptor.start_msg();
            in >> decryptor;
            decryptor.end_msg(); // flush buffers, complete computations
        }
        catch(...)
        {
            throw;
        }
    }
    
4

1 回答 1

0

使用随机IV的CBC模式,只需在加密数据前加上IV用于解密,不需要保密。不需要传入IV,让加密函数创建一个随机IV。

于 2017-05-12T18:41:42.760 回答