RSA Strings

30 views
Skip to first unread message

Atton Mathews

unread,
Jul 8, 2015, 2:09:58 AM7/8/15
to cryptop...@googlegroups.com
I must ask how could it be possible to create RSA keys then write them to file, then read them as a string and use them in the following code.

void rsaEncode()
{
   
////////////////////////////////////////////////
   
// Generate keys
   
AutoSeededRandomPool rng;

   
InvertibleRSAFunction params;
   
params.GenerateRandomWithKeySize( rng, 1536 );

    RSA
::PrivateKey privateKey( params );
    RSA
::PublicKey publicKey( params );

   
string plain="RSA Encryption", cipher, recovered;

   
////////////////////////////////////////////////
   
// Encryption
    RSAES_OAEP_SHA_Encryptor e
( publicKey );

   
StringSource ss1( plain, true,
       
new PK_EncryptorFilter( rng, e,
           
new StringSink( cipher )
       
) // PK_EncryptorFilter
     
); // StringSource
    cout
<< cipher << endl;
   
////////////////////////////////////////////////
   
// Decryption
    RSAES_OAEP_SHA_Decryptor d
( privateKey );

   
StringSource ss2( cipher, true,
       
new PK_DecryptorFilter( rng, d,
           
new StringSink( recovered )
       
) // PK_DecryptorFilter
     
); // StringSource

   
assert( plain == recovered );
    cout
<< recovered << endl;
    system
("pause");
}



Jeffrey Walton

unread,
Jul 8, 2015, 3:42:32 AM7/8/15
to cryptop...@googlegroups.com

On Wednesday, July 8, 2015 at 2:09:58 AM UTC-4, Atton Mathews wrote:
I must ask how could it be possible to create RSA keys then write them to file, then read them as a string and use them in the following code...

See http://www.cryptopp.com/wiki/Keys_and_Formats and then come back with questions :)

The short answer is to do a privateKey.Load(...) and privateKey.Save(...). The page above will help you fill in the blanks for the ellipses.

*If* you want to save them in PEM format (as opposed to ASN.1/DER), see http://www.cryptopp.com/wiki/PEM_Pack).

Jeff

Atton Mathews

unread,
Jul 14, 2015, 12:56:02 AM7/14/15
to cryptop...@googlegroups.com
I've seen what you are talking about, but I am talking about RSA that works like the following AES.

void EEencode (string data,string Skey)
{
    byte key[16];
    byte iv[16];
    string bin;
    string theKey(Skey);
    int i(0);
    theKey.append("t;rke;tlrke65409654ytr");

    while(i != 16)
    {
        key[i] = theKey[i];
        iv[i] = theKey[i];
        i++;
    }
    // Encryptor
    CryptoPP::ECB_Mode< CryptoPP::AES >::Encryption
    //Encryptor( key, sizeof(key), iv );
    Encryptor( key, sizeof(key));
 
  // Encryption
  CryptoPP::StringSource( data, true,
    new CryptoPP::StreamTransformationFilter( Encryptor,
      new CryptoPP::StringSink( MTbuffer )
    ) // StreamTransformationFilter
  ); // StringSource

}


Jean-Pierre Münch

unread,
Jul 15, 2015, 4:48:07 PM7/15/15
to cryptop...@googlegroups.com
One reasons I see why this should fail is that you defined Encryptor to be AES-ECB encryption which needs padding mechanisms to handle arbitrary data amounts. I'd suggest you to give CTR_Mode<> a try (with IV)

BR

JPM
--
--
You received this message because you are subscribed to the "Crypto++ Users" Google Group.
To unsubscribe, send an email to cryptopp-user...@googlegroups.com.
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 cryptopp-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages