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()); }
success = rsa2.GenerateKey(1024);
if (success != true) {
ShowMessage(rsa2.lastErrorText()); }
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;
CkRsa rsaEncryptor;
rsa.put_EncodingMode("hex");
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;
CkRsa rsaDecryptor;
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 :)