1

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?

Community
  • 1
  • 1
KurinchiMalar
  • 598
  • 3
  • 12
  • 28
  • Simply memcpy your string into the key byte array. The key must be 16 bytes so you need to pad your "myKey" – mkaes Nov 17 '15 at 09:39
  • I did ` std::string keyraw = "0123456789012345"; memcpy(key, keyraw,16);` i am getting ** no known conversion for argument 1 from âstd::string {aka std::basic_string}â to âconst byte* ** – KurinchiMalar Nov 17 '15 at 09:53
  • Ok, then you are ready to go. Either use the `ProcessString` method of the `aesEncryption` object or create a StreamTransformation like in your example. – mkaes Nov 17 '15 at 09:57
  • Possible duplicate of [How to convert SecByteBlock to string?](http://stackoverflow.com/q/31929531) and [string to SecByteBlock conversion](http://stackoverflow.com/q/26145776). – jww Nov 17 '15 at 15:40
  • 1
    @KurinchiMalar - Also see [Advanced Encryption Standard](https://cryptopp.com/wiki/Advanced_Encryption_Standard) on the Crypto++ wiki. – jww Nov 18 '15 at 02:39
  • Now available on the Crypto++ wiki: [`SecBlock`](https://www.cryptopp.com/wiki/SecBlock). The wiki page includes conversion examples. – jww Mar 23 '18 at 19:21

3 Answers3

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);
Marko Popovic
  • 3,999
  • 3
  • 22
  • 37
  • 3
    I usually don't downvote, but I think this answer warrants it. The answer leaves a lot to be desired; especially that part about padding with asterisks. Can you cite something that recommends that method of key stretching? Also, you usually don't lop-off extra bits. You extract the entropy and you use them. – jww Nov 18 '15 at 02:31
  • i must say that you made my day, thank you. – binarytrails Jun 21 '21 at 19:44
1

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;

}
Anagnostou John
  • 498
  • 5
  • 14
0

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;
}
Comet
  • 260
  • 2
  • 8