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

📄 aesdlg.cpp

📁 AES加解密算法实现
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	OnSetkey128();	
}

//选择明文
void CAESDlg::OnSBrowse() 
{
	// TODO: Add your control notification handler code here
	static char BASED_CODE szFilter[] = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*||";
	int result;
	//
	CFileDialog dlg(true, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, NULL);
	if (dlg.DoModal()==IDOK)
	{
		sflag	=	1;
		editflag	=	0;
		showflag	=	1;
		//
		ClearAll(0);
		//
		strcpy(sfilename, dlg.GetPathName());
		m_sfile.SetWindowText(sfilename);
		m_sfile.UpdateWindow();		
		//读入
		result	=	copyfile(sfilename, SFILENAME);		
		//
		OnSShowText();		
	}	
}

//选择密文
void CAESDlg::OnRBrowse() 
{
	// TODO: Add your control notification handler code here	
	static char BASED_CODE szFilter[] = "All Files (*.*)|*.*||";
	int result;
	FILE* handle;
	int filelen;
	//
	CFileDialog dlg(true, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, NULL);
	if (dlg.DoModal()==IDOK)
	{
		rflag	=	1;
		showflag	=	1;
		//
		ClearAll(0);
		//
		strcpy(rfilename, dlg.GetPathName());		
		//		
		handle	=	fopen(rfilename, "rb");
		filelen	=	filelength(fileno(handle));
		result	=	fclose(handle);
		if (filelen==0 || filelen%16!=0)
		{	
			show("密文长度不对");
			rflag	=	0;
			return;	
		}
		//
		m_rfile.SetWindowText(rfilename);
		m_rfile.UpdateWindow();	
		//读入
		result	=	copyfile(rfilename, RFILENAME);		
		//
		OnRShowText();
	}
}

//输出明文
void CAESDlg::OnSOutput() 
{
	// TODO: Add your control notification handler code here
	static char BASED_CODE szFilter[] = "All Files (*.*)|*.*||";	
	int result;
	//
	if (sflag==0)
	{
		show("请先选择明文文件或输入明文");
		return;
	}
	//	
	CFileDialog dlg(false, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, NULL);	
	if (dlg.DoModal()==IDOK)
	{
		strcpy(soutfile, dlg.GetPathName());
		//
		result	=	copyfile(SFILENAME, soutfile);
		//
		show("明文输出完成");
	}
}

//输出密文
void CAESDlg::OnROutput() 
{
	// TODO: Add your control notification handler code here
	static char BASED_CODE szFilter[] = "All Files (*.*)|*.*||";	
	int result;
	//
	if (rflag==0)
	{
		show("请先选择密文文件");
		return;
	}
	//
	CFileDialog dlg(false, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, NULL);	
	if (dlg.DoModal()==IDOK)
	{
		strcpy(routfile, dlg.GetPathName());
		//
		result	=	copyfile(RFILENAME, routfile);
		//
		show("密文输出完成");
	}	
}

//加密明文
void CAESDlg::OnEncrypt() 
{
	// TODO: Add your control notification handler code here
	int i;
	int filelen;
	int pos;
	FILE* handle;
	FILE* handle2;
	int firstcount;
	int result;
	//
	if (sflag==0)
	{
		show("请先选择明文文件或输入明文");
		return;
	}
	//准备工作
	m_rfile.SetWindowText("");
	m_rtext.SetWindowText("");	
	//
	handle	=	fopen(SFILENAME, "rb");	
	if (handle==NULL)
	{
		show("非法明文文件");		
		return;
	}
	filelen	=	filelength(fileno(handle));
	if (filelen==0)
	{
		show("明文为空");
		fclose(handle);
		return;
	}
	handle2	=	fopen(RFILENAME, "wb");
	//
	setposinit(0, filelen);
	pos	=	0;
	firstcount	=	0;
	while (pos<filelen)
	{
		memset(stext, 0, TEXT_MAX+1);
		scount	=	fread(stext, 1, TEXT_MAX, handle);
		if (scount%16!=0)
			scount	=	scount+16-scount%16;
		if (firstcount==0)
			firstcount	=	scount;
		pos	+=	scount;
		//加密工作:stext->rtext
		for (i=0; i<scount; i+=16)
		{
			memcpy(s_blk, stext+i, 16);
			encrypt(s_blk, r_blk);
			memcpy(rtext+i, r_blk, 16);
		}
		//
		result	=	fwrite(rtext, 1, scount, handle2);
		//
		setpos(pos);			
	}
	result	=	fclose(handle);
	result	=	fclose(handle2);
	//
	scount	=	firstcount;
	rcount	=	scount;
	rflag	=	1;
	showflag	=	1;
	//显示结果
	OnRShowText();
}

