Hi,
I'm performing an AES encryption/decryption process (using the Chilkat AES encryption functions).
The encryption result is performed smoothly and being sent from the client to the server.
The problem is with the decryption function, every time the function tries to decrypt the received encrypted-result, it returns garbage!!!!
Although I've made sure that the required parameters (used within the encryption function) are correct, such as the initialization vector, shared secret key, and even the properties are the same.
NOTE: the initialization vector is being appended (in clear text) to the encrypted message and sent to the server. Once it reaches the server-side,it is split from the encrypted message before the following decryption function executes. Thus, it is identical to the one used in encryption.
the following is the encryption function, which is working properly:
const char * Client_Side::AES_Encrypt(const char * msg1, const char * C_sessionKey, const char * ivector)
{
CkCrypt2 crypt;
bool success = crypt.UnlockComponent("****************");
if (success != true) {
std::cout << crypt.lastErrorText() << "\r\n";
}
crypt.put_CryptAlgorithm("aes");
crypt.put_KeyLength(128);
crypt.put_CipherMode("cbc");
crypt.SetEncodedKey(C_sessionKey,"base64");
crypt.SetEncodedIV(ivector,"base64");
crypt.put_EncodingMode("base64");
const char *cipherText64 = 0;
cipherText64 = crypt.encryptStringENC(msg1);
std::cout <<"\n\nENCRYPTED MESSAGE1: "<< cipherText64 << "\r\n";
return cipherText64;}
the following is the Decryption code, which returns garbage:
const char * Server_Side::AES_Decrypt(const char * Enc_Msg1, const char * SrvSessionKey
,const char * iv)
{
CkCrypt2 decrypt;
bool success = decrypt.UnlockComponent("******************");
if (success != true) {
std::cout << decrypt.lastErrorText() << "\r\n"; }
decrypt.put_CryptAlgorithm("aes");
decrypt.put_KeyLength(128);
decrypt.put_CipherMode("cbc");
decrypt.SetEncodedKey(SrvSessionKey,"base64");
decrypt.SetEncodedIV(iv,"base64");
decrypt.put_EncodingMode("base64");
std::cout<<"\nKEY: "<<SrvSessionKey;
std::cout<<"\nIV: "<< iv;
std::cout<<"\nMESSAGE TO BE DECRYPTED:"<<"\r\n"<<Enc_Msg1;
const char * Decrypted_Message1 = 0;
Decrypted_Message1 = decrypt.decryptStringENC(Enc_Msg1);
std::cout <<"\n\nDecrypted_Message1:"<<"\r\n"<<Decrypted_Message1;
std::cout <<"\n\nLength"<< strlen(Decrypted_Message1);
return Decrypted_Message1;
}
Please, I need help with this.
What I have tried:
1- I've tried putting the decrypt function in the main, still didn't work.
2- made sure that the initialization vector, and the shared secret key are identical.
3- changed the encoding mode in both functions, did not work.
4- triedd generating the shared secret key within the same decrypt function, did not work.