I am using Example of AES using Crypto++. I want to encrypt with this key:
std::string key = "mykey";
Allocate memory for key
byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );
Do Encrypt:
CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv );
How do I pass a key of std::string type to the aesEncryption function?
Here is the solution:
std::string sKey = "mykey";
if(CryptoPP::AES::DEFAULT_KEYLENGTH < sKey.size())
sKey = sKey.substr(0, CryptoPP::AES::DEFAULT_KEYLENGTH); // chop if too long
else if(CryptoPP::AES::DEFAULT_KEYLENGTH > sKey.size())
sKey += std::string(CryptoPP::AES::DEFAULT_KEYLENGTH - sKey.size(), '*'); // pad
memcpy(key, sKey.c_str(), CryptoPP::AES::DEFAULT_KEYLENGTH);