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

📄 aidlg.cpp

📁 1、采用(1)盲目搜索(2)启发式搜索对凯撒密文进行自动解密。 2、对替代法加密的程序进行解密(未完成
💻 CPP
📖 第 1 页 / 共 2 页
字号:
{
	CString str;
	CString show;
	GetDlgItem(IDC_EDIT1)->GetWindowText(str);
	int length=str.GetLength();
	for(int i=0;i<length;i++)
	{
		if(str[i]<='z' && str[i]>='a'){
			show+=( (str[i]-'a'-add+26)%26 +'a' ); 	
		}
		else if(str[i]<='Z' && str[i]>='A'){
			show+=( (str[i]-'A'-add+26)%26 +'A' );
		}
		else
			show+=str[i];
	}
	return show;
}

int CAIDlg::Max(int *p)
{
	int max=0;
	for(int i=1;i<26;i++)
		max=(p[max]>p[i])? max:i;
	p[max]=-1;
	return max;
}


void CAIDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	GetDlgItem(IDC_EDIT1)->SetWindowText("");
	GetDlgItem(IDC_EDIT2)->SetWindowText("");
	GetDlgItem(IDC_EDIT3)->SetWindowText("");
	GetDlgItem(IDC_EDIT4)->SetWindowText("");
}


	//全局 关键定义  两个回溯
	CStringList pReback;//词库中匹配的单词,用来查找单词后 回溯"字母映射表"chPair[26]
	CStringList pSortWord; //密文单词长度的排序
	char chPair[26]; //**字母映射表
	CStringList pChooseWord;//被选中的单词,用来后一个单词找不到的时候  回溯
	//

void CAIDlg::OnButton4() 
{

	//1、得到字母的频率
	char chWord_E=CountTimes(); 
	
	//2、对单词排序pSortWord  分为2个步骤--先pAssignWord再pSortWord
	//单词按照长度分配pAssignWord
	CStringList pAssignWord[30];
	int len=0,i;
	CString temp="";
	POSITION pos;
	//chPair.Empty();
	if(GetWord())
	{
		//单词按照长度分配
		for( pos = pWordList.GetHeadPosition(); pos != NULL;pWordList.GetNext(pos)) 
		{
			temp=pWordList.GetAt(pos);
			temp.MakeUpper();//大写
			//必须有字母‘E’
			if( temp.Find(chWord_E) ){//必须有字母E
				len=temp.GetLength();
				pAssignWord[len].AddHead(temp);
			}
		}
		//单词按照长度从大到小排序
		for(i=1;i<30;i++)
		{
			if( !pAssignWord[i].IsEmpty() )
			{
				for( pos = pAssignWord[i].GetHeadPosition(); 
										pos != NULL;pAssignWord[i].GetNext(pos)) 
					pSortWord.AddHead(pAssignWord[i].GetAt(pos));
			}
		}
		for( pos = pSortWord.GetHeadPosition(); 
										pos != NULL;pSortWord.GetNext(pos)) 
		fout<<pSortWord.GetAt(pos)<<endl;
		//得到的pSortWord 都是大写 ,并且不会去掉重复。*****
	}
	//3、查找开始啦!哈哈哈
	CString csEnd;
	int iNotFind=0;
	int test=1;
	csEnd=pSortWord.GetHead();
	pSortWord.RemoveHead();
	//pChooseWord.AddTail(csEnd);
	iNotFind=TwoKindFind(csEnd,1);
	//while(test && !pSortWord.IsEmpty())
	while(!pSortWord.IsEmpty())
	{
		if(iNotFind==0 )  //没有找到
		{
			if(pChooseWord.IsEmpty()==0)  //pChooseWord空
			{
				csEnd=pSortWord.GetHead();
				pSortWord.RemoveHead();
				iNotFind=TwoKindFind(csEnd,1);
			}
			else
			{
				csEnd=pChooseWord.GetTail();
				iNotFind=TwoKindFind(csEnd,2);
			}
		}
		else
		{
				csEnd=pSortWord.GetHead();
				pSortWord.RemoveHead();
				iNotFind=TwoKindFind(csEnd,1);
		}
		/*
		//检查字母映射表是否可以了
		for(int aa=0;aa<26;aa++)
		{
			if(chPair[aa]>='A'&&chPair[aa]<='Z')
			{
				test=0;
				continue;
			}
			else
			{
				test=1;
				break;
			}
		}
		*/
	}

	//if(test==0)//字母映射表可以
	//{
		CString csAA,csBB;
		int BB, AA;
		GetDlgItem(IDC_EDIT1)->GetWindowText(csAA);		
		for(AA=0;AA<csAA.GetLength();AA++)
		{
			if( csAA[AA]>='A'&&csAA[AA]<='Z') 
			{
				BB=csAA[AA]-'A';
				csBB+=chPair[BB];
			}
			else if(csAA[AA]>='a'&&csAA[AA]<='z')
			{
				BB=csAA[AA]-'a';
				csBB+=( chPair[BB]+32);
			}
			else 
				csBB+=csAA[AA];
		}
		GetDlgItem(IDC_EDIT2)->SetWindowText(csBB);
		pReback.RemoveAll();
		pSortWord.RemoveAll();
		pChooseWord.RemoveAll();
		//chPair.Empty();
		memset(chPair,0,26);
//	}
//	else
	//	MessageBox("消息不足!");
		
}
	/*全局 关键定义  两个回溯
	CStringList pReback;//词库中匹配的单词,用来查找单词后 回溯"字母映射表"chPair[26]
	CStringList pSortWord; //密文单词长度的排序
	char chPair[26]; //**字母映射表
	CStringList pChooseWord;//被选中的单词,用来后一个单词找不到的时候  回溯
	*/

