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

📄 lockfiledlg.cpp

📁 一个Windows下的文件加密工具
💻 CPP
📖 第 1 页 / 共 2 页
字号:
{
	// TODO: Add your control notification handler code here
	if(UpdateData()==FALSE) return;

	CSBDestination sb(m_hWnd,IDS_TITLE);
	sb.SetFlags( BIF_RETURNONLYFSDIRS | BIF_BROWSEINCLUDEFILES /*| BIF_STATUSTEXT */);
	sb.SetInitialSelection(m_FileName);
	if (sb.SelectFolder()) {
		m_FileName = sb.GetSelectedFolder();
		UpdateData(FALSE);
		OnChanged();
	}
}

bool CLockFileDlg::IsDir(char* pFileName)
{
	char FileName[MAX_PATH];
	strcpy(FileName,pFileName);

	BOOL bIsDir=FALSE;
	char ch=FileName[strlen(FileName)-1];
	if(ch=='\\' || ch==':') bIsDir=TRUE;
	else if(memcmp(FileName,"\\\\",2)==0) if(strstr(FileName+2,"\\")==NULL) bIsDir=TRUE;
	if(bIsDir==FALSE) {
		strcat(FileName,"\\*.*");
		CFileFind Finder;
		bIsDir=Finder.FindFile(FileName);
		Finder.Close();
	}
	return (bIsDir==TRUE);
}

bool CLockFileDlg::IsDir(CString strFileName)
{
	char FileName[MAX_PATH];
	strcpy(FileName,strFileName);
	return IsDir(FileName);
}

void CLockFileDlg::DisplayMessage(char* pStr,char* pExt)
{
	char tmpBuf[MAX_PATH];
	if(pStr==NULL && pExt && m_MsgList.GetCount()) {
		m_MsgList.GetText(m_MsgList.GetCount()-1,tmpBuf);
		pStr=tmpBuf;
		m_MsgList.DeleteString(m_MsgList.GetCount()-1);
	} else if(pStr!=NULL && pExt==NULL) {
		if(strlen(pStr)>MSG_LEN) {
			char p[MAX_PATH];
			GetShortName(pStr,p);
			strcpy(pStr,p);
		}
	}
	if(pStr) strcpy(tmpBuf,pStr);
	else tmpBuf[0]=0;
	if(pExt) strcat(tmpBuf,pExt);
	m_MsgList.AddString(tmpBuf);
}

void CLockFileDlg::DisplayMessage(char* pStr,CString Ext)
{
	char tmpBuf[MAX_PATH];
	if(pStr==NULL && m_MsgList.GetCount()) {
		m_MsgList.GetText(m_MsgList.GetCount()-1,tmpBuf);
		pStr=tmpBuf;
		m_MsgList.DeleteString(m_MsgList.GetCount()-1);
	}
	if(pStr) strcpy(tmpBuf,pStr);
	else tmpBuf[0]=0;
	strcat(tmpBuf,Ext);
	m_MsgList.AddString(tmpBuf);
}

void CLockFileDlg::DisplayMessage(CString str,char* pExt)
{
	if(pExt==NULL) {
		if(str.GetLength()>MSG_LEN) {
			char p[MAX_PATH];
			GetShortName(str.GetBuffer(MAX_PATH),p);
			str.ReleaseBuffer();
			str=p;
		}
	} else str+=pExt;

	m_MsgList.AddString(str);
}

void CLockFileDlg::DisplayMessage(CString str,CString Ext)
{
	str+=Ext;
	m_MsgList.AddString(str);
}

