"unknown: this object doesn't support multiple channels" 加密期间

"unknown: this object doesn't support multiple channels" during encryption

我在文件加密期间遇到 "unknown: this object doesn't support multiple channels" 异常。我可以生成会话密钥,但它不能用于加密文件。

以下是我的文件加密代码片段:

void enc_file_EAX(PAES_KEY_WITH_IV key, const char *in_file, const char *out_file)
{

    try {
        CryptoPP::EAX<CryptoPP::AES>::Encryption encryptor;
        encryptor.SetKeyWithIV(key->key, key->key.size(), key->iv);
        CryptoPP::FileSource f(in_file, true,
                     new CryptoPP::AuthenticatedEncryptionFilter(encryptor,
                        new CryptoPP::FileSink(
                             std::string(in_file).c_str()),
                             CryptoPP::AuthenticatedDecryptionFilter::THROW_EXCEPTION |
                             CryptoPP::AuthenticatedDecryptionFilter::MAC_AT_END ));
        std::fstream file(out_file, std::ios::binary | std::ios::ate);
        size_t remaining = file.tellg();
        file.close();
        size_t BLOCK_SIZE = 16384;
        while (remaining && !f.SourceExhausted()) {
            const unsigned int req = std::min(remaining, BLOCK_SIZE);
            f.Pump(req);
            f.Flush(false);
            remaining -= req;
        }

    } catch (const CryptoPP::Exception& e) {
        std::cout << e.GetWhat() << std::endl;
        return;
    }
}

有人可以帮我解决这个问题吗?这里出了什么问题?

提前致谢。

EAX<AES>::Encryption encryptor;
encryptor.SetKeyWithIV(key->key, key->key.size(), key->iv);
FileSource f(in_file, true,
        new AuthenticatedEncryptionFilter(encryptor,
            new FileSink(
                std::string(in_file).c_str()),
            AuthenticatedDecryptionFilter::THROW_EXCEPTION |
            AuthenticatedDecryptionFilter::MAC_AT_END ));

您正在同一对象管道中混合和匹配加密和解密装置。你应该先解决这个问题。

查看 AuthenticatedEncryptionFilterTHROW_EXCEPTION | MAC_AT_END 标志可能被强制转换为 putAAD 标志:

AuthenticatedEncryptionFilter(AuthenticatedSymmetricCipher &c,
    BufferedTransformation *attachment = NULL, bool putAAD=false,
    int truncatedDigestSize=-1, const std::string &macChannel=DEFAULT_CHANNEL,
    BlockPaddingScheme padding=DEFAULT_PADDING);

我记得你需要避开 Crypto++ wiki 上的 Pipelines if you want to add AAD. The pipe only allows inputting data on DEFAULT_CHANNEL, which provides AE (but no AAD). Also see EAX Mode | AEAD example


您对 FileSinkFileSource 的使用看起来不太正确。我不确定这会如何表现:

FileSource f(in_file, true, ...
    new FileSink(std::string(in_file).c_str()), ...);

您应该使用 in_file.c_str() 作为文件名:

FileSource f(in_file.c_str(), ...);

您可能还应该为接收器使用不同的文件:

FileSource f(out_file.c_str(), ...);

编辑(来自评论):

No , we don't want to add pipeline, we just want to use DEFAULT_CHANNEL for inputting data. And given example is for text input. I want to use same function for file inputting. Can you elaborate more on this?

在这种情况下,尝试:

EAX<AES>::Encryption encryptor;
encryptor.SetKeyWithIV(key->key, key->key.size(), key->iv);
FileSource f(in_file.c_str(), true,
        new AuthenticatedEncryptionFilter(encryptor,
            new FileSink(out_file.c_str()));

那应该输入机密性和完整性 (AE) 的文本。它将避免仅具有机密性的文本 (AEAD)。

另请参阅 Crypto++ wiki 上的 EAX Mode。它有 AE 和 AEAD 的三四个例子。