3

我有两个简单的加密解密函数。这些功能适用于文本文件,我可以成功检索文件。但它会破坏二进制文件。如果我加密图像然后解密,它就会损坏。

bool Encrypter::FileEncrypter(std::string src, std::string dest)
{
    try {
        Botan::InitializationVector iv(enckey.substr(0,32));
        Botan::SymmetricKey symKey(enckey.substr(32,32));
        Botan::DataSource_Stream in(src.c_str(), true);
        Botan::Pipe enc(Botan::get_cipher("AES-128/CBC", symKey, iv, Botan::ENCRYPTION), new Botan::DataSink_Stream(dest.c_str()));
        enc.process_msg(in);
        return true;
    }
    catch(std::exception &e){
        return false;
    }

}

bool Encrypter::FileDecrypter(std::string src, std::string dest)
{
    try {
        Botan::InitializationVector iv(enckey.substr(0,32));
        Botan::SymmetricKey symKey(enckey.substr(32,32));
        Botan::DataSource_Stream in(src.c_str(), true);
        Botan::Pipe dec(Botan::get_cipher("AES-128/CBC", symKey, iv, Botan::DECRYPTION), new Botan::DataSink_Stream(dest.c_str()));
        dec.process_msg(in);
        return true;
    }
    catch(std::exception &e){
        return false;
    }

}
4

1 回答 1

3

API 说:

DataSink_Stream (const std::string &pathname, bool use_binary=false)

您可能希望将最后一个参数设置为true.

于 2016-02-24T01:16:45.090 回答