Class Encryfile
{
Public
void cryptstring (unsigned char chkey); Add, Decrypt string
BOOL Loadstringfromfile (const CString &strfilename, unsigned char chkey); Read Decrypt
BOOL Savestringtofile (const CString &strfilename, unsigned char chkey);//Save Encryption
Public
CString M_strfilename; Filename
CString M_strfilebuffer; File buffers
DWORD m_dwfilelength; File length
TCHAR Chbuffer[max_size]; File buffers//#define MAX_SIZE 65535
};
. cpp
Const unsigned char DEFAULT = 123;
void Cryptstring (unsgined char chkey)
{
DWORD Dwlen = M_strfilebuffer.getlength ();
For (DWORD i =0; i<dwlen; i++)
{
Chbuffer[i] = Chbuffer[i]^chkey; XOR, J encryption
}
M_strfilebuffer = Chbuffer;
}
BOOL Loadstringfromfile (const CString &strfilename, unsigned char chkey)
{
DWORD dwread = 0;
HANDLE hfile = CreateFile (strFileName, Generic_read | Generic_write, 0, NULL, open_existing, file_attribute_normal, NULL);
if (invalid_handle_value! = hfile)
{
Open success
M_dwfilelength = GetFileSize (hfile, NULL);
if (! ReadFile (hfile, Chbuffer, M_dwfilelength, &dwread, NULL))
{
Error
return false;
}
CloseHandle (hfile); Close file handle
}
Else
{
Error:open fail.
return false;
}
M_strfilebuffer = Chbuffer; //
Cryptstring (Chkey); Decrypt
Test the contents of a read-out file
hfile = CreateFile ("Xxx.ini", Generic_read | Generic_write, 0, NULL, create_always, file_attribute_normal, NULL);
BOOL BRet = (:: WriteFile (hfile, M_strfilebuffer, M_dwfilelength, &dwread, NULL));
if (BRet)
{
CloseHandle (hfile);
return true;
}
return false;
}
BOOL Savestringtofile (const Cstring &strfilename, unsigned char chkey)
{
DWORD dwread = 0;
HANDLE hfile = CreateFile (strFileName, Generic_read | Generic_write, 0, NULL, open_existing, file_attribute_normal,null);
if (invalid_handle_value! = hfile)
{
M_dwfilelength = GetFileSize (hfile, NULL);
if (! ReadFile (hfile, Chbuffer, M_dwfilelength, &dwread, NULL))
{
Error Read
return false;
}
CloseHandle (hfile);
}
Else
{
Open error
return false;
}
M_strfilebuffer = Chbuffer;
Cryptstring (Chkey); Encryption
hfile = CreateFile (strFileName, generic_write, 0, NULL, create_always, file_attribute_normal, NULL);
if (invalid_handle_value! = hfile)
{
Dwrod dwwrite = 0;
if (! WriteFile (hfile, M_strfilebuffer, M_dwfilelenght, &dwwrite, NULL))
{//erro write return false;}
Closehanle (hfile);
}
Else
{//error open return false}
Cryptstring (Chkey); The string is decrypted to not change the original string
return true;
}
Write Encrypted File contents