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); memset(cPlain2, 0, nLen); memset(cCipher, 0, nLen); memset(cResult, 0, nLen);
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);
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;
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;
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;
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