Crypto++ 异常调用 messageEnd

Crypto++ exception calling messageEnd

我使用以下代码解密文件:

FileSource fe(fileUrl.c_str(), false,
                  new AuthenticatedDecryptionFilter(decryptor, new FileSink(
                          std::string(fileUrl).c_str()), CryptoPP::AuthenticatedDecryptionFilter::THROW_EXCEPTION |  CryptoPP::AuthenticatedDecryptionFilter::MAC_AT_END ));

        size_t BLOCK_SIZE = 16384;
    while (remaining && !fe.SourceExhausted()) {
        const unsigned int req = STDMIN(remaining, BLOCK_SIZE);
        fe.Pump(req);
        fe.Flush(false);

        remaining -= req;
    }
    fe.MessageEnd();

如果我尝试在没有 fe.MessageEnd() 的情况下执行此操作,我的解密文件短 16 个字节。所以我想我需要调用 MessageEnd() 来解决这个问题。 但是如果我调用 MessageEnd() 我得到 Follwing Exception: BufferedTransformation: this object doesn't allow input

if i call MessageEnd() I get Follwing Exception: BufferedTransformation: this object doesn't allow input...

正确。 FileSource 是来源,消息必须存在。您不能在源上调用 PutPut2 来向消息中添加其他数据。


我认为您有两种选择可以更好地控制信号。

第一

Source 上致电 Flush

const int opts = AuthenticatedDecryptionFilter::THROW_EXCEPTION |
    AuthenticatedDecryptionFilter::MAC_AT_END;

FileSource fe(fileUrl.c_str(), false,
    new AuthenticatedDecryptionFilter(decryptor, new FileSink(
        std::string(fileUrl).c_str()), opts));

fe.Flush(true);

另请参阅手册中 Filter::Flush 中对 Flush 的注释。

第二

存储指向过滤器的指针并对其调用 MessageEnd

const int opts = AuthenticatedDecryptionFilter::THROW_EXCEPTION |
     AuthenticatedDecryptionFilter::MAC_AT_END;
AuthenticatedDecryptionFilter* adf = NULL;

FileSource fe(fileUrl.c_str(), false,
    adf = new AuthenticatedDecryptionFilter(decryptor, new FileSink(
        std::string(fileUrl).c_str()), opts));
adf.MessageEnd();

这有点不寻常,所以我不确定您会遇到什么副作用。

不要删除指针。 FileSource 将在右大括号超出范围时将其删除。


... my decrypted file is 16 bytes short...

在我看来,如果在 Source 上调用 Flush 对您不起作用,这就是您应该解决的问题。

还要记住... AuthenticatedEncryptionFilter 的输出是二元组 {ciphertext,mac},由于MAC,所以得到16字节的密文扩展。以后使用AuthenticatedDecryptionFilter时,验证后去掉mac。所以恢复的文本应该与明文大小相同,两者都应该比密文少 16 个字节。

我不清楚的是,事情是否按预期工作,但您没有意识到它应该如何工作。或者你真的在某处丢失了 16 字节的恢复文本。