Click here to Skip to main content
15,886,032 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,,,

I'm using C++ Builder XE3, to build an application that encrypts a mobile number and decrypts it using RSA encryption/decryption (from Chilkat Software). The problem is that when i divided the method into two functions (one for encryption, and one for decryption), the Encryption function works just fine, but when using the Decrypt funtion i get no results and sometimes i get rubbish.
The following function generates the public and private key, and pass them to the Encrypt and Decrypt function:

<pre lang="cs">String Generate_public_private_keys(bool fg1)
  {
    CkRsa rsa2;

    bool success;
    success = rsa2.UnlockComponent("************")
    if (success != true) {
        ShowMessage(rsa2.lastErrorText()); }

    //  Generate a 1024-bit key.  Chilkat RSA supports
    //  key sizes ranging from 512 bits to 4096 bits.
    success = rsa2.GenerateKey(1024);
    if (success != true) {
        ShowMessage(rsa2.lastErrorText()); }

    //  Keys are exported in XML format:
    if(fg1==true)
    {
    const char * publicKey;
    publicKey = rsa2.exportPublicKey();
    ShowMessage(publicKey);
    return publicKey;
    }
    else
    {
    const char * privateKey;
    privateKey = rsa2.exportPrivateKey();
    ShowMessage(privateKey);
    return privateKey;
    }
    }


The Encrypt function is passed the mobile number to be encrypted and the public key as follows:

String EncryptString(String mn1, String pk)
{
	CkRsa rsa;


	//  This example also generates the public and private
	//  keys to be used in the RSA encryption.
	//  Normally, you would generate a key pair once,
	//  and distribute the public key to your partner.
	//  Anything encrypted with the public key can be
	//  decrypted with the private key.  The reverse is
	//  also true: anything encrypted using the private
	//  key can be decrypted using the public key.


	//  Start with a new RSA object to demonstrate that all we
	//  need are the keys previously exported:
	CkRsa rsaEncryptor;

	//  Encrypted output is always binary.  In this case, we want
	//  to encode the encrypted bytes in a printable string.
	//  Our choices are "hex", "base64", "url", "quoted-printable".
	 rsa.put_EncodingMode("hex");

	//  We'll encrypt with the public key and decrypt with the private
	//  key.  It's also possible to do the reverse.
	flag=rsaEncryptor.ImportPublicKey(AnsiString(pk).c_str());

	bool usePrivateKey;
	usePrivateKey = false;
	const char * encryptedStr;
	encryptedStr = rsaEncryptor.encryptStringENC((AnsiString(mn1).c_str()),usePrivateKey);

	return encryptedStr;
   }



While the Decrypt function is passed the encrypted mobile number and the private key
String DecryptString(String enc, String pvk)
 {
		CkRsa rsa1;

	//  Now decrypt:

	CkRsa rsaDecryptor;


	//  Encrypt a string and return the encrypted data base64-encoded:
	rsa1.put_EncodingMode("hex");

	flag2=rsaDecryptor.ImportPrivateKey(AnsiString(pvk).c_str());

	ShowMessage(pvk);

	bool usePrivateKey = true;
	const char * decryptedStr;

	decryptedStr = rsaDecryptor.decryptStringENC((AnsiString(enc).c_str()),usePrivateKey);


	return decryptedStr;
	}


So, i think the problem in the Decrypt function lies within the EncodingMode, because the decryptedStr cannot display it's value,eventhough it has one.
Please, NEED HELP :)
Posted
Comments
Richard MacCutchan 17-Feb-13 12:06pm    
In your encryption routine you import the public key, but then specify usePrivateKey. Is that correct?
raniam 18-Feb-13 2:46am    
Hi,,,
I've tried it, but still DECRYPTION is not working :(
Richard MacCutchan 18-Feb-13 3:45am    
I cannot find any information on your CkRsa class so I cannot make any further suggestions. I also wonder why you create two objects of this class in your code.

1 solution

This is just a guess, but in EncryptString() you could change
C++
rsa.put_EncodingMode("hex");

to
C++
rsaEncryptor.put_EncodingMode("hex");

and in DecryptString() you could change
C++
rsa1.put_EncodingMode("hex");

to
C++
rsaDecryptor.put_EncodingMode("hex");


Another thing you could check is the string types you're using: do they really match the expected argument and return types of the functions you call? Maybe the character encoding doesn't match.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900