Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encryption and decryption corrupting binary files

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

}
like image 843
adnan kamili Avatar asked Dec 08 '25 06:12

adnan kamili


1 Answers

The API says:

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

you may want to set that last argument to true.

like image 172
Maarten Bodewes Avatar answered Dec 09 '25 18:12

Maarten Bodewes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!