Search code examples
c++crypto++

AES Decryption only in Crypto++


I have a method based on the crypto++ examples (but with hardcoded key and iv) in which i encrypt and decrypt a string, which works fine. Now when i comment out the encryption code and set the ciphertext to the Output that i receive from the encryption part i only get nonsense instead of the string i started with. Here is my code (with the encryption part commented out):

byte iv[CryptoPP::AES::BLOCKSIZE];
byte key[CryptoPP::AES::MAX_KEYLENGTH];
std::string strKey = "F5C3DD6EA4F2AE1713F4B9B21FD0011CCECB5CA8858F313B5EA3E85CD1F6E70E";
std::string striv = "DEA43ED679566B66FD2B5126149F34A";

int x = 0;
for (unsigned int i = 0; i < strKey.length(); i += 2) {
    std::string bytestring = strKey.substr(i, 2);
    key[x] = (char)strtol(bytestring.c_str(), NULL, 16);
    x++;
}
int y = 0;
for (unsigned int i = 0; i < striv.length(); i += 2) {
    std::string bytestring = striv.substr(i, 2);
    iv[y] = (char)strtol(bytestring.c_str(), NULL, 16);
    y++;
}

std::string plain = "test mode";
std::string cipher, encoded, recovered;
cipher = "F190D36A0FEEF07C5B";

/*
//print key
encoded.clear();
CryptoPP::StringSource(key, sizeof(key), true,
    new CryptoPP::HexEncoder(new CryptoPP::StringSink(encoded)));
std::cout << "key: " << encoded << std::endl;

//print iv
encoded.clear();
CryptoPP::StringSource(iv, sizeof(iv), true,
    new CryptoPP::HexEncoder(new CryptoPP::StringSink(encoded)));
std::cout << "iv: " << encoded << std::endl;

try
{
    std::cout << "plain text: " << plain << std::endl;

    CryptoPP::CFB_Mode<CryptoPP::AES>::Encryption en;
    en.SetKeyWithIV(key, sizeof(key), iv);
    CryptoPP::StringSource(plain, true, new CryptoPP::StreamTransformationFilter(en, new CryptoPP::StringSink(cipher)));
    std::cout << "cipher: " << cipher;
}
catch (const std::exception& e)
{
    std::cout << "CryptInit Exception: " << e.what() << std::endl;
}

encoded.clear();
CryptoPP::StringSource(cipher, true,
    new CryptoPP::HexEncoder(new CryptoPP::StringSink(encoded)));
std::cout << "cipher text: " << encoded << std::endl;
//*/
try
{
    CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption de;
    de.SetKeyWithIV(key, sizeof(key), iv);

    CryptoPP::StringSource s(cipher, true,
        new CryptoPP::StreamTransformationFilter(de, new CryptoPP::StringSink(recovered)));

    std::cout << "recovered text: " << recovered << std::endl;

}
catch (const std::exception& e)
{
    std::cout << "CryptInit Exception: " << e.what() << std::endl;
}

This is the Output i receive: ├─Ö.k░®Y♫µµâƒ├v

For reproduction purposes here are the required includes:

#include <cryptopp\aes.h>
#include <cryptopp\filters.h>
#include <cryptopp\modes.h>
#include <cryptopp\hex.h>
#include <cryptopp\cryptlib.h>

Solution

  • cipher = "F190D36A0FEEF07C5B";
    

    And:

    CryptoPP::StringSource s(cipher, true,
        new CryptoPP::StreamTransformationFilter(de, new CryptoPP::StringSink(recovered)));
    

    It looks like you are trying to decrypt a hex encoded string. You probably need to run the cipher text through a HexDecoder. Maybe something like:

    StringSource s(cipher, true,
        new HexDecoder(
            new StreamTransformationFilter(de, new StringSink(recovered))));