1

I am using crypto++ AES to encrypt and decrypt.

I have the key stored as string type in database for some reason.

How do I assign this key to the AES SecByteBlock key or initialize the AES key with this value instead of some random generated blocks.

std::string key_fromDB = "12dfre314?//1afsfa";
AutoSeededRandomPool prng;
byte iv[ AES::BLOCKSIZE ];
prng.GenerateBlock( iv, sizeof(iv) );
SecByteBlock temp_key(AES::DEFAULT_KEYLENGTH);
prng.GenerateBlock( temp_key, temp_key.size() );

try 
{
    CBC_Mode< AES >::Decryption d;
    d.SetKeyWithIV(temp_key , temp_key.size(), iv );

    // The StreamTransformationFilter removes
    //  padding as required.
    StringSource ss( cipher, true,
        new StreamTransformationFilter( d,
            new StringSink( recovered )
        ) // StreamTransformationFilter
    ); // StringSource

    std::cout << "recovered text: " << recovered << std::endl;
}
catch ( const CryptoPP::Exception& e)
{
    std::cerr << e.what() << std::endl;
}
jww
  • 97,681
  • 90
  • 411
  • 885
GoQuestion
  • 71
  • 2
  • 9
  • Possible duplicate of [std::string to SecByteBlock conversion](https://stackoverflow.com/q/26145776/608639). Also see [How to convert SecByteBlock to string?](https://stackoverflow.com/q/31929531/608639) – jww Apr 17 '19 at 21:27
  • Thanks! That should work! – GoQuestion Apr 17 '19 at 23:55

0 Answers0