3

I have two simple encryption decryption functions. The functions work fine for text files and I can retrieve the file successfully. But it corrupts binary files. If I encrypt an image and then decrypt, it gets corrupted.

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

}
jww
  • 97,681
  • 90
  • 411
  • 885
adnan kamili
  • 8,967
  • 7
  • 65
  • 125

1 Answers1

3

The API says:

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

you may want to set that last argument to true.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263