//解密密文
void CAESDlg::OnDecrypt() 
{
	// TODO: Add your control notification handler code here
	int i;
	int filelen;
	int pos;
	FILE* handle;
	FILE* handle2;
	int firstcount;
	int result;
	//
	if (rflag==0)
	{
		show("请先选择密文文件");
		return;
	}
	//准备工作
	m_sfile.SetWindowText("");
	m_stext.SetWindowText("");
	//
	handle	=	fopen(RFILENAME, "rb");	
	if (handle==NULL)
	{
		show("非法密文文件");		
		return;
	}
	filelen	=	filelength(fileno(handle));
	if (filelen==0 || filelen%16!=0)
	{
		show("密文长度不对");
		fclose(handle);
		return;
	}
	handle2	=	fopen(SFILENAME, "wb");
	//
	setposinit(0, filelen);
	pos	=	0;
	firstcount	=	0;
	while (pos<filelen)
	{
		memset(rtext, 0, TEXT_MAX+1);
		rcount	=	fread(rtext, 1, TEXT_MAX, handle);
		//
		if (firstcount==0)
			firstcount	=	rcount;
		pos	+=	rcount;
		//解密工作:rtext->stext
		for (i=0; i<rcount; i+=16)
		{
			memcpy(r_blk, rtext+i, 16);
			decrypt(r_blk, s_blk);
			memcpy(stext+i, s_blk, 16);
		}
		//
		result	=	fwrite(stext, 1, rcount, handle2);
		//
		setpos(pos);			
	}
	result	=	fclose(handle);
	result	=	fclose(handle2);
	//显示结果
	rcount	=	firstcount;
	scount	=	rcount;
	sflag	=	1;
	showflag	=	1;
	OnSShowText();
}

//显示明文
//用文本方式,将ASC的0转换为空格来显示
void CAESDlg::OnSShowText() 
{
	// TODO: Add your control notification handler code here	
	int i;	
	FILE* handle;
	int result;
	//
	memset(stext, 0, TEXT_MAX+1);
	handle	=	fopen(SFILENAME, "rb");
	if (handle==NULL)
	{
		show("非法明文文件");		
		return;
	}
	scount	=	fread(stext, 1, TEXT_MAX, handle);
	result	=	fclose(handle);
	//处理	
	for (i=0; i<scount; i++)
	{
		if (stext[i]==0)
			stext2[i]	=	' ';
		else
			stext2[i]	=	stext[i];
	}
	stext2[scount]	=	0;
	//
	m_stext.SetWindowText(stext2);
	m_stext.UpdateWindow();
}

//显示密文
//用01方式,不用文本方式
void CAESDlg::OnRShowText() 
{
	// TODO: Add your control notification handler code here
	int i,j;
	int p;
	FILE* handle;
	int result;
	const int mark[9]	=	{0, 128, 64, 32, 16, 8, 4, 2, 1};
	unsigned char c1,c2,c3;	
	//
	memset(rtext, 0, TEXT_MAX+1);		
	handle	=	fopen(RFILENAME, "rb");
	if (handle==NULL)
	{
		show("非法密文文件");		
		return;
	}
	rcount	=	fread(rtext, 1, TEXT_MAX, handle);		
	result	=	fclose(handle);
	//处理	
	p	=	0;
	for (i=0; i<rcount; i++)
	{
		for (j=1; j<=8; j++)
		{
			c1	=	rtext[i];
			c2	=	mark[j];
			c3	=	c1&c2;
			if (c3==0) 
				rtext2[p]	=	'0';
			else
				rtext2[p]	=	'1';
			p++;
		}
	}
	rtext2[p]	=	0;
	//
	m_rtext.SetWindowText(rtext2);
	m_rtext.UpdateWindow();
}

//清除明文
void CAESDlg::OnSClear() 
{
	// TODO: Add your control notification handler code here
	memset(stext, 0, TEXT_MAX+1);
	memset(stext2, 0, TEXT_MAX+1);
	DeleteFile(SFILENAME);
	//
	sflag	=	0;	
	editflag	=	0;
	showflag	=	1;
	//
	m_sfile.SetWindowText("");
	m_sfile.UpdateWindow();
	//
	m_stext.SetWindowText("");
	m_stext.UpdateWindow();
	//	
	m_sshow.SetWindowText("");
	m_rshow.SetWindowText("");
	m_time.SetWindowText("");		
	m_progress.SetRange32(0, 100);
	m_progress.SetPos(0);
}

