Click here to Skip to main content
14,869,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm having this problem, where I have this const char * Decrypted_Message2 that contains a certificate, browser nonce, and a Server Nonce (Separated by a delimiter ,).
I have this function that split them individually, using the delimiter "," character. And I want to retrieve the second and third items into the Array.
At first it works fine and I get the browser nonce, which is called "Recovered_Browser_Nonce_from_Message2 ". But then when I call the split function again to get the server nonce "Recovered_Server_Nonce_from_Message2", I find that the "Decrypted_Message" is empty!!!!!!


C++
const char * Decrypted_Message2 = Decrypt_Message2(encrypted_message2, Shared_Session_Key_C );
	cout<<"\n\nDECRYPTED MESSAGE2 {S,nb,ns}:  "<<Decrypted_Message2;



const char * Recovered_Browser_Nonce_from_Message2 = Split_Server_Message2(Decrypted_Message2, true);
	cout<<"\n\nRECOVERED_BROWSER_NONCE:"<<Recovered_Browser_Nonce_from_Message2;
	cout<<"\n\ndecrypted message2:"<<Decrypted_Message2; //HERE IS WHERE I FIND IT EMPTY

const char * Recovered_Server_Nonce_from_Message2 = Split_Server_Message2(Decrypted_Message2, false);
	 cout<<"\nRECOVERED SERVER NONCE:  "<<Recovered_Server_Nonce_from_Message2;


the following is the split function:

C++
const char * Client_Side::Split_Server_Message2(CkString Recieved_Message2_from_Server, bool flag)
{
	const char * Recovered_nb = "";
	const char * Recovered_ns = "";
	bool exceptDoubleQuoted = true;
	bool exceptEscaped = true;	// Do not treat characters preceded with a backslash as a delimiter.
	bool keepEmpty = false;	// Do not keep empty fields.
	char delimiter = ',';

	CkStringArray *array = Recieved_Message2_from_Server.split(delimiter,exceptDoubleQuoted,exceptEscaped,keepEmpty);


	if(flag == true)
	  {
	   for (int i=0; i<array->get_Count(); i++)
		{
		  array->getString(i);
		  //printf("%d: [%s]\n",i,array->getString(i));
		  Recovered_nb =  array->getString(1);  //Browser Nonce (nb)
		}
	   return Recovered_nb;
	  }


	else
	  {
		  for (int j=0; j<array->get_Count(); j++)
		{
		  array->getString(j);
		  //printf("%d: [%s]\n",j,array->getString(j));
		  Recovered_ns =  array->getString(2);     //Server Nonce (ns);
		}
		return Recovered_ns;
	  }

}


I've been struggling with this issue for two whole days now, and I cant seem to find the problem!!!!
Can someone please help me with this.

Regards.
Rania

What I have tried:

1- I've tried copying the "Decrypted_Message2" into another string, it didn't work either.
I even tried writing another "split" function, only to retrieve the third item, but it didn't work.
Posted
Updated 31-Mar-16 21:18pm

1 solution

I don't know why the string becomes empty. A probable reason may be a stack corruption but I don't see one in your code.

But I suggest to change the split code because there is no need to call the split function multiple times and - more important - it contains a memory leak.

The CkString::split function returns a CkStringArray that has been allocated using new but is never deleted in your code. See CkString::split[^]:
Quote:
Note: The application is responsible for deleting (via the C++ delete operator) the object returned by this method.

Try this code instead which does not use an additional split function (I have used short variable names here):
// Copy message to a CkString.
CkString msg;
msg.SetString(Decrypted_Message2);
// Split it.
CkStringArray *array = msg.split(',', true, true, false);
// Get pointers to items checking if they exist.
//const char *cert = array->GetCount() > 0 ? array->GetString(0) : "";
const char *nb = array->GetCount() > 1 ? array->GetString(1) : "";
const char *ns = array->GetCount() > 2 ? array->GetString(2) : "";
// Use [cert,] nb, and ns here

// Delete array when finished using it.
delete array;
   
Comments
raniam 1-Apr-16 10:28am
   
Thank you so much.
it worked.
you've been very helpful.

Best regards.
Jochen Arndt 1-Apr-16 10:41am
   
Thank you for your feedback and accepting the solution.

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