Hi,

I think I got the solution. I now use ArraySource to read the 'key'
generated for the AES algorithm. 
Can anyone check whether this is correct? 

Code:
/************************************************/
int main (int argc, char* argv[])
{
        // Pseudo Random Number Generator
        AutoSeededRandomPool rng;

        // Generate session key for AES
        byte key[AES::DEFAULT_KEYLENGTH];
        rng.GenerateBlock(key, AES::DEFAULT_KEYLENGTH);

        // Generate random IV for AES
        byte iv[AES::BLOCKSIZE];
        rng.GenerateBlock(iv, AES::BLOCKSIZE);

        // Define privateKey & publicKey as RSA asymmetric keys
        RSA::PrivateKey privateKey;
        RSA::PublicKey publicKey;

        //Load pregenerated RSA Keys
        privateKey.Load( FileSource ("private.key", true ).Ref());
        publicKey.Load( FileSource ("public.key", true).Ref());

        // Prepare key for encryption   
        string hexKey, cipherKey;
        
        ArraySource     (key, sizeof(key), true, 
                new HexEncoder (
                        new StringSink (hexKey)
                        )
                );
        
        // Save hexKey to 'hexkey.txt'
        StringSource (hexKey, true, new FileSink("hexkey.txt"));

        // Encrypt Session Key
        RSAES_OAEP_SHA_Encryptor encrypt (publicKey);

        StringSource(hexKey, true, 
                new PK_EncryptorFilter (rng, encrypt,
                        new StringSink (cipherKey)
                        )
                );

        return 0;
}
/************************************************/

Thank you,
fboyixiz


fboyixiz wrote:
> 
> Hi,
> 
> I know this sounds silly but I have a hard time trying to encrypt the AES
> session key using RSA.
> I know that the is key is in byte, can I still use StringSource to read
> the key and encrypt using PK_EncryptorFilter?
> 
> Below is my coding:
> 
> int main (int argc, char* argv[])
> {
>       // Pseudo Random Number Generator
>       AutoSeededRandomPool rng;
> 
>       // Generate session key for AES
>       byte key[AES::DEFAULT_KEYLENGTH];
>       rng.GenerateBlock(key, AES::DEFAULT_KEYLENGTH);
> 
>       // Generate random IV for AES
>       byte iv[AES::BLOCKSIZE];
>       rng.GenerateBlock(iv, AES::BLOCKSIZE);
> 
>       // Define privateKey & publicKey as RSA asymmetric keys
>       RSA::PrivateKey privateKey;
>       RSA::PublicKey publicKey;
> 
>       //Load pregenerated RSA Keys
>       privateKey.Load( FileSource ("private.key", true ).Ref());
>       publicKey.Load( FileSource ("public.key", true).Ref());
> 
> /*****************************************************/
>       // Encrypt Session Key
>       RSAES_OAEP_SHA_Encryptor encrypt (publicKey);
>       
>       string cipherKey,cipherIV, hexKey;
>       
>       StringSource (key, new HexEncoder (new StringSink (hexKey)) );
>       cout << "Key: " << hexKey;
> 
>       StringSource (hexKey, true, new PK_EncryptorFilter (rng, encrypt,
>               new StringSink (cipherKey)));
> 
> /******************************************************/
>       return 0;
> }
> 
> Thanks you,
> fboyixiz
> 

-- 
View this message in context: 
http://old.nabble.com/How-to-encrypt-AES-session-key-using-RSA--tp27424547p27425658.html
Sent from the Crypto++ Users mailing list archive at Nabble.com.

-- 
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.

Reply via email to