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

📄 filedlg.cpp

📁 DES加解密 AES(使用了文件读写的缓冲和多线程技术)
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// FileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AES.h"
#include "FileDlg.h"
#include "AESDlg.h"


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


CAESDlg *p_parent;
/////////////////////////////////////////////////////////////////////////////
// CFileDlg dialog


CFileDlg::CFileDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFileDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFileDlg)
	m_sfilename = _T("");
	m_cfile = _T("");
	m_enfile = _T("");
	m_ext = _T("");
	m_filelen = _T("");
	m_pfile = _T("");
	m_time = _T("");
	m_state = _T("");
	//}}AFX_DATA_INIT
}


void CFileDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFileDlg)
	DDX_Control(pDX, IDC_STATE, m_MsgState);
	DDX_Control(pDX, IDC_TIME, m_UsedTime);
	DDX_Control(pDX, IDC_PROGRESS, m_progress);
	DDX_Text(pDX, IDC_SOURCEFILE, m_sfilename);
	DDX_Text(pDX, IDC_CFILE, m_cfile);
	DDX_Text(pDX, IDC_ENFILE, m_enfile);
	DDX_Text(pDX, IDC_EXT, m_ext);
	DDX_Text(pDX, IDC_FILELEN, m_filelen);
	DDX_Text(pDX, IDC_PFILE, m_pfile);
	DDX_Text(pDX, IDC_TIME, m_time);
	DDX_Text(pDX, IDC_STATE, m_state);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFileDlg, CDialog)
	//{{AFX_MSG_MAP(CFileDlg)
	ON_BN_CLICKED(IDC_SFBROWSE, OnSFBrowse)
	ON_BN_CLICKED(IDC_ENFBROWSE, OnEnFBrowse)
	ON_BN_CLICKED(IDC_MFBROWSE, OnMFBrowse)
	ON_BN_CLICKED(IDC_PFBROWSE, OnPFBrowse)
	ON_BN_CLICKED(IDC_ENCRYP, OnEncryp)
	ON_BN_CLICKED(IDC_DECRYP, OnDecryp)
	ON_WM_TIMER()
	ON_EN_CHANGE(IDC_EXT, OnChangeExt)
	ON_BN_CLICKED(IDC_BUTCLEAR, OnButClear)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFileDlg message handlers

void CFileDlg::OnSFBrowse() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	CString lpszFilter;
	double length;
	CFileStatus status;//创建文件状态类的对象
	lpszFilter.Format("所有文件(*.*)|*.*||");
	CFileDialog filedlg(true,NULL,NULL,OFN_HIDEREADONLY,lpszFilter,NULL);
	if(filedlg.DoModal()==IDOK)
	{
		m_sfilename=filedlg.GetPathName();
		FileName=filedlg.GetFileName();
		CString ext=filedlg.GetFileExt();
		FileName=FileName.Left(FileName.GetLength()-ext.GetLength()-1);
		this->GetDlgItem(IDC_ENFBROWSE)->EnableWindow(true);//使浏览文件夹按钮可用
		CFile::GetStatus(m_sfilename,status);
		length=status.m_size;
		m_FileLength=long(length);
		if(length>=1024)
		{
			length=length/1024.0;
			if(length>=1024)
			{
				length=length/1024.0;
				m_filelen.Format("%ld字节\t亦即%.3f兆",status.m_size,length);//文件长度
			}
			else
			m_filelen.Format("%ld字节\t亦即%ld千字节",status.m_size,length);//文件长度
		}
		else
			m_filelen.Format("%ld字节",status.m_size);//文件长度
	}
	UpdateData(false);	
}

void CFileDlg::OnEnFBrowse() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	LPBROWSEINFO lpbi=new BROWSEINFO;
	lpbi->hwndOwner=GetSafeHwnd();//NULL;
	lpbi->pidlRoot=NULL;
	lpbi->pszDisplayName=NULL;
	lpbi->lpszTitle="请选择所要存放的目录";
	lpbi->ulFlags=BIF_BROWSEFORCOMPUTER|BIF_EDITBOX;
	lpbi->lpfn=NULL;
	LPITEMIDLIST lpitemidlist=SHBrowseForFolder(lpbi);
	if(lpitemidlist==NULL)
	{
		delete lpbi;
		lpbi=NULL;
		return;
	}
	char path[MAX_PATH];
	//转换项目标志符列表为一个文件路径
	SHGetPathFromIDList(lpitemidlist,path);
	delete lpbi;
	m_enfile.Format("%s\\%s.cryp",path,FileName);
	m_enfile.Replace("\\\\","\\");
	if(CheckSpace(m_enfile)==false)//检查磁盘的剩余空间是否足够
		m_enfile=_T("");
	UpdateData(false);	
}

void CFileDlg::OnMFBrowse() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	CString lpszFilter;
	double length;
	CFileStatus status;//创建文件状态类的对象
	lpszFilter.Format("加密文件(*.cryp)|*.cryp||");
	CFileDialog filedlg(true,NULL,NULL,OFN_HIDEREADONLY,lpszFilter,NULL);
	if(filedlg.DoModal()==IDOK)
	{
		m_cfile=filedlg.GetPathName();
		FileName=filedlg.GetFileName();
		CString ext=filedlg.GetFileExt();
		FileName=FileName.Left(FileName.GetLength()-ext.GetLength()-1);
		this->GetDlgItem(IDC_PFBROWSE)->EnableWindow(true);//使浏览文件夹按钮可用
		CFile::GetStatus(m_cfile,status);
		length=status.m_size;
		m_FileLength=long(length);
		if(length>=1024)
		{
			length=length/1024.0;
			if(length>=1024)
			{
				length=length/1024.0;
				m_filelen.Format("%ld字节\t亦即%.3f兆",status.m_size,length);//文件长度
			}
			else
			m_filelen.Format("%ld字节\t亦即%ld千字节",status.m_size,length);//文件长度
		}
		else
			m_filelen.Format("%ld字节",status.m_size);//文件长度
	}
	UpdateData(false);
}

void CFileDlg::OnPFBrowse() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	LPBROWSEINFO lpbi=new BROWSEINFO;
	lpbi->hwndOwner=GetSafeHwnd();//NULL;
	lpbi->pidlRoot=NULL;
	lpbi->pszDisplayName=NULL;
	lpbi->lpszTitle="请选择所要存放的目录";
	lpbi->ulFlags=BIF_BROWSEFORCOMPUTER|BIF_EDITBOX;
	lpbi->lpfn=NULL;
	LPITEMIDLIST lpitemidlist=SHBrowseForFolder(lpbi);
	if(lpitemidlist==NULL)
	{
		delete lpbi;
		lpbi=NULL;
		return;
	}
	char path[MAX_PATH];
	//转换项目标志符列表为一个文件路径
	SHGetPathFromIDList(lpitemidlist,path);
	delete lpbi;
	if(m_ext.IsEmpty())
	{
		MessageBox("加密后文件的后缀名尚未给定!不能执行此操作!","警告",MB_OK|MB_ICONWARNING);
		return;
	}
	m_pfile.Format("%s\\%s.%s",path,FileName,m_ext);
	m_pfile.Replace("\\\\","\\");
	if(CheckSpace(m_pfile)==false)//检查磁盘的剩余空间是否足够
		m_pfile=_T("");
	UpdateData(false);	
}

BOOL CFileDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	// TODO: Add extra initialization here
	p_parent=(CAESDlg *)this->GetParentOwner();
	m_ext.Format("txt");
	this->GetDlgItem(IDC_ENFBROWSE)->EnableWindow(false);//先禁用浏览文件夹按钮
	this->GetDlgItem(IDC_PFBROWSE)->EnableWindow(false);//先禁用浏览文件夹按钮
	m_state.Format("<无任务>");
	m_filelen.Format("<无文件>");
	m_time.Format("0秒");
	m_progress.SetRange(0,100);
	m_progress.SetPos(0);
	pMyThread=NULL;
	EnOrDecrypt=2;
//	pMyThread=AfxBeginThread(MyThreadProc,this,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED,NULL);//创建线程
//	pMyThread=AfxBeginThread(MyThreadProc,this);//创建线程
	UpdateData(false);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CFileDlg::OnEncryp() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	int Nk,Nr,sel;
	CString str;
	if(p_parent->m_key.IsEmpty()||m_sfilename.IsEmpty()||m_enfile.IsEmpty())
	{
		MessageBox("所提供的信息不全,无法执行此操作!","错误",MB_OK|MB_ICONWARNING);
		return;
	}
	if(m_FileLength<16)
	{
		MessageBox("所给定的文件的长度必须大于等于16个字节!","错误",MB_OK|MB_ICONWARNING);
		return;	
	}
	if(CheckSpace(m_enfile)==false)//检查磁盘的剩余空间是否足够
		return;	
	sel=p_parent->m_lentype.GetCurSel();//取得密钥长度的类型
	switch(sel)
	{
	case 0:Nk=4;Nr=10;break;
	case 1:Nk=6;Nr=12;break;
	case 2:Nk=8;Nr=14;break;
	}
	if(p_parent->m_key.GetLength()<Nk*4)
		m_key=p_parent->m_keytemp;
	else
		m_key=p_parent->m_key;
	if(aes.SetVariable(Nk,Nr,m_key)==false)
	{
		MessageBox(aes.ErrorMessage,"错误",MB_OK||MB_ICONWARNING);
		return;
	}
	if(aes.SetFileName(m_sfilename,m_enfile)==false)
	{
		MessageBox(aes.ErrorMessage,"错误",MB_OK||MB_ICONWARNING);
		return;
	}
	m_state.Format("正在加密文件,这根据文件的大小所需要的时间长度不一。请耐心等待!");
	str.Format("正在加密文件 “%s”!",m_sfilename);//状态条信息
	p_parent->m_StatusBar.SetText(str,0,1);
	m_progress.SetPos(0);//设置进度条的位置
	SetTimer(IDC_TIMER,10,NULL);
	UpdateData(false);
	m_UsedTime.SetWindowText("正在计算运行时间...");

	this->GetDlgItem(IDC_DECRYP)->EnableWindow(false);//正在执行加密,则先禁用解密按钮
	this->GetDlgItem(IDC_ENCRYP)->EnableWindow(false);//正在执行加密,则先禁用加密按钮
	this->GetDlgItem(IDC_EXT)->EnableWindow(false);//正在执行加密,则先禁用后缀名按钮
	EnOrDecrypt=0;

/*	pMyThread->ResumeThread();
	if(pMyThread!=NULL)
	{
		pMyThread->PostThreadMessage(WM_QUIT,0,0);
		pMyThread->ExitInstance();
		::WaitForSingleObject(pMyThread->m_hThread,INFINITE);
		pMyThread=NULL;
	}
*/	pMyThread=AfxBeginThread(MyThreadProc,this);//创建线程
	
}

void CFileDlg::OnDecryp() 
{

⌨️ 快捷键说明

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