'Using std::string for key with AES encryption in Crypto++
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?
Solution 1:[1]
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);
Solution 2:[2]
Here is an other example using sha256 and the password cat!
#include <iomanip>
#include <iostream>
#include <cryptopp/modes.h>
#include <cryptopp/aes.h>
#include <cryptopp/filters.h>
#include <cryptopp/sha.h>
#include <cryptopp/hex.h>
int main(int argc, char* argv[]) {
CryptoPP::SHA256 hash;
CryptoPP::byte digest[CryptoPP::SHA256::DIGESTSIZE];
std::string message = "cat";
hash.CalculateDigest(digest, (CryptoPP::byte*)message.c_str(), message.length());
CryptoPP::HexEncoder encoder;
std::string sKey;
encoder.Attach(new CryptoPP::StringSink(sKey));
encoder.Put(digest, sizeof(digest));
encoder.MessageEnd();
CryptoPP::byte key[CryptoPP::AES::MAX_KEYLENGTH]; //16 Bytes MAXKEYLENGTH 32 BYTES(SHA 256)
CryptoPP::byte iv[CryptoPP::AES::BLOCKSIZE];
memcpy(key, sKey.c_str(), CryptoPP::AES::MAX_KEYLENGTH);;
memset(iv, 0x00, CryptoPP::AES::BLOCKSIZE);
//
// String and Sink setup
//
std::string plaintext = "Now is the time for all good men to come to the aide...";
std::string ciphertext;
std::string decryptedtext;
//
// Dump Plain Text
//
std::cout << "Plain Text (" << plaintext.size() << " bytes)" << std::endl;
std::cout << plaintext;
std::cout << std::endl << std::endl;
//
// Create Cipher Text
//
CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::MAX_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv);
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink(ciphertext));
stfEncryptor.Put(reinterpret_cast<const unsigned char*>(plaintext.c_str()), plaintext.length());
stfEncryptor.MessageEnd();
//
// Dump Cipher Text
//
std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;
for (int i = 0; i < ciphertext.size(); i++) {
std::cout << "0x" << std::hex << (0xFF & static_cast<CryptoPP::byte>(ciphertext[i])) << " ";
}
std::cout << std::endl << std::endl;
//
// Decrypt
//
CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::MAX_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv);
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decryptedtext));
stfDecryptor.Put(reinterpret_cast<const unsigned char*>(ciphertext.c_str()), ciphertext.size());
stfDecryptor.MessageEnd();
//
// Dump Decrypted Text
//
std::cout << "Decrypted Text: " << std::endl;
std::cout << decryptedtext;
std::cout << std::endl << std::endl;
return 0;
}
Solution 3:[3]
Functions:
encc - Encrypts plain text and returns "ivString::ciphertxt".
decc - Disassembles the IV and ciphertext and Decrypts 'ciphertext'.
string encc(string plain) {
using namespace CryptoPP;
AutoSeededRandomPool prng;
SecByteBlock iv(AES::BLOCKSIZE);
//the password
std::string sKey = "UltraSecretKeyPhrase";
// Convert "UltraSecretKeyPhrase" to SecByteBlock
SecByteBlock key((const unsigned char*)(sKey.data()), sKey.size());
// Generate IV
prng.GenerateBlock(iv, iv.size());
std::string cipher, recovered;
//Try Encrypt
try
{
CBC_Mode< AES >::Encryption e;
e.SetKeyWithIV(key, key.size(), iv);
StringSource s(plain, true,
new StreamTransformationFilter(e,
new StringSink(cipher)
)
);
}
catch (const Exception& e)
{
exit(1);
}
string ciphertxt, ivString;
//HexEncode IV
HexEncoder encoder(new FileSink(std::cout));
encoder.Detach(new StringSink(ivString));
encoder.Put(iv, iv.size());
encoder.MessageEnd();
//HexEncode ciphertxt
encoder.Detach(new StringSink(ciphertxt));
encoder.Put((const byte*)&cipher[0], cipher.size());
encoder.MessageEnd();
string toSend = ivString + "::" + ciphertxt;
return toSend;
}
string decc(string toDec) {
using namespace CryptoPP;
std::string sKey = "UltraSecretKeyPhrase";
SecByteBlock key((const unsigned char*)(sKey.data()), sKey.size());
std::string recovered;
string str1 = "::";
size_t found = toDec.find(str1);
//seperate iv and ciphertxt
if (found != string::npos) {
std::string sIv = toDec.substr(0, found);
std::string encMessageHex = toDec.substr(found + 2);
cout << endl << "IV: " << sIv << endl << "Encoded Msg: " << encMessageHex << endl;
string iv, encMessage;
HexDecoder decoder, msgDecoder;
//Decode the IV Hex back to byte*
decoder.Attach(new StringSink(iv));
decoder.Put((byte*)sIv.data(), sIv.size());
decoder.MessageEnd();
//Decode the ciphertxt Hex back to byte*
decoder.Attach(new StringSink(encMessage));
decoder.Put((byte*)encMessageHex.data(), encMessageHex.size());
decoder.MessageEnd();
//Try decoding the ciphertxt
try
{
CBC_Mode< AES >::Decryption d;
d.SetKeyWithIV(key.data(), key.size(), (byte *)iv.data(), AES::BLOCKSIZE);
StringSource s(encMessage, true,
new StreamTransformationFilter(d,
new StringSink(recovered)
)
);
return recovered;
}
catch (const Exception& e)
{
std::cerr << e.what() << std::endl;
exit(1);
}
}
else return NULL;
}
int main(){
string hh = encc("this is encoded");
cout << hh << endl;
string gg = decc(hh);
cout << gg << endl;
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Marko Popovic |
Solution 2 | Anagnostou John |
Solution 3 | Comet |