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

📄 desdlg.cpp

📁 基于DES加密算法
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// DESDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DES.h"
#include "DESDlg.h"

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

#define MAXIDSTRINGSIZE		128
#define ID_STRING	"THIS FILE IS ENCRYPTED BY SINMIN'S TOOL. VERSION 1.00 .\nCONNECT ME: sinmin@0451.com\n"
#define BUFFERSIZE		(256*1024)		// 256K buffer each

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDESDlg dialog

CDESDlg::CDESDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDESDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDESDlg)
	m_strSourceFile = _T("");
	m_strDestinationFile = _T("");
	m_strPassword = _T("");
	m_strPassword2 = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDESDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDESDlg)
	DDX_Control(pDX, IDC_PROGRESS, m_ctrlProgress);
	DDX_Text(pDX, IDC_EDTSOURCE, m_strSourceFile);
	DDX_Text(pDX, IDC_EDTDESTINATION, m_strDestinationFile);
	DDX_Text(pDX, IDC_EDTPASSWORD, m_strPassword);
	DDX_Text(pDX, IDC_EDTCONFIRMPASSWORD, m_strPassword2);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDESDlg, CDialog)
	//{{AFX_MSG_MAP(CDESDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTNGETSOURCE, OnBtngetsource)
	ON_BN_CLICKED(IDC_GETDESTINATION, OnGetdestination)
	ON_BN_CLICKED(IDC_BTNGO, OnBtngo)
	ON_BN_CLICKED(IDC_HIDEPASSWORD, OnHidepassword)
	ON_EN_KILLFOCUS(IDC_EDTCONFIRMPASSWORD, OnKillfocusEdtconfirmpassword)
	ON_EN_CHANGE(IDC_EDTSOURCE, OnChangeEdtsource)
	ON_EN_CHANGE(IDC_EDTDESTINATION, OnChangeEdtdestination)
	ON_EN_CHANGE(IDC_EDTCONFIRMPASSWORD, OnChangeEdtconfirmpassword)
	ON_EN_CHANGE(IDC_EDTPASSWORD, OnChangeEdtpassword)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDESDlg message handlers

