Click here to Skip to main content
13,258,479 members (55,070 online)
Rate this:
Please Sign up or sign in to vote.
See more:

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:
    const char * publicKey;
    publicKey = rsa2.exportPublicKey();
    return publicKey;
    const char * privateKey;
    privateKey = rsa2.exportPrivateKey();
    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".
	//  We'll encrypt with the public key and decrypt with the private
	//  key.  It's also possible to do the reverse.
	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:
	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 17-Feb-13 4:59am
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
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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This is just a guess, but in EncryptString() you could change


and in DecryptString() you could change


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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171114.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100