Write Encrypted File contents

Source: Internet
Author: User
Tags readfile

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.