牡丹AES CBC PKCS7加密解密



我现在正在使用Botan库。

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

Botan 提供的 AES/CBC 解密会在发生错误时引发异常,我不确定它是否容易受到填充预言机攻击。

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

更新:

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

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

    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;
        }
    }
    

将 CBC 模式与随机 IV 一起使用,只需在加密数据前面加上 IV 即可用于解密,它不需要是秘密的。无需传入一个 IV,让加密功能创建一个随机 IV。

最新更新