void CAESDlg::OnRClear() 
{
	// TODO: Add your control notification handler code here
	//
	memset(rtext, 0, TEXT_MAX+1);
	memset(rtext2, 0, 8*TEXT_MAX+1);
	DeleteFile(RFILENAME);
	//
	rflag	=	0;
	showflag	=	1;
	//
	m_rfile.SetWindowText("");
	m_rfile.UpdateWindow();
	//
	m_rtext.SetWindowText("");
	m_rtext.UpdateWindow();
	//	
	m_sshow.SetWindowText("");
	m_rshow.SetWindowText("");
	m_time.SetWindowText("");		
	m_progress.SetRange32(0, 100);
	m_progress.SetPos(0);
}


void CAESDlg::OnStop() 
{
}

void CAESDlg::show(char *str)
{
	MessageBox(str, "信息", MB_OK|MB_ICONINFORMATION);
}

//显示进度
void CAESDlg::setpos(int pos)
{
	char temp[128];
	int i;	
	//
	if (showflag==1 || rand()%NUM_RAND1==0)
	{		
		etime	=	GetTickCount()-stime;
		sprintf(temp, "%i秒%i毫秒", etime/1000, etime%1000);
		//
		m_time.SetWindowText(temp);
		m_time.UpdateWindow();
	}
	//
	if (showflag==1 || rand()%NUM_RAND2==0)
	{
		for (i=SHOW_MAX-1; i>0; i--)
		{
			sshow[i]	=	sshow[i-1];
			rshow[i]	=	rshow[i-1];
		}
		sshow[0]	=	rand()%2+'0';
		rshow[0]	=	rand()%2+'0';
		//
		m_sshow.SetWindowText(sshow);
		m_sshow.UpdateWindow();
		//
		m_rshow.SetWindowText(rshow);
		m_rshow.UpdateWindow();
	}
	//
	showflag	=	0;
	m_progress.SetPos(pos);	
}

void CAESDlg::setposinit(int a, int b)
{
	int	i;
	//
	m_progress.SetRange32(a,b);
	m_progress.SetPos(0);
	//
	for (i=0; i<SHOW_MAX; i++)
	{
		sshow[i]	=	rand()%2+'0';
		rshow[i]	=	rand()%2+'0';
	}
	sshow[SHOW_MAX]	=	0;
	rshow[SHOW_MAX]	=	0;
	//
	stime	=	GetTickCount();
}

void CAESDlg::OnSetkey128() 
{
	// TODO: Add your control notification handler code here
	keylen	=	128;
	memcpy(key, (char*)key128, keylen);	
	OnShowKey();
	//
	set_key(key128, keylen);
}

void CAESDlg::OnSetkey192() 
{
	// TODO: Add your control notification handler code here
	keylen	=	192;
	memcpy(key, (char*)key192, keylen);		
	OnShowKey();
	//
	set_key(key192, keylen);
}

void CAESDlg::OnSetkey256() 
{
	// TODO: Add your control notification handler code here	
	keylen	=	256;
	memcpy(key, (char*)key256, keylen);	
	OnShowKey();
	//
	set_key(key256, keylen);
}

void CAESDlg::OnShowKey()
{	
	char skey[KEY_MAX+128];
	int p;
	int i;
	//
	p	=	0;
	for (i=0; i<keylen; i++)
	{		
		skey[p]	=	key[i]+'0';
		p++;
		if ((i+1)%64==0)
		{
			skey[p]	=	'\n';
			p++;
		}		
	}
	skey[p]	=	0;
	//
	m_key.SetWindowText(skey);
	m_key.UpdateWindow();	
}

//将用户输入的明文输出到文件中
void CAESDlg::OnKillfocusEdit3() 
{
	// TODO: Add your control notification handler code here
	FILE* handle;
	int len;
	char* buf;
	int result;
	//
	len	=	m_stext.GetWindowTextLength();
	buf	=	(char*)malloc(len+1);
	//
	result	=	m_stext.GetWindowText(buf, len+1);
	handle	=	fopen(SFILENAME, "wb");
	result	=	fwrite(buf, 1, len, handle);
	result	=	fclose(handle);
	//
	free(buf);
	//
	sflag	=	1;
	showflag	=	1;
}

void CAESDlg::OnChangeEdit3() 
{
	editflag	=	1;
}

void CAESDlg::OnOK() 
{
	// TODO: Add extra validation here
	CDlgEndBox dlg;
	dlg.DoModal();
	//
	CDialog::OnOK();
}


void CAESDlg::OnShowVersion() 
{
	// TODO: Add your control notification handler code here
	CDlgVersion dlg;
	dlg.DoModal();
}

int CAESDlg::copyfile(char *sfilename, char *dfilename)
{
	int result;
	//
	CDlgWait dlg;
	result	=	dlg.Create(IDD_WAIT);
	result	=	dlg.ShowWindow(SW_SHOWNORMAL);
	//
	result	=	CopyFile(sfilename, dfilename, FALSE);
	//
	result	=	dlg.DestroyWindow();
	//
	return 0;
}


⌨️ 快捷键说明

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