Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Builder
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 Smile | :)
Posted 17-Feb-13 4:59am
raniam438
Comments
Richard MacCutchan at 17-Feb-13 12:06pm
   
In your encryption routine you import the public key, but then specify usePrivateKey. Is that correct?
raniam at 18-Feb-13 2:46am
   
Hi,,,
I've tried it, but still DECRYPTION is not working :(
Richard MacCutchan at 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
good
Please Sign up or sign in to vote.

Solution 1

This is just a guess, but in EncryptString() you could change
rsa.put_EncodingMode("hex");
to
rsaEncryptor.put_EncodingMode("hex");
and in DecryptString() you could change
rsa1.put_EncodingMode("hex");
to
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 400
1 Sergey Alexandrovich Kryukov 329
2 Afzaal Ahmad Zeeshan 264
3 BillWoodruff 245
4 CPallini 195
0 OriginalGriff 5,560
1 DamithSL 4,476
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2014
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