Crypto ++异常调用messageEnd

时间:2016-03-01 21:42:28

标签: c++ encryption crypto++

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

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异常:BufferedTransformation:这个对象不允许输入

1 个答案:

答案 0 :(得分:1)

  

如果我调用MessageEnd(),我会得到Follwing异常: BufferedTransformation: this object doesn't allow input ......

正确。 FileSource是来源,消息必须存在。您无法在来源上调用PutPut2来向邮件添加其他数据。

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

<强> 第一

致电Flush上的Source

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::FlushFlush评论。

<强> 第二

存储指向过滤器的指针并在其上调用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将在结束时超出范围时将其删除。

  

...我的解密文件短16个字节......

在我看来,如果Flush上的Source调用不适合你,那么这就是你应该采取的问题。

另请注意...... AuthenticatedEncryptionFilter 的输出是2元组 {ciphertext,mac} ,因此您可以获得16字节的密文扩展。稍后,当您使用 AuthenticatedDecryptionFilter 时,mac会在验证后删除。因此,恢复的文本应该与纯文本的大小相同,两者都应该比密文少16个字节。

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