Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am creating a wrapper class in MFC to encrypt and decrypt CString object in MFC using blowfish(Ref: )
/a>[
^]

My code is as follows.
C++
CString MyClass::Encryption(CString strPlainText)

{

int nLen = strPlainText.GetLength() + 1;

	if(strPlainText.IsEmpty())
	return CString();

	CString strCipher;

	char* cPlain = new char[nLen];
	char* cPlain2 = new char[nLen];
	char* cCipher = new char[nLen];
	char* cResult = new char[nLen];
	
	memset(cPlain, 0, nLen); //fill with 0s
  memset(cPlain2, 0, nLen); // fill with 0s.
   memset(cCipher, 0, nLen); // fill with 0s.
   memset(cResult, 0, nLen); // fill with 0s.

   char szHex[17];
	szHex[16] = 0;
	unsigned char aucCipherText[8];

	int i= 0;

	for(i = 0; i<nLen-1; i++)
	{
		cPlain2[i] = (char) strPlainText.GetAt(i);		
	}
	cPlain2[nLen-1] = '\0';

	strcpy(cPlain, cPlain2);//copy to datain,

	ofstream out("out.txt",ios::trunc);

	CBlowFish oBlowFish((unsigned char*)"1234567890123456", 16);
  oBlowFish.Encrypt((unsigned char*)cPlain, 
	  (unsigned char*)aucCipherText, 8, CBlowFish::ECB);

  out<<"Encrypt  :"<<cPlain << "  :To :" << aucCipherText<<endl;

  	CharStr2HexStr(aucCipherText, szHex, 8);
		out << "Encrypted hex:" << szHex << endl;

		CString str(szHex,17);

		return str;

}


This Works and i Get a result like: 895F8DB1D73F9415 .

Now i have another function to decryt my result back to original string, and thats where i have a litle bit of a problem.
What could be wrong ? Am only getting 8 characters decrypted.

C++
CString CMyClass::Decrypt(CString strCipher,  int nFinalLen)
{
	char szHex[17];
	ofstream out("out2.txt", ios::trunc);

	for(int i=0; i<strCipher.GetLength() && i<17; i++)
		szHex[i] = (char)strCipher.GetAt(i);

	out<<"Decrypting : hex, " << szHex<<endl;

	unsigned char aucCipherText[8];

	HexStr2CharStr(szHex, aucCipherText,8);
	
	char* sz = new char[nFinalLen];
	memset(sz,0, nFinalLen);

		out<<"Convert hex to char: "<<aucCipherText<<endl;	
//This gives me the original character format,

		//const char plain[8];
		CBlowFish oBlowFish((unsigned char*)"1234567890123456", 16);
		oBlowFish.Decrypt(aucCipherText,(unsigned char*) sz,8);
		out<<"Decrypted text: "<<sz<<endl;

		
		CString str(sz, nFinalLen);

		delete [] sz;

		return str;

///////////////////////////////////////////////////////////
//Bearing in mind this works.

unsigned char aucKey[8];
	unsigned char aucPlainText[12];
	char szHex[17];
	szHex[16] = 0;
	unsigned char aucCipherText[8];
	ofstream out("out.txt", ios::trunc);
	
		
		
	
		 char sz[12] = "Mynameiskin";
		strcpy((char*) aucPlainText, (const char*) sz);
		
			out << "Pain text :" << aucPlainText <<endl;


		CBlowFish oBlowFish("09090909", 8);
		oBlowFish.Encrypt(aucPlainText, aucCipherText, 8);
		out << "Encrypted text:" << aucCipherText <<endl;

		CharStr2HexStr(aucCipherText, szHex, 8);
		out << "Encrypted hex:" << szHex << endl;

		out<<"Decrypting NOW: "<<endl;	
		
		HexStr2CharStr(szHex, aucCipherText,8);
		out<<"Convert hex to char: "<<aucCipherText<<endl;	
		//const char plain[8];
		oBlowFish.Decrypt(aucCipherText,(unsigned char*) sz,8);
		out<<"Decrypted text: "<<sz<<endl;


My problem is that my decrypt function only decrypts 8 characters.
The nFinalLen argument has the size of the original string (expected plain text).
Thank you in advance
Posted
Comments
SoMad 28-Jan-13 0:49am    
I am pretty tired right now, so please forgive me if I am completely wrong about this. You might not be using the same CBlowFish class that I am, but if you are, you have to pass the correct length of the buffer to the Encrypt() and Decrypt() functions. You have hardcoded 8 as the length in all of those calls.

Soren Madsen
Garth J Lancaster 28-Jan-13 0:53am    
ya beat me to it Soren - nice work :-)
SoMad 28-Jan-13 0:55am    
Sorry :). If you want to post this in a more detailed answer, go ahead. I am off to bed in a few minutes.

Soren Madsen
Garth J Lancaster 28-Jan-13 0:53am    
Why are you using 8 (?bytes) as the length for the Encrypt and 8 for the length for the Decrypt ? - it seems that you need to examine George's API a bit better to determine the meaning of the parameters - what IS the length of the string you are passing in ?

1 solution

Seems that you call Encrypt for first 8 bytes of your string only, so as a result only 8 chars are encrypted:
C++
oBlowFish.Encrypt((unsigned char*)cPlain, 
	  (unsigned char*)aucCipherText, 8, CBlowFish::ECB);

As a result you get 895F8DB1D73F9415 in HEX 16 characters, which is ok 2 chars per value.
Then you decrypt the same value with Decrypt and again tell that you have only 8 characters in your buffer:
C++
oBlowFish.Decrypt(aucCipherText,(unsigned char*) sz,8);

Try to split your string into blocks of 8 characters and then apply encoding to blocks, or apply encoding to whole string
 
Share this answer
 

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