int CAIDlg::TwoKindFind(CString csWord,int flag)
{
	CString csNoChangeWord=csWord;
	csWord.MakeLower();//小写 用大小写来区分
	//1、下载映射表
	int length=csWord.GetLength();
	int j,n;
	char ch;
	for(j=0;j<26;j++)
	{
		if(chPair[j]<='Z' && chPair[j]>='A')//***前面必须设置为  小写
		{//不等0,就是有匹配到别的字母
			ch=j+'a';
			for(n=0;n<length;n++)
			{
				if(csWord[n]==ch)
					csWord.SetAt(n,chPair[j]);//小写改成大写
			}
		}
	}
	//2、查找匹配  返回是否找到
	POSITION pos;
	CString temp;
	int m;
	int isContinue=1;//不匹配
	//CString csGetReback=pReback.GetTail();
	CString csUploadRebackWord;
	POSITION pos2 = plist[length].GetHeadPosition();
	if(flag!=1)
	{
		CString csGetReback=pReback.GetTail();
		for(pos2 = plist[length].GetHeadPosition(); pos2 != NULL;plist[length].GetNext(pos2) ) 
		{
			temp=plist[length].GetAt(pos2);
			if(temp.Compare(csGetReback)==0)
			{
				//plist[length].GetNext(pos2);
				break;
			}
		}
	}
	if(plist[length].GetAt(pos2)==plist[length].GetTail())
		isContinue=1;
	else
	{	
		//如果第一次就直接开始
		for( pos =pos2; pos != NULL && isContinue==1;plist[length].GetNext(pos) ) 
		{
			temp=plist[length].GetAt(pos);
			for(m=0;m<length;m++)
			{
				if(csWord[m]<='Z' && csWord[m]>='A')  //大写的就那种匹配过的字符 
				{
					if( csWord[m]==temp[m] )//如果都可以就可以退出,
					{								//不要再继续了
						isContinue=0;
						csUploadRebackWord=temp;
						continue;
					}
					else
					{
						isContinue=1;
						csUploadRebackWord.Empty();
						break;
					}
				}
			}
		}
	}
	//看看有没有找到isContinue
	if(isContinue==0)//找到:1、第一次  2、非第一次
	{
		//检查是否有冲突 字母映射表
		int iChongTu=0;
		length=csWord.GetLength();
		//1、同一个字母(密文)被不同字母(明文)替换	
		for(j=0;j<length&&iChongTu==0;j++)
		{
			if(csWord[j]!=csUploadRebackWord[j]) //找到,但是,其他字符匹配和原来有冲突
			{
				for(int i=0;i<26;i++)
				{
					char abc=chPair[ csWord[j] ];
					if( (abc>='A'&&abc<='Z') &&abc!=csUploadRebackWord[j] )
					{
						iChongTu=1;
						break;
					}
				}
			}
		}
		if(iChongTu!=1)
		{
			//2、不同字母(密文)被同一个字母(明文)替换	
			char chPairCompare[26]; //假设用来上传的, 用来检查冲突
			strcpy(chPairCompare,chPair);
			for(j=0;j<length;j++)  //1、上传到 chPairCompare
			{
				if(csWord[j]!=csUploadRebackWord[j])
				{
					//chPair.SetAt(csWord[j],csUploadRebackWord[j] );
					chPairCompare[ csWord[j]-'a' ]=csUploadRebackWord[j] ;
				}
			}
			int ChongFu[26];
			memset(ChongFu,0,26);
			for(j=0;j<26;j++)      //2、 ChongFu纪录chPairCompare中字母出现的个数
			{
				if(chPairCompare[j]>='A'&&chPairCompare[j]<='Z')
					ChongFu[ chPairCompare[j]-'A' ]+=1;
			}
			for(j=0;j<26;j++)      //3、如果超过1次,就说明有不同字母(密文)被同一个字母(明文)替换
			{
				if(ChongFu[j]>1)
					iChongTu=1;
			}
		}
		//检查完毕
		if( iChongTu==0) //没有冲突
		{
			//没有冲突  才能上传字母映射表
			for(j=0;j<length;j++)
			{
				if(csWord[j]!=csUploadRebackWord[j]) //找到,但是,其他字符匹配和原来有冲突
				{
					//chPair.SetAt(csWord[j],csUploadRebackWord[j] );
					chPair[ csWord[j]-'a' ]=csUploadRebackWord[j] ;
				}
			}
			if(flag==1)//第一次
			{
				pReback.AddTail(csUploadRebackWord);	
				pChooseWord.AddTail(csNoChangeWord);
			}
			else  //非第一次 
			{
				pReback.RemoveTail();
				pReback.AddTail(csUploadRebackWord);
			}
			return 1;
		}
		else//有冲突
		{
			if(flag==1)//第一次
			{
				pReback.AddTail(csUploadRebackWord);
				pChooseWord.AddTail(csNoChangeWord);
				TwoKindFind(csNoChangeWord,2);
			}
			else 
			{
				pReback.RemoveTail();
				pReback.AddTail(csUploadRebackWord);
				TwoKindFind(csNoChangeWord,2);
			}
		}
	}
	else//没有找到
	{
		if(flag!=1)
		{
			pReback.RemoveTail();
			pChooseWord.RemoveTail();
		}
		return 0;
	}
}
	/*全局 关键定义  两个回溯
	CStringList pReback;//词库中匹配的单词,用来查找单词后 回溯"字母映射表"chPair[26]
	CStringList pSortWord; //密文单词长度的排序
	char chPair[26]; //**字母映射表
	CStringList pChooseWord;//被选中的单词,用来后一个单词找不到的时候  回溯
	*/
