Search code examples
c++blowfishcrypto++

Passing a key in a SecByteBlock to an algorithm?


I am currently working on a Cryptopp encryption project and would need to pass a key to a Blowfish encryption algorithm.

If I understand correctly I would need to edit these 2 lines:

SecByteBlock key(Blowfish::DEFAULT_KEYLENGTH);
prng.GenerateBlock( key, key.size() );

My idea would be to pass a string variable (like ekey) here to a SecByteBlock, like so:

SecByteBlock key(ekey.data(), ekey.size());

But It looks like I'm going the wrong way. So, how do I pass a key to the algorithm?


Solution

  • The various Crypto++ objects in this area (block/stream ciphers and modes) do not take a SecByteBlock. They all take a const byte* and size_t. It comes from SymmetricCipher interface.


    SecByteBlock key(ekey.data(), ekey.size());

    Since you have an ekey with a data and size, you should probably use it directly:

    CBC_Mode< Blowfish >::Encryption enc;
    enc.SetKeyWithIV(ekey.data(), ekey.size(), iv.data(), iv.size());
    

    If you only have a SecByteBlock, then something like:

    SecByteBlock key(...);
    CBC_Mode< Blowfish >::Encryption enc;
    enc.SetKeyWithIV(key.data(), key.size(), iv.data(), iv.size());
    

    There's a wiki page on Blowfish at Crypto++ - Blowfish.

    CBC mode in the example above only provides confidentiality. You should also consider an Authenticated Encryption mode to provide confidentiality and authenticity assurances.