void CLockFileDlg::GetShortName(char* pLongName,char* pShortName)
{
	char tmpBuf[MAX_PATH],*p=tmpBuf;
	if(pLongName==NULL) return;
	int Len=strlen(pLongName);
	if(Len>MAX_PATH) return;
	strcpy(tmpBuf,pLongName);
	if(pShortName==NULL) pShortName=pLongName;
	strcpy(pShortName,tmpBuf);
	if(Len<=MSG_LEN) return;

	int i,Pos1=-1,Pos2=-1;
	for(i=0;i<Len;i++) {
		if(*p=='\\') { if(Pos1==-1) Pos1=i; Pos2=i; }
		p++;
	}
	if(Pos1==Pos2) Pos1=-1;
	if(Pos2<(MSG_LEN-12)) {
		if(strstr(tmpBuf,".") && (Pos2=strlen(strstr(tmpBuf,".")))<(MSG_LEN/2)) {
			memcpy(pShortName,tmpBuf,MSG_LEN-3-Pos2);
			strcpy(pShortName+MSG_LEN-3-Pos2,"...");
			strcpy(pShortName+MSG_LEN-Pos2,strstr(tmpBuf,"."));
		} else {
			memcpy(pShortName,tmpBuf,MSG_LEN-3);
			strcpy(pShortName+MSG_LEN-3,"...");
		}
		return;
	}
	while(1) {
		p=tmpBuf+Pos1+1;
		for(i=Pos1+1;i<Pos2;i++) {
			if(*p=='\\') break;
			p++;
		}
		if(i<Pos2 && (Pos2-i)>(Len-(MSG_LEN-3)) || (i<(MSG_LEN-15))) Pos1=i;
		else break;
	}
	if(Pos1==-1 && Pos2>(MSG_LEN-14)) Pos1=MSG_LEN-17;
	memcpy(pShortName,tmpBuf,Pos1+1);
	strcpy(pShortName+Pos1+1,"...");
	if((Len-Pos2)>(MSG_LEN-Pos1-4)) {
		int Len;
		if(strstr(tmpBuf+Pos2,".") && (Len=strlen(strstr(tmpBuf+Pos2,".")))<10) {
			memcpy(pShortName+Pos1+4,tmpBuf+Pos2,MSG_LEN-3-Pos1-4-Len);
			strcpy(pShortName+MSG_LEN-3-Len,"...");
			strcpy(pShortName+MSG_LEN-Len,strstr(tmpBuf+Pos2,"."));
		} else {
			memcpy(pShortName+Pos1+4,tmpBuf+Pos2,MSG_LEN-3-Pos1-4);
			strcpy(pShortName+MSG_LEN-3,"...");
		}
	} else strcpy(pShortName+Pos1+4,tmpBuf+Pos2);
}

void CLockFileDlg::GetDispName(char* pShortName,char* pLongName)
{
	if(pShortName==NULL) return;
	strcpy(pShortName,pLongName);
	while(strstr(pShortName,"\\")) strcpy(pShortName,strstr(pShortName,"\\")+1);
}

void CLockFileDlg::GetDispName(CString& strShortName,CString strLongName)
{
	char ShortName[MAX_PATH],LongName[MAX_PATH];
	strcpy(LongName,strLongName);
	GetDispName(ShortName,LongName);
	strShortName=ShortName;
}

void CLockFileDlg::OnChanged() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	if(UpdateData()==FALSE) return;
	GetDlgItem(IDC_LOCK)->EnableWindow(m_Busy==FALSE && m_FileName.IsEmpty()==FALSE && m_Password.IsEmpty()==FALSE && m_SurePassword==m_Password);
}

typedef CList<CString,CString> CFileList;

