Providing this Code, the key and iv are randomly generated, this way it's not possible to create e.g. 2 Functions to decrypt and encrypt with AES. How can I set own key and iv? Or even better, how to "save" the randomly generated key's and use them for decryption afterwards?
string plain = text;
string cipher, encoded, recovered;
byte aeskey[AES::DEFAULT_KEYLENGTH];
byte iv[AES::BLOCKSIZE];
prng.GenerateBlock(aeskey, sizeof(aeskey));
prng.GenerateBlock(iv, sizeof(iv));
encoded.clear();
StringSource(aeskey, sizeof(aeskey), true, new HexEncoder(new StringSink(encoded)));
encoded.clear();
StringSource(iv, sizeof(iv), true, new HexEncoder(new StringSink(encoded)));
try
{
cout << "plain text: " << plain << endl;
CBC_Mode< AES >::Encryption Encrypt;
Encrypt.SetKeyWithIV(aeskey, sizeof(aeskey), iv);
StringSource s(plain, true, new StreamTransformationFilter(Encrypt, new StringSink(cipher)));
}
catch (const CryptoPP::Exception & Encrypt)
{
cerr << Encrypt.what() << endl;
//exit(1);
}
encoded.clear();
StringSource(cipher, true, new HexEncoder(new StringSink(encoded)));
cout << "cipher text: " << encoded << endl;
try
{
std::cout << "HI" << "\n";
CBC_Mode< AES >::Decryption Decrypt;
Decrypt.SetKeyWithIV(aeskey, sizeof(aeskey), iv);
StringSource c(cipher, true, new StreamTransformationFilter(Decrypt, new StringSink(recovered)));
cout << "recovered text: " << recovered << endl;
}
catch (const CryptoPP::Exception & e)
{
cerr << e.what() << endl;
exit(1);
}
Aucun commentaire:
Enregistrer un commentaire