'Getting Started with crypto++ decryption with AES

I'm trying to learn a bit about cryptography and am trying to use AES decryption with the crypto++ library in c++. I have a ciphertext string and a key string. Using these two, I'd like to decrypt this ciphertext. Here is my code:

#include "mycrypto.h"
#include <stdio.h>
#include <cstdlib>
#include <string>

#include <aes.h>
#include <config.h>
#include <hex.h>
#include <files.h>
#include <cryptlib.h>
#include <modes.h>
#include <osrng.h>
#include <filters.h>
#include <sha.h>
#include <rijndael.h>


using namespace std;
using namespace CryptoPP;

int main()
{

    string myPlainText;
    string myKey = "140b41";
    string myCipherText = "4ca00f";

    byte key[AES::DEFAULT_KEYLENGTH];
    byte iv[AES::BLOCKSIZE];

    CryptoPP::CBC_Mode<AES>::DECRYPTION decryptor;
    decryptor.SetKeyWithIV(key, sizeof(key), iv);

    StringSource(myCipherText, true, new StreamTransformationFilter( decryptor, new StringSink(myPlainText)));

    return 0;
}

I get a number of errors with this code. The most immediate is this one:

'DECRYPTION' is not a member of 'CryptoPP::CBC_Mode'

Can anyone straighten me out with this code. I've been all over the crypto++ documentation, but I don't see what I am doing wrong.

Thank you!



Solution 1:[1]

I think DECRYPTION is spelled Decryption. At least, that is how it appears in this example.

Solution 2:[2]

Your missing a few thing here. I understand as learning crypto++ was daunting for me.

  • It is CBC_Mode< AES >::Decryption.
  • You have no IV to decrypt with?
  • You haven't converted the hex 'myCipherText' back to (byte*).

This is my working example.

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 reuben
Solution 2