上位机MFC实现文件加密解压功能
例程实现对指定文件或指定文件夹内的全部文件进行加密 与解密功能。
效果如上图。
点击浏览文件夹或文件后,
可以输入加密文件进行加密或解密。
运行例程时,首先要通过密码登录软件
- void CSecurityDlg::OnOK()
- {
- // TODO: Add extra validation here
- static int cx=0;
- if(UpdateData()==FALSE) return;
- if(m_Password.Compare("wmr")) {
- if(cx<2) {
- MessageBox("密码错误,请重新输入 !"," 权限校验 ",MB_ICONWARNING);
- cx++;
- } else {
- MessageBox("密码错误,您无权使用本工具 !"," 权限校验 ",MB_ICONSTOP);
- CDialog::OnCancel();
- }
- return;
- }
- CDialog::OnOK();
- }
复制代码 然后才是浏览要加密 的文件
- void CLockFileDlg::OnView()
- {
- // 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();
- }
- }
复制代码 最后才是执行文件的加密
- 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);
- }
复制代码 工程源代码下载:
上位机VC MFC程序开发精典实例大全源码与视频讲解配套下载408例 经历1年的编程与录制点击进入查看
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
|