Personally I prefer the encryption output to go to a string to give you 
more control over it...for example you may want to encrypt something into a 
json object for sending over the internet rather than writing directly to a 
file.  This is what I am doing:

std::string encrypt_rsa_aes( const std::string &plaintext, const 
rsa_public_key &rsa_key, CryptoPP::AutoSeededRandomPool &rng )
    {
        using namespace CryptoPP;

        // Generate a random AES key
        SecByteBlock key(AES::DEFAULT_KEYLENGTH);
        rng.GenerateBlock( key, key.size() );

        // Generate a random IV
        SecByteBlock iv( AES::BLOCKSIZE );
        rng.GenerateBlock(iv, AES::BLOCKSIZE);

        //encrypt with AES..
        SecByteBlock encrypted( plaintext.length() );
        CFB_Mode<AES>::Encryption cfbEncryption(key, key.size(), iv);
        cfbEncryption.ProcessData( encrypted, (byte*) plaintext.c_str(), 
plaintext.length() );

        //now encrypt the AES key with RSA...
        SecByteBlock encrypted_aes_key = rsa_key.encrypt( key, rng );

        //combine all together into one buffer...
        size_t total_size = iv.size() + encrypted_aes_key.size() + 
encrypted.size();
        SecByteBlock combined( total_size );
        byte *iv_offset = combined, *key_offset = iv_offset + iv.size(), 
*cipher_offset = key_offset + encrypted_aes_key.size();
        memcpy( iv_offset, iv, iv.size() );
        memcpy( key_offset, encrypted_aes_key, encrypted_aes_key.size() );
        memcpy( cipher_offset, encrypted, encrypted.size() );

        //base64 encode the combination...
        return base64_encode( combined );

    }

rsa_public_key is a wrapper class that I made for RSA::PublicKey that uses 
RSAES_OAEP_SHA_Encryptor

-- 
-- 
You received this message because you are subscribed to the "Crypto++ Users" 
Google Group.
To unsubscribe, send an email to [email protected].
More information about Crypto++ and this group is available at 
http://www.cryptopp.com.
--- 
You received this message because you are subscribed to the Google Groups 
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to