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

📄 desdlg.cpp

📁 基于DES加密算法
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	CEdit   *pPwd,*pCfm;
	
	pBtn=(CButton *)GetDlgItem(IDC_HIDEPASSWORD);
	pPwd=(CEdit *)GetDlgItem(IDC_EDTPASSWORD);
	pCfm=(CEdit *)GetDlgItem(IDC_EDTCONFIRMPASSWORD);
	
	pPwd->SetPasswordChar(pBtn->GetCheck()==1 ? '*' : '\0');
	pCfm->SetPasswordChar(pBtn->GetCheck()==1 ? '*' : '\0');
	pPwd->Invalidate();
	pCfm->Invalidate();
}

void CDESDlg::OnKillfocusEdtconfirmpassword() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);		//CTRL =>STRING
	if(!samePwd())
		MessageBox("警告:  输入密码与确认密码不一致!!","数据加密");
	else
		allReady();
}

BOOL CDESDlg::samePwd(void )
{
	if(m_strPassword.Compare(m_strPassword2))
		return FALSE;
	else
		return TRUE;
}

BOOL CDESDlg::allReady(void)
{
	GetDlgItem(IDC_BTNGO)->EnableWindow(FALSE);

	UpdateData();
	if(m_strPassword.IsEmpty()) return FALSE;

	if(!samePwd()) return FALSE;		// diffrent password
	
	if(m_strSourceFile.IsEmpty()|| m_strDestinationFile.IsEmpty() )
		return FALSE;

	GetDlgItem(IDC_BTNGO)->EnableWindow(TRUE);
	return TRUE;
}

void CDESDlg::OnChangeEdtsource() 
{
	// 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
	allReady();	
}

void CDESDlg::OnChangeEdtdestination() 
{
	// 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
	allReady();
	
}

// encryption source file and write to destination file
// first, show progress bar, allocate memory for reading/writing
//      buffer, if fail to allocate, return FALSE.
// second, write original file information, also include 
//     encryption file ID( a string), CRC, algorithm number
//     file information
// and third, do encryption. 