void CLockFileDlg::LockFile(CString DirName,CString FileName,CString Password)
{
	CString FindName,DispName;
	CFileList FileList,DirList;
	FileList.RemoveAll();
	DirList.RemoveAll();

	char CurDir[MAX_PATH];
	GetCurrentDirectory(MAX_PATH,CurDir);
	if(m_isNet || SetCurrentDirectory(DirName)==TRUE) {
		if(DirName.GetLength()>(MSG_LEN-23)) {
			DisplayMessage("Process directory ---- ","");
			DisplayMessage(DirName,NULL);
		} else DisplayMessage("Process directory ---- ",DirName);
	}
	
	CFileFind Finder;
	BOOL bOK=Finder.FindFile(FileName);
	if(bOK) {
		while(bOK) {
			bOK = Finder.FindNextFile();
			if(Finder.IsDirectory()==FALSE) {
				FindName=m_isNet?Finder.GetFilePath():Finder.GetFileName();
				FileList.AddTail(FindName);
				m_TotalFileNum++;
			}
		}
	}
	Finder.Close();
	if(m_SubDirFlg) {
		if(m_isNet) {
			if(DirName.GetAt(DirName.GetLength()-1)!='\\') DirName+="\\";
			DirName+="*.*";
		}
		bOK=Finder.FindFile(m_isNet?DirName:"*.*");
		if(bOK) {
			while(bOK) {
				bOK = Finder.FindNextFile();
				if(Finder.IsDirectory() && Finder.IsDots()==FALSE) {
					FindName=Finder.GetFilePath();
					DirList.AddTail(FindName);
				}
			}
		}
		Finder.Close();
	}
	
	m_DirNum++;

	HANDLE hFile;
	DWORD dwFileAttr,dwNewAttr;
	POSITION pos=FileList.GetHeadPosition();
	if(FileList.GetCount()==0) DisplayMessage("File not found","");
	else for(int i=0;i<FileList.GetCount();i++) {
		FindName=FileList.GetNext(pos);
		GetDispName(DispName,FindName);
		DisplayMessage(DispName,NULL);
		if((dwFileAttr=GetFileAttributes(FindName))!=-1) {
			dwNewAttr=dwFileAttr;
			if(m_Readonly) dwNewAttr &= ~FILE_ATTRIBUTE_READONLY;
			if(m_Hidden) dwNewAttr &= ~FILE_ATTRIBUTE_HIDDEN | ~FILE_ATTRIBUTE_SYSTEM;
			else if(dwFileAttr & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) dwNewAttr |= FILE_ATTRIBUTE_READONLY;
			SetFileAttributes(FindName,dwNewAttr);
		}
		hFile=CreateFile(FindName,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
		if(hFile==INVALID_HANDLE_VALUE) DisplayMessage(NULL,",Fail");
		else {
			DWORD nBytes,nBytesRead;
			nBytes=GetFileSize(hFile,&nBytesRead);
			if(nBytes>(10*1024*1024)) nBytes=10*1024*1024;
			char* pBuf=new char[nBytes];
			if(pBuf!=NULL) {
				FILETIME tm1,tm2,tm3;
				GetFileTime(hFile,&tm1,&tm2,&tm3);
				if(ReadFile(hFile,pBuf,nBytes,&nBytesRead,NULL)!=FALSE && nBytesRead==nBytes) {
					PassData(pBuf,nBytes,Password);
					if(SetFilePointer(hFile,0,NULL,FILE_BEGIN)==0 && WriteFile(hFile,pBuf,nBytes,&nBytesRead,NULL)!=FALSE && nBytesRead==nBytes) {
						DisplayMessage(NULL,",OK");
						m_OkFileNum++;
					} else DisplayMessage(NULL,",Fail");
				} else DisplayMessage(NULL,",Fail");
				SetFileTime(hFile,&tm1,&tm2,&tm3);
				delete pBuf;
			} else DisplayMessage(NULL,",Fail");
			CloseHandle(hFile);
		}
		if(dwFileAttr!=-1) SetFileAttributes(FindName,dwFileAttr);
	}
	pos=DirList.GetHeadPosition();
	for(int i=0;i<DirList.GetCount();i++) {
		FindName=DirList.GetNext(pos);
		if(m_isNet) {
			FileName=FindName;
			if(FileName.GetAt(FileName.GetLength()-1)!='\\') FileName+="\\";
			FileName+="*.*";
		}
		LockFile(FindName,FileName,Password);
	}
	if(m_isNet==FALSE) SetCurrentDirectory(CurDir);
}

void PassData(char* pDataBuf,DWORD DataLen,CString Password)
{
	char pPass[128];
	BYTE* p=(BYTE*)pDataBuf;
	DWORD i=0,j,PassLen=Password.GetLength();
	strcpy(pPass,Password);

	while(i<DataLen) {
		for(j=0;j<PassLen;j++) {
			if((i+j)>=DataLen) break;
			*p^=pPass[j]; // 与密码异或
			*p^=0x01;     // 最后一位取反
			p++;
		}
		i+=j;
	}
}

void CLockFileDlg::OnCancel() 
{
	// TODO: Add your control notification handler code here
	if(MessageBox("确认退出本工具吗 ?"," 退出确认 ",MB_YESNO|MB_DEFBUTTON2|MB_ICONQUESTION)==IDYES)
		CDialog::OnCancel();
}

⌨️ 快捷键说明

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