Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Dear all:

I'd like to encryption & decryption a password for CString on MFC, having searched the problem on google; However, I can't find a solution I can use...
The encrypted string is not very important so I only need a easiest way to encrypt & decrypt it.
I hope someone could give me a suggestion.

Thanks a lot!!

Sincerely,

Hughes
Posted

1 solution

 
Share this answer
 
Comments
Member 10307999 18-Nov-14 23:07pm    
Hi Lancaster,
Thanks for your reply! I've research the essay you posted, and I can encrypt & decrypt a CString like:
[Head file]
CCrypto crypto;
CByteArray arBytes;
[cpp file]
void CEncryptionDlg::OnBnClickedButton_Encrypt()
{
crypto.DeriveKey(_T("ABCD"));
CString sPassword=_T("Hello");
crypto.Encrypt(sPassword,arBytes);
}
void CEncryptionDlg::OnBnClickedButton_Decrypt()
{
CString strDecrypted;
crypto.Decrypt(arBytes, strDecrypted);
MessageBox(strDecrypted, _T("Notify"), MB_OK);
}

And then, I want to save the sPassword but the type of data is CByteArray between the encrypt() and decrypt().
If I want to save the CByteArray in a txt file, I gotta transform into CString.
It failed it...

my code:
[Head file]
CCrypto crypto;
CString sContent;
[cpp file]
void CEncryptionDlg::OnBnClickedButton_EncryptString()
{
crypto.DeriveKey(_T("ABCDEF"));
CString sPassword=_T("Hi");
CByteArray arBytes2;
crypto.Encrypt(sPassword,arBytes2);
CString str2(arBytes2.GetData()); //CByteArray to CString
sTest=str2;
}
void CEncryptionDlg::OnBnClickedButton_DecryptString()
{
CByteArray byteArr;
//CString to CByteArray --- start
BYTE *pByteArray = (PBYTE)(LPCTSTR)sTest.GetBuffer();
byteArr.SetSize(sTest.GetLength());
memcpy(byteArr.GetData(), pByteArray, sTest.GetLength());
//CString to CByteArray --- end

CString sDecrypted;
crypto.Decrypt(byteArr, sDecrypted);
MessageBox(sDecrypted, _T("Notify"), MB_OK);
}

But the sDecryprted is NOT "Hi" I want to get :(
Could someone give me some suggestions?
Thanks!!!
Garth J Lancaster 19-Nov-14 4:31am    
I disagree with this

>> If I want to save the CByteArray in a txt file, I gotta transform into CString.


what is the rationale for that - you cant guarantee that the encrypted string contains human readable characters, so writing it to a text file ? (and if I were contemplating writing it to a text file given my previous comment I'd likely be base64 encoding it, but that's a different matter)

I would be writing it to a binary file - and avoiding the conversion back to CString - there's a lot we don't know about your project, for example, are you working in Unicode/UUTF-16, or ANSI, UTF-8 or ??? for example

// ** Untested **

CFile myFile;
CString strFileName = _T("Some File Name.dat");

// Write To File
if(myFile.Open(_T(strFileName), CFile::modeWrite | CFile::typeBinary | CFile::modeCreate))
{
myFile.Write(byteArr.GetData(),byteArr.GetSize());
}

// Read From File
CByteArray bytesFromFile;
if(myFile.Open(_T(strFileName), CFile::modeRead | CFile::typeBinary))
{
bytesFromFile.SetSize(1024); // ensure that buffer is allocated and the size we want it
UINT bytesRead = myFile.Read(bytesFromFile.GetData(), bytesFromFile.GetSize());
}
Member 10307999 24-Nov-14 22:06pm    
Many thanks :)

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