Implement file Encryption function

Source: Internet
Author: User
Tags bool decrypt knowledge base

I downloaded a "Xu Jingzhou" in the VC Knowledge Base (vckbase.com) a few days ago, the source code of a gadget called < encryption star >, I compiled it and took the file to experiment to find that it doesn't work for most files. Encrypted text file only the first part of the file is encrypted and most of the complete existence, if used to encrypt mp3, simply does not work, after the encryption of the MP3 or can be very good from the end of playback. So I invented a file encryption method, although very simple, but can securely encrypt any computer files and can also use the same password for multiple encryption of the same file.

The method I use is to read and encrypt every byte of the file from beginning to end, as follows: (please refer to source code);

Create a new project based on the dialog box (named Ecflie, and then remove the static controls that are identified, canceled, and system additions.) add three buttons and two edit controls to the dialog box. Two edit controls are associated with variables CString M_path and CString M_pass, where m_ Pass as the user input password, m_path as a file path;

The three buttons are the browse, encrypt, and Decrypt buttons respectively.

Add the following function:

__int64 epass(); //密码初始化函数
BOOL ecfile(LPCTSTR fpath);//文件加密函数
BOOL dcfile(LPCTSTR fpath);//文件解密函数

A function to encrypt a file BOOL cecfiledlg::ecfile (lpctstr fpath)
{
Char *data;
CFile *file;
DWORD Flen;   
M_password = Epass ();//password initialization
file = new CFile;
if (!file->open (Fpath, cfile::sharedenynone|   Cfile::modereadwrite))
{
return FALSE;

Flen = File->getlength ();   
data = new char[(int) flen];//allocating memory for Files
File->seektobegin ();
File->read (data, Flen);     
//This encrypts all the bytes in the file
for (int i=0; i< (int) flen; i++)
{
Data[i] ^= M_password;
 Data[i] ^= Flen;   Because the size of the file increases by 5 bytes after each encryption, it doesn't matter if you encrypt it two times with the same password
}
File->seektobegin ();
File->write (data, Flen);   
delete[] data;//free memory first
//Add password Authentication information
Char cpass[5] = "Love";
  for (int j=0; j<5; j + +)
{
Cpass[j] ^= M_password;
}
File->seektoend ();   
File->write (&cpass, 5);//Add a neat part of the password at the end of the file, so that the file is incremented by 5 bytes
File->close ();
Delete file;
return TRUE;
}

function to decrypt a fileBOOL Cecfiledlg::d cfile (LPCTSTR fpath)
{
Char *data;
CFile *file;
DWORD Flen;
Char love[5];
File = new CFile;
if (!file->open (Fpath, cfile::sharedenynone| Cfile::modereadwrite))
{
return FALSE;
}
Flen = File->getlength ();
data = new char[(int) Flen];
Verify that the password is correct
File->seek ( -5, cfile::end);
File->read (&love, 5);
M_password = Epass ();
for (int i=0; i<5; i++)
{
Love[i] ^= M_password;
}
if (strcmp (Love, "Love")!=0)
{
return FALSE;
}
Decrypt
File->seektobegin ();
File->read (data, Flen);
To decrypt the original method
for (int j=0; j< (int) Flen; j + +)
{
DATA[J] ^= M_password;
DATA[J] ^= (flen-5);
}
File->seektobegin ();
File->write (data, Flen);
File->setlength (flen-5); Removing encryption is the added password verification section
File->close ();
delete[] data;
Delete file;
return TRUE;
}

The function that gets the password above (M_password = Epass ();) can be defined by itself, I only do the simple operation of the password:__int64 CEcfileDlg::epass()
{
  DWORD plen;
  char *ppass;
  __int64 mc= 8757735233305;
  UpdateData(TRUE);
  ppass = m_pass.GetBuffer(0);
  plen = strlen(ppass);
  for(int i=0; i<(int)plen; i++)
  {
    mc ^= ppass[i]|128;
  }
  return mc;
}

Then you can call these two functions in the right place. For details please check the source code, (under Win XP debugging passed).

This article supporting source code

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.