I want to generate RSA Public and private to char*

11 views
Skip to first unread message

Cam Vi Luong

unread,
May 9, 2016, 12:35:10 AM5/9/16
to Crypto++ Users
I have seen example of Cryptopp, there is GenerateRSAKey function which generate a public and private key to file, but I want to generate it to memory. Thus I have change the source code from 

HexEncoder privFile(new FileSink(privFilename));
HexEncoder pubFile(new FileSink(pubFilename));
--->
HexEncoder privString(new StringSink(privResult));
HexEncoder pubString(new StringSink(pubResult));

Is there right? 

Then I use this pair of key to encrypt and decrypt a string but it fatal error 
My encrypt and decrypt code:

char* CRSAService::EncryptString(const char *pszPublicKey, char *pszSeeds, char *pszMessage)
{
char *pszResult;
RandomPool randPool;
randPool.IncorporateEntropy((byte *)pszSeeds, strlen(pszSeeds));

StringSource pubString(pszPublicKey, true, new HexDecoder);
RSAES_OAEP_SHA_Encryptor pub(pubString);

string szResult;

StringSource(pszMessage, true, new PK_EncryptorFilter(randPool, pub, new HexEncoder(new StringSink(szResult))));

pszResult = new char[szResult.size() + 1];
copy(szResult.begin(), szResult.end(), pszResult);
pszResult[szResult.size()] = '\0';

return pszResult;
}

char* CRSAService::DecryptString(const char *pszPrivateKey, char *pszCipherText)
{
char *pszResult;
StringSource privString(pszPrivateKey, true, new HexDecoder);
RSAES_OAEP_SHA_Decryptor priv(privString);

string szResult;
StringSource(pszCipherText, true, new HexDecoder(new PK_DecryptorFilter(GlobalRNG(), priv, new StringSink(szResult))));

pszResult = new char[szResult.size() + 1];
copy(szResult.begin(), szResult.end(), pszResult);
pszResult[szResult.size()] = '\0';

return pszResult;
}

Is there something wrong?

Cam Vi
Reply all
Reply to author
Forward
0 new messages