BOOL CDESDlg::doEncryption(void)
{
	CFile flSrc,flDst;
	CFileStatus flStatus;
						//CFileException fe;	
	CString strMsg;
	unsigned int crcCode,i;
	char *pBuf=NULL;	// pointer to memory buffer
	char cZero=0;
	unsigned char algorithmNum,subAlgorithmNum;
	UINT nIDStringLength;

	// allocate memory
	if((pBuf=(char *)malloc(BUFFERSIZE))==NULL){	// allcate buffer
		MessageBox("分配内存失败!\n\n请关闭其它正在运行的程序,然后再试!","加密");
		return FALSE;
	}

	//test source exist
	if(!CFile::GetStatus(m_strSourceFile,flStatus)){
		MessageBox("源文件不存在!","数据加密",
			MB_ICONASTERISK|MB_OK);
		if(pBuf) free(pBuf);
		return FALSE;
	}

	m_ctrlProgress.ShowWindow(SW_SHOW);		// SHOW PROGRESS BAR
	m_ctrlProgress.SetRange32(0,flStatus.m_size);// set range
	m_ctrlProgress.SetPos(0);
	m_ctrlProgress.UpdateWindow();

	//open source file and destination file
	if(!flSrc.Open(m_strSourceFile,CFile::modeRead|CFile::typeBinary)){
		MessageBox("无法打开源文件!","数据加密",MB_OK|MB_ICONEXCLAMATION);
		if(pBuf) free(pBuf);
		return FALSE;
	}
	if(!flDst.Open(m_strDestinationFile,CFile::modeWrite|CFile::typeBinary|CFile::modeCreate)){
		MessageBox("无法创建生成的文件!","数据加密",MB_OK|MB_ICONEXCLAMATION);
		if(pBuf) free(pBuf);
		return FALSE;
	}

	//write head information to  destination file
	// 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 backup))
	//(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)

	try{
		nIDStringLength=strlen(ID_STRING);		// ID String
		flDst.Write(ID_STRING,nIDStringLength);
		for(i=nIDStringLength;i<MAXIDSTRINGSIZE;i++)
			flDst.Write(&cZero,1);
		
		for(i=0;i<12;i++)			//write CRC (fake)
			flDst.Write(&cZero,1);
	
		//Encryption algrithom number
		algorithmNum=1;subAlgorithmNum=0;
		flDst.Write(&algorithmNum,1);
		flDst.Write(&subAlgorithmNum,1);
		
		// write original file information block length
		WORD nFileStatusSize=sizeof(CFileStatus);
		flDst.Write(&nFileStatusSize,2);
		
		//write original file information
		flDst.Write(&flStatus,sizeof(CFileStatus));
	}catch(CException *e){
		char msgErr[1024];
		e->GetErrorMessage(msgErr,1024);
		e->Delete();
		MessageBox(msgErr,"加密");
		if(pBuf!=NULL)
			free(pBuf);
		return FALSE;
	}
		
	//Next do encryption and write to destination file:::
	//first read a block from source to buffer
	//then calculate crc.
	//next do encryption of buffer block	
	//next write to destination file
	//next update progress bar
	//Note: if rest of the file is less than buffer size,
	//    that means the end of the file reached, then set
	//    the size to 8*X bytes to make encryption correct.
	UINT nBytesRead;
	crc.resetCRC();
	des.setKey(m_strPassword.GetBuffer(m_strPassword.GetLength()+1));	// add 1 for the NULL terminator
	try{
		// do next loop to encryption a file by process
		//		a block once, and one by one
		while((nBytesRead=flSrc.Read((void *)pBuf,BUFFERSIZE))>0){
			//calculate CRC
			crcCode=crc.calculateCRC((void *)pBuf,nBytesRead);
			
			//set block to 8*X bytes
			if(nBytesRead%8){	// not 8*x bytes
				nBytesRead=((nBytesRead+7)/8)*8;
			}
			//do encryption
			if(!des.encryption((void *)pBuf,nBytesRead)){
				MessageBox("加密运算出错!","加密");
				free(pBuf);
				return FALSE;
			}

			// write to destination file
			flDst.Write((void *)pBuf,nBytesRead);

			//update progresss bar
			m_ctrlProgress.SetPos(m_ctrlProgress.GetPos()+nBytesRead);
			//this->Invalidate();
			this->UpdateWindow();
		}

		//NEXT STEP: write crc
		flDst.Seek(MAXIDSTRINGSIZE,CFile::begin);
		for(i=0;i<3;i++)			//write three crcs
			flDst.Write(&crcCode,4);
	}catch(CException *e){
		char strErr[1024];
		e->GetErrorMessage(strErr,1024);
		MessageBox(strErr,"错误");
		e->Delete();
		return FALSE;
	}
	
	//NEXT STEP: free memory
	if(pBuf)	free((void *)pBuf);

	MessageBox("顺利完成文件加密!\n请记住您所用的密码,否则将无法解密!","加密");
	return TRUE;
}


