⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unlockdlg.cpp

📁 主要完成功能如下: 支持资源管理器的右键菜单; 支持加密和解密; 加密文件后缀为.lck
💻 CPP
字号:
// UnlockDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FreeLock.h"
#include "UnlockDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CUnlockDlg dialog


CUnlockDlg::CUnlockDlg(char *cp,CWnd* pParent /*=NULL*/)
	: CDialog(CUnlockDlg::IDD, pParent)
{FilePath=cp;
	//{{AFX_DATA_INIT(CUnlockDlg)
	FileName = _T("");
	Key = _T("");
	Delete = FALSE;
	//}}AFX_DATA_INIT
}


void CUnlockDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUnlockDlg)
	DDX_Control(pDX, IDC_UNLOCK, Unlock);
	DDX_Control(pDX, IDC_OPEN, Open);
	DDX_Text(pDX, IDC_FILE, FileName);
	DDX_Text(pDX, IDC_KEY, Key);
	DDV_MaxChars(pDX, Key, 20);
	DDX_Check(pDX, IDC_DELETE, Delete);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CUnlockDlg, CDialog)
	//{{AFX_MSG_MAP(CUnlockDlg)
	ON_EN_CHANGE(IDC_KEY, OnChangeKey)
	ON_BN_CLICKED(IDC_UNLOCK, OnUnlock)
	ON_BN_CLICKED(IDC_OPEN, OnOpen)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUnlockDlg message handlers

BOOL CUnlockDlg::OnInitDialog() 
{
 SetWindowText("文件解密");
	CDialog::OnInitDialog();
	
 File.SetFilePath(FilePath);
	FileName=File.GetFileName();
 Delete=TRUE;
 UpdateData(FALSE);
	return TRUE;
}

void CUnlockDlg::OnChangeKey() 
{UpdateData(TRUE);
 Unlock.EnableWindow(Key.GetLength());
 Open.EnableWindow(Key.GetLength());	
}

void CUnlockDlg::OnUnlock() 
{
 UnlockFile();
}

void CUnlockDlg::OnOpen() 
{
 UnlockFile(TRUE);
}

void CUnlockDlg::UnlockFile(int Open)
{
 char NewName[MAX_PATH];
 unsigned char Key0[20],Index[20],Buffer[BUFFER_SIZE];

 UpdateData(TRUE);
 OnCancel();

 unsigned char Length=FileName.GetLength();
 File.Open(FilePath,CFile::modeRead);
 File.Read(&Length,sizeof(unsigned char));
 File.Read(NewName,Length);
 NewName[Length]='\0';

 unsigned char BlueJoe[10]="白乔 浪浪";
 unsigned char ch,*cp1=(unsigned char *)LPCTSTR(Key),*cp2=BlueJoe;

 File.Read(&Length,sizeof(unsigned char));
 Length=Length^131;

 if(Length!=Key.GetLength())
 {
  Alert();
  return;
 }

 for(int i=0;i<Length;i++)
 {
  File.Read(&ch,sizeof(unsigned char));
  Key0[i]=ch^(*cp2);
  cp2++;
  if(!*cp2)
   cp2=BlueJoe;

  File.Read(&ch,sizeof(unsigned char));

  ch^=131;
  Index[i]=ch;

  if(Key0[i]!=Key[(int)ch])
  {
   Alert();
   return;
  }
 }
 Key0[Length]='\0';

 char TempPath[MAX_PATH];
 GetTempPath(MAX_PATH,TempPath);
 strcat(TempPath,NewName);

 CFile Output(TempPath,CFile::modeCreate|CFile::modeWrite);

 cp2=BlueJoe;

 while(1)
 {int Size=File.Read(Buffer,BUFFER_SIZE);
  if(!Size)
   break;
  unsigned char *cp=Buffer;
  i=0;
  while(i<Size)
  {*cp=(*cp)^(*cp1)^(*cp2);
   cp++;
   cp1++;
   cp2++;
   i++;
   if(!*cp1)
    cp1=(unsigned char *)LPCTSTR(Key);
   if(!*cp2)
    cp2=BlueJoe;
  }
  Output.Write(Buffer,Size);
  if(Size<BUFFER_SIZE)
  break;
 }

 Output.Close();
 File.Close();

 if(Open)
 {
  ShellExecute(NULL,"open",TempPath,NULL,NULL,SW_SHOW);
  return;
 }
 
 if(Delete)
  File.Remove(FileName);

 if(File.Open(NewName,CFile::modeRead))
 {
  File.Close();
  File.Remove(NewName);
 }
 File.Rename(TempPath,NewName);
}

void CUnlockDlg::Alert()
{
 AfxMessageBox("密码错误,你没有权限使用该文档!",MB_OK,0);
 return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -