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

📄 desfiledlg.cpp

📁 密码学实验 SDES
💻 CPP
字号:
// DesFileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Ldw.h"
#include "DesFileDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDesFileDlg dialog


CDesFileDlg::CDesFileDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDesFileDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDesFileDlg)
	m_ifile = _T("");
	m_ofile = _T("");
	m_key = _T("");
	//}}AFX_DATA_INIT
}


void CDesFileDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDesFileDlg)
	DDX_Control(pDX, IDCANCEL, m_exit);
	DDX_Control(pDX, IDC_UNDO, m_undo);
	DDX_Control(pDX, IDC_UNDES, m_undes);
	DDX_Control(pDX, IDC_DES, m_des);
	DDX_Control(pDX, IDC_TIME, m_time);
	DDX_Control(pDX, IDC_PROGRESS1, m_pro);
	DDX_Text(pDX, IDC_IFILE, m_ifile);
	DDX_Text(pDX, IDC_OFILE, m_ofile);
	DDX_Text(pDX, IDC_KEY, m_key);
	DDV_MaxChars(pDX, m_key, 10);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDesFileDlg, CDialog)
	//{{AFX_MSG_MAP(CDesFileDlg)
	ON_BN_CLICKED(IDC_GETIFILE, OnGetifile)
	ON_BN_CLICKED(IDC_GETOFILE, OnGetofile)
	ON_BN_CLICKED(IDC_DES, OnDes)
	ON_BN_CLICKED(IDC_UNDES, OnUndes)
	ON_BN_CLICKED(IDC_UNDO, OnUndo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDesFileDlg message handlers
BOOL CDesFileDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	EnableVcl(1);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDesFileDlg::OnOK() 
{
	// TODO: Add extra validation here
	return;
	CDialog::OnOK();
}

void CDesFileDlg::OnGetifile() 
{
	// TODO: Add your control notification handler code here
	CFileDialog file(TRUE);
	file.m_ofn.lpstrTitle="选择要加密的文件";
	file.m_ofn.lpstrDefExt="*";
	file.m_ofn.lpstrFilter="所有文件    (*.*)\0*.*\0\0";
	if(file.DoModal() == IDOK)
	{
		m_ifile = file.GetPathName();
		UpdateData(0);
	}
}

void CDesFileDlg::OnGetofile() 
{
	// TODO: Add your control notification handler code here
	CFileDialog file(FALSE);
	file.m_ofn.lpstrTitle="输入加密后保存的文件";
	file.m_ofn.lpstrDefExt="des";
	file.m_ofn.lpstrFilter="所有文件    (*.*)\0*.*\0\0";
	if(file.DoModal() == IDOK)
	{
		m_ofile = file.GetPathName();
		UpdateData(0);
	}
}

void CDesFileDlg::OnDes() 
{
	UpdateData(1);
	if(m_key.GetLength() <=0)
	{
		AfxMessageBox("密钥不能为空。");
		return;
	}
	operate = DO_DES;
	isdo = true;
	EnableVcl(0);
	AfxBeginThread(DesThread,(LPVOID)this,THREAD_PRIORITY_NORMAL);
}

void CDesFileDlg::OnUndes() 
{
	// TODO: Add your control notification handler code here
	UpdateData(1);
	if(m_key.GetLength() <=0)
	{
		AfxMessageBox("密钥不能为空。");
		return;
	}
	operate = DO_UNDES;
	isdo = true;
	EnableVcl(0);
	AfxBeginThread(DesThread,(LPVOID)this,THREAD_PRIORITY_NORMAL);
}
void CDesFileDlg::OnUndo() 
{
	// TODO: Add your control notification handler code here
	isdo = false;
}

void CDesFileDlg::EnableVcl(byte type)
{
	switch(type)
	{
	case 0:
		GetDlgItem(IDC_IFILE)->EnableWindow(false);
		GetDlgItem(IDC_OFILE)->EnableWindow(false);
		GetDlgItem(IDC_KEY  )->EnableWindow(false);
		GetDlgItem(IDC_GETIFILE)->EnableWindow(false);
		GetDlgItem(IDC_GETOFILE)->EnableWindow(false);
		m_des.EnableWindow(false);
		m_undes.EnableWindow(false);
		m_undo.EnableWindow(true);
		m_exit.EnableWindow(false);
		break;
	case 1:
		GetDlgItem(IDC_IFILE)->EnableWindow(true);
		GetDlgItem(IDC_OFILE)->EnableWindow(true);
		GetDlgItem(IDC_KEY  )->EnableWindow(true);
		GetDlgItem(IDC_GETIFILE)->EnableWindow(true);
		GetDlgItem(IDC_GETOFILE)->EnableWindow(true);
		m_des.EnableWindow(true);
		m_undes.EnableWindow(true);
		m_undo.EnableWindow(false);
		m_exit.EnableWindow(true);
		break;
	case 2:
		break;
	case 3:
		break;
	}
}

UINT DesThread(LPVOID lparam)
{
	CDesFileDlg *dlg = (CDesFileDlg *)lparam;
	CFile ifile,ofile;
	if(ifile.Open(dlg->m_ifile,CFile::modeRead) == 0)
	{
		AfxMessageBox("输入文件无法打开,文件名无效。");
		dlg->EnableVcl(1);
		return 0;
	}
	if(ofile.Open(dlg->m_ofile,CFile::modeWrite|CFile::modeCreate) == 0)
	{
		AfxMessageBox("输出文件无法打开,文件名无效。");
		ifile.Close();
		dlg->EnableVcl(1);
		return 0;
	}
	///////////////////////////////////
	byte operate = dlg->operate;
	DWORD now,last = GetTickCount();
	CString ti;
	
	CSDes des(1024);
	des.SetKey(dlg->m_key);
	DWORD filelen,readlen,writelen;
	filelen = ifile.GetLength();
	readlen = writelen = 0;
	///////////////////////////////////
	dlg->m_pro.SetRange(0,1000);
	///////////////////////////////////
	while((writelen < filelen) && dlg->isdo)
	{
		if(filelen - writelen >= 1024)
		{
			readlen = 1024;
		}
		else
		{
			readlen = filelen - writelen;
		}
		ifile.Read(des.GetPByte(),readlen);
		switch(operate)
		{
		case DO_DES:
			des.Des();
			break;
		case DO_UNDES:
			des.UnDes();
			break;
		}
		ofile.Write(des.GetPByte(),readlen);
		writelen += readlen;

		if( GetTickCount() - now > 10)
		{
			now = GetTickCount();
			dlg->m_pro.SetPos(short(((float)writelen/filelen)*1000));
			dlg->m_pro.RedrawWindow();
		
			ti.Format("已用时[%.3f]秒,剩余[%.3f]秒",float(now-last)/1000,(float(now-last) * ( filelen-writelen)) /(float)writelen /1000);

			dlg->m_time.SetWindowText(ti);
			dlg->m_time.RedrawWindow();
		}
	}
	ifile.Close();
	ofile.Close();
	if(writelen >= filelen)
	{
		now = GetTickCount();
		ti.Format("文件解密成功。用时[%.3f]秒",float(now-last)/1000);
		AfxMessageBox(ti);
	}

	dlg->m_pro.SetPos(0);
	dlg->m_time.SetWindowText("");
	dlg->EnableVcl(1);

	return 0;
}

⌨️ 快捷键说明

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