Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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.
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.
 
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 27-Jan-13 18:15pm
Comments
SoMad at 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 at 28-Jan-13 0:53am
   
ya beat me to it Soren - nice work :-)
SoMad at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Seems that you call Encrypt for first 8 bytes of your string only, so as a result only 8 chars are encrypted:
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:
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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 487
1 CPallini 345
2 OriginalGriff 240
3 George Jonsson 159
4 Abdul Samad KP 145
0 OriginalGriff 6,329
1 Sergey Alexandrovich Kryukov 5,700
2 CPallini 4,940
3 George Jonsson 3,469
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 28 Jan 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