BOOL CDESDlg::OnInitDialog()
{
	CEdit *pEdt;
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

	((CButton *)GetDlgItem(IDC_HIDEPASSWORD))->SetCheck(1);

	pEdt=(CEdit *)GetDlgItem(IDC_EDTPASSWORD);
	pEdt->SetPasswordChar('*');
	pEdt=(CEdit *)GetDlgItem(IDC_EDTCONFIRMPASSWORD);
	pEdt->SetPasswordChar('*');

	allReady();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDESDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDESDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDESDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


//process the ECP file, is the file is a ECP file, the get 
//information about: a) original file Infor(CFileStatus); (b)
//      CRC;  (3)CRC is indetical with backups or not(m_CRCOK);
//      (4), m_ECFFileStart, the start point of the ecp file
// if it's not a ECP file or file no exist, then return false

// the head infor include:
//(1) encryption file ID string( a string such as 
//	  'ENCRYPTED FILE, BY SIMIN'S TOOL'
//    (64bytes),empty bytes fill 0x00
//(2) CRC code(12 bytes(4 bytes CRC and 2 backups))
//(3) encryption algrithon and Version ( 2 bytes)
//		high byte is the algrithom number, low byte is the
//		sub number
//(4)  file information length( 2bytes) (sizeof(CFileStatus))
//(3)original file's lenght,date time, file path and name
//		(256 bytes)		
BOOL CDESDlg::processECPFile(CString strPN)
{
	char cInfo[MAXIDSTRINGSIZE+2];
	unsigned int crcBk1,crcBk2;
	WORD  nFlStaSize;
	CFileStatus flSta;

	if(!CFile::GetStatus(strPN,flSta)){// file do not exist
		return FALSE;
	}
	
	try{
		CFile fl(strPN,CFile::modeRead);	//OPEN FILE
		fl.Read(cInfo,MAXIDSTRINGSIZE);
		cInfo[MAXIDSTRINGSIZE]=cInfo[MAXIDSTRINGSIZE+1]=0;// make a terminator
		
		if((strstr(cInfo,"SINMIN")==NULL ) ||		//not found 'sinmin'
			(strstr(cInfo,"ENCRYPTED")==NULL)){		//not found 'encrypted'
			return FALSE;
		}
		//getCRCs
		fl.Read(&m_CRC,4);
		fl.Read(&crcBk1,4);
		fl.Read(&crcBk2,4);
		if( (m_CRC==crcBk1) && (m_CRC==crcBk2))
			m_CRCOK=TRUE;
		else{
			if(crcBk1==crcBk2)	m_CRC=crcBk1;
			m_CRCOK=FALSE;
		}

		//get algorithm number, do nothing in this version
		fl.Read(&crcBk1,2);			//for skip the algorithm bytes
		
		//get start point of the data
		fl.Read(&nFlStaSize,2);
		m_ECPFileStart=MAXIDSTRINGSIZE+16+nFlStaSize;

		//get file status information
		fl.Read(&m_fileStatus,nFlStaSize);
	}catch(CException *e){
		e->Delete();
		return FALSE;
	}

		return TRUE;
}


void CDESDlg::OnBtngetsource() 
{
	// TODO: Add your control notification handler code here
	m_strSourceFile=getFilePathName(TRUE,m_strSourceFile);
	if(!m_strSourceFile.IsEmpty()){
		//check the file is a encrypted file or not. if it is, then
		// setup the original file name, if not, then give a 
		// recommended file  name		
		if(processECPFile(m_strSourceFile)){	//source file is .Ecp file
			m_strDestinationFile=m_fileStatus.m_szFullName;
		}else{
			m_strDestinationFile=m_strSourceFile+".ECP";
		}
		UpdateData(FALSE);		//member val to control
	}else
		UpdateData(TRUE);		// RESTORE OLD INFORMATION
	
	allReady();
}

// set correspond property for the dialog,then open the dialog,
// and return the file name
CString CDESDlg::getFilePathName(BOOL open,CString fileName)
{
	CString r,strHead,strFlt;
	
	strFlt="加密文件(*.ECP)|*.ECP|";
	strFlt+="文本及文档文件(*.txt,*.wps,*.doc,*.rtf)|*.txt;*.wps;*.doc;*.rtf|";
	strFlt+="网页文件(*.htm,*.html,*.asp)|*.htm;*.html;*.asp|";
	strFlt+="图型文件(*.bmp,*.jpg,*.jpeg,*.gif,*.tif,*.tga,*.pic)|*.bmp;*.jpg;*.jpeg;*.gif;*.tif;*.tga;*.pic|";
	strFlt+="可执行文件(*.exe,*.com)|*.exe;*.com|";
	strFlt+="所有文件(*.*)|*.*||";

	CFileDialog fd(open,"",fileName,
		OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,strFlt,this);	

	if(open=TRUE){		//get source file		
		strHead="请选择要加密或解密的源文件";
		if(((CButton *)GetDlgItem(IDC_RDOENCRYPTION))->GetCheck()==1){
			fd.m_ofn.nFilterIndex=2; // document file
		}else{			// do decryption
			fd.m_ofn.nFilterIndex=1; // *.ecp file,start from 1
			fd.m_ofn.lpstrDefExt="ECP";
		}
	}else{				// get destination file
		strHead="请选择加密或解密要生成的文件";
		if(((CButton *)GetDlgItem(IDC_RDOENCRYPTION))->GetCheck()==1){	//do ecnryption
			fd.m_ofn.lpstrDefExt="ECP";
		}else{			// do decryption
			fd.m_ofn.nFilterIndex=6;
		}
	}
	fd.m_ofn.lpstrTitle=strHead;

	if(fd.DoModal()==IDOK){
		r=fd.GetPathName();
		return r;
	}
	return "";
}

void CDESDlg::OnGetdestination() 
{
	m_strDestinationFile=getFilePathName(FALSE,m_strDestinationFile);	//P","","*.*|*.*||");
	
	if(!m_strDestinationFile.IsEmpty())
		UpdateData(FALSE);
	else
		UpdateData(TRUE);
	allReady();
}

void CDESDlg::OnBtngo() 
{
	CString strMsg;
	// TODO: Add your control notification handler code here
	if(((CButton *)GetDlgItem(IDC_RDOENCRYPTION))->GetCheck()==1){
		strMsg.Format("对文件进行数据加密?\n源文件:%s\n生成文件:%s",m_strSourceFile,m_strDestinationFile);
		if(MessageBox(strMsg,"请确认您的操作",MB_YESNO|MB_DEFBUTTON2|MB_ICONQUESTION)==IDYES){
			doEncryption();
		}
	}
	else if(((CButton *)GetDlgItem(IDC_RDODECRYPTION))->GetCheck()==1){
		strMsg.Format("对文件进行数据解密?\n源文件:%s\n生成文件:%s",m_strSourceFile,m_strDestinationFile);
		if(MessageBox(strMsg,"请确认您的操作",MB_YESNO|MB_DEFBUTTON2|MB_ICONQUESTION)==IDYES){
			doDecryption();
		}
	}
	else
		MessageBox("请选择加密或解密!","操作");
	
	//hide progress bar
	m_ctrlProgress.ShowWindow(SW_HIDE);
}

void CDESDlg::OnHidepassword() 
{
	// TODO: Add your control notification handler code here
	CButton *pBtn;

⌨️ 快捷键说明

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