char CAIDlg::CountTimes()
{
	CString str="";
	GetDlgItem(IDC_EDIT1)->GetWindowText(str);
	if(str.IsEmpty()!=0)
	{
		MessageBox("请输入密文!");
		return 0;
	}
	else
	{
		str.MakeUpper();
		//ABCDE------Z
		//01234------25
		int i,j=0;
		int iWordTime[26];
		for(i=0;i<26;i++)
			iWordTime[i]=0;
		//memset(iWordTime,0,26);
		int length=str.GetLength();
		for(i=0;i<length;i++)
		{
			if( str[i]<='Z'&&str[i]>='A' )
			{
				j=str[i]-'A';
				iWordTime[j]++;	
			}
		}
		int e=Max(iWordTime);
		char chWord_E=e+'A'; //下标  //确定哪个字母被映射为E
		chPair[e]='E';  //先改变一个  哈
		//chPair.SetAt(e,'E');
		//chWord_E ->'E'
		return chWord_E;
	}
}

void CAIDlg::OnButton5() 
{
	GetDlgItem(IDC_EDIT4)->SetWindowText("");
	// TODO: Add your control notification handler code here
	CString chYingshe,chYingshe1;
	GetDlgItem(IDC_EDIT3)->GetWindowText(chYingshe1);
	if(chYingshe1.IsEmpty==0)
	{
		//return ;
		MessageBox("请在“字母映射表”输入“字母对应关系”");
	}
		//BCDEFGHIJKLMNOPQRSTUVWXYAZ
	//CString chYingshe("BCDEFGHIJKLMNOPQRSTUVWXYAZ");
	//除去非字母的东西
	chYingshe.Empty();
	for(int i=0;i<chYingshe1.GetLength();i++)
	{
		if (chYingshe1[i]<='z' && chYingshe1[i]>='a')
			chYingshe+=chYingshe1[i];
		else if (chYingshe1[i]<='Z' && chYingshe1[i]>='A')
			chYingshe+=chYingshe1[i];
	}
//int kk=chYingshe.GetLength();

//GetDlgItem(IDC_EDIT1)->SetWindowText(chYingshe.GetLength());
	
	if(chYingshe.GetLength()!=26)
	{
	
		AfxMessageBox("字母不够26个!");
		return ;
	}
	//检查字母中是否一一对应
	char temp;
	int iTimes=0;
	chYingshe.MakeUpper();
	for(i=0;i<chYingshe.GetLength();i++)
	{
		iTimes=0;
		temp=chYingshe[i];
		for(int j=0;j<chYingshe.GetLength();j++)
		{
			if(temp==chYingshe[j])
				iTimes+=1;
		}
		if(iTimes!=1)
		{
	//		return ;
			MessageBox("字母没有一一对应!");
			return ;
		}
	}


	
	CString str;
	GetDlgItem(IDC_EDIT2)->GetWindowText(str);
	int length=str.GetLength();
	if(length==0)
	{
		MessageBox("请在“明文输入框”中输入明文!");
		return ;
	}
	else
	{
		CString output;	
		int n;
		for(int i=0;i<length;i++)
		{
			if (str[i]<='z' && str[i]>='a') 
			{
				n=str[i]-'a';
				output+=chYingshe[n]+32;
			}
			else if(str[i]<='Z' && str[i]>='A')
			{
				n=str[i]-'A';
				output+=chYingshe[n];
			}
			else
				output+=str[i];
		}
		GetDlgItem(IDC_EDIT1)->SetWindowText(output);
	}
}

⌨️ 快捷键说明

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