Search code examples
c++qtcryptographybotan

Error encrypting files with Botan and Qt


I'am trying to use "Botan" to encrypt and decrypt files (AES 256). Integrating the library in Qt was done successfully. I followed many examples I found on the Internet like this tutorial but I got the following

error: 
class Botan::S2K' has no member named 'set_iterations'

I figured out that the Botan version for which the tutorial was created is outdated and that the version I'am using (1.10.5) is incompatible.

My question is:
Where can I find the tutorial for the new version? If none exists, where can I download the windows installer of the previous version (1.8 or 1.9)?

Here is my code so far: (Encryption)

string file = "...";
string fileEncrypted = "...";

Botan::LibraryInitializer init;

string passphrase = "password";
AutoSeeded_RNG rng;
S2K* s2k = get_s2k("PBKDF2(SHA-256)");
s2k->set_iterations(4049);

SecureVector<byte> key_and_IV = s2k->derive_key(48, passphrase).bits_of();
SymmetricKey key(key_and_IV, 32);
InitializationVector iv(key_and_IV +32, 16);

std::ifstream in(file, std::ios::binary);
std::ofstream out(fileEncrypted, std::ios::binary);

Pipe pipe(get_cipher("AES-256/CBC", key, iv,ENCRYPTION),new DataSink_Stream(out));
pipe.start_msg();
in >> pipe;
pipe.end_msg();

Solution

  • You can obtain version 1.9 from here, but I am afraid you have two issues with the usage of the new version:

    • get_s2k() is getting obsolete, and you should use get_pbkdf() instead.

    • Instead of setting the iteration with the mutator method, you could pass the number of iterations to the derive_key in the version when using PBKDF instead of the deprecated S2k.

    See the encrypt2 example of theirs for instance:

    ...
    PKCS5_PBKDF2 pbkdf2(new HMAC(new SHA_160));
    
    const u32bit PBKDF2_ITERATIONS = 8192;
    
    SecureVector<byte> salt(8);
    rng.randomize(&salt[0], salt.size());
    
    SecureVector<byte> master_key = pbkdf2.derive_key(48, passphrase,
                                                     &salt[0], salt.size(),
                                                     PBKDF2_ITERATIONS).bits_of()
    ...
    

    You could check out further examples for details in the doc/examples folder once you fetch their release and unpack it.