// to decryption a file
BOOL CDESDlg::doDecryption(void)
{
	CFile flSrc,flDst;
	CFileStatus flStatus;
						//CFileException fe;	
	CString strMsg;
	char *pBuf=NULL;	// pointer to memory buffer
	unsigned int crcCode;
	//unsigned char algorithmNum,subAlgorithmNum;
	
	//test source exist
	if(!CFile::GetStatus(m_strSourceFile,flStatus)){
		MessageBox("源文件不存在!","解密",
			MB_ICONASTERISK|MB_OK);
		return FALSE;
	}
	if(!processECPFile(m_strSourceFile)){
		MessageBox("源文件不是可识别的加密文件!","解密",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;
	}

	m_ctrlProgress.ShowWindow(SW_SHOW);		// SHOW PROGRESS BAR
	m_ctrlProgress.SetRange32(0,flStatus.m_size);
	m_ctrlProgress.SetPos(0);
	m_ctrlProgress.UpdateWindow();

	if(!m_CRCOK){
		MessageBox("警告!\n循环冗余校验码(CRC)与备份不一致!","解密",MB_OK|MB_ICONEXCLAMATION);
	}	

	//open source file and destination file
	if(!flSrc.Open(m_strSourceFile,CFile::modeRead|CFile::typeBinary)){
		MessageBox("无法打开源文件!","解密",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;
	}
	if(!flDst.Open(m_strDestinationFile,CFile::modeWrite|CFile::typeBinary|CFile::modeCreate)){
		MessageBox("无法创建生成的文件!","解密",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;
	}
	
	// allocate memory
	if((pBuf=(char *)malloc(BUFFERSIZE))==NULL){	// allcate buffer
		MessageBox("分配内存失败!\n\n请关闭其它正在运行的程序,然后再试!","解密");
		return FALSE;
	}

	// do something acroding to the version number
	// but this version do nothing

	//NEXT STEP : decryption the file
	//(1) read a block from source to buffer
	//(2) decrypte block
	//(3) calculate CRC
	//(4) write to destination file
	//(5) update progress bar

	flSrc.Seek(m_ECPFileStart,CFile::begin);
	UINT nBytesRead,nTotalBytes=0;
	crc.resetCRC();
	des.setKey(m_strPassword.GetBuffer(m_strPassword.GetLength()+1));	// add 1 for the NULL terminator
	try{
		// do next loop to decryption a file by process
		//		a block once, and one by one
		while((nBytesRead=flSrc.Read((void *)pBuf,BUFFERSIZE))>0){
			nTotalBytes+=nBytesRead;		// add totol bytes readed
			
			//do encryption
			if(!des.decryption((void *)pBuf,nBytesRead)){
				MessageBox("解密出错!\n文件长度不为 8 的整数倍!","解密");
				if(pBuf) free(pBuf);
				return FALSE;
			}

			// get correct length
			if(nTotalBytes>(UINT)m_fileStatus.m_size){
				nBytesRead-=(nTotalBytes - m_fileStatus.m_size);
			}
			
			//calculate CRC
			crcCode=crc.calculateCRC((void *)pBuf,nBytesRead);
			
			// write to destination file
			flDst.Write((void *)pBuf,nBytesRead);
			
			//update progresss bar
			m_ctrlProgress.SetPos(m_ctrlProgress.GetPos()+nBytesRead);
//			this->Invalidate();
			this->UpdateWindow();
		}// while(..)
	}catch(CException *e){
		char strErr[1024];
		e->GetErrorMessage(strErr,1024);
		e->Delete();
		MessageBox(strErr,"错误");		
		if(pBuf) free(pBuf);		// free memory
		return FALSE;
	}
	
	//set orginal file status, include time,date, attribute
	flDst.Close();
	CFileStatus fsOrg;
	CFile::GetStatus(m_strDestinationFile,fsOrg);
	fsOrg.m_atime=m_fileStatus.m_atime;
	fsOrg.m_attribute=m_fileStatus.m_attribute;
	fsOrg.m_ctime=m_fileStatus.m_ctime;
	fsOrg.m_mtime=m_fileStatus.m_mtime;
	CFile::SetStatus(m_strDestinationFile,fsOrg);

	//NEXT STEP: free memory
	if(pBuf) free((void *)pBuf);
	
	//check crc is ok and report
	if(crcCode!=m_CRC){
		MessageBox("解密已完成,但循环冗余校验 (CRC) 与原始值不吻合!\n可能是密码不正确,或者是加密文件被更改过","解密");
	}else
		MessageBox("顺利完成文件解密!","解密");

	return TRUE;
}

void CDESDlg::OnChangeEdtconfirmpassword() 
{
	// 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
	allReady();
}

void CDESDlg::OnChangeEdtpassword() 
{
	// 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
	allReady();
	
}

⌨️ 快捷键说明

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