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

📄 caesardedlg.cpp

📁 1、对于凯撒密文
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			isCorrect = false;
			break;				//error
		}
		iStart = iEnd + 1;
	}

//	iEnd = str.Find(' ',0);		//find the first space	
	
//	if(iEnd <0 && iStrLen >0)
//	{
//		iEnd = iStrLen;			//This is for the special case when there is only one word
//	}
/*	
	while ( iEnd < iStrLen - 1)
	{
		
		strRes = str.Mid(iStart,iEnd-iStart); //Extract the word
		
		if(m_pDictionary->IsWordListed(strRes)==3 
			|| m_pDictionary->IsWordListed(strRes)==4)		//check for spelling
		{
			isCorrect = false;
			break;				//error
		}
		iStart = iEnd;
		while ( iStart < iStrLen - 1 && !( ( str.GetAt(iStart) >= 'a' && str.GetAt(iStart) <= 'z' )
		|| ( str.GetAt(iStart) >= 'A' && str.GetAt(iStart) <= 'Z' ) ) 
		)
		iStart ++;
		iEnd = iStart;
		while ( ( ( str.GetAt(iEnd) >= 'a' && str.GetAt(iEnd) <= 'z' )
		|| ( str.GetAt(iEnd) >= 'A' && str.GetAt(iEnd) <= 'Z' ) ) 
		&& iEnd < iStrLen - 1 )
		iEnd ++;
	}
	if ( iStart < iStrLen - 1 )
	{
		strRes = str.Mid(iStart,iStrLen-1);		// get the last word
		if(m_pDictionary->IsWordListed(strRes)!=0)
		{
			isCorrect = false;
		}
	}*/
/*	WordMatch match = m_pDictionary.IsWordListed(m_cipher);
	CString str;

        switch (match)
        {
        case eMatchPerfect:
            str = m_cipher + " is a valid word";
            break;

        case eMatchCapitalisedFirst:
            str = m_cipher + " is a valid word, but with capitalised first letter";
            break;

        case eMatchMixedCase:
            str = m_cipher + " is not valid due to incorrect case. " ;
            break;

        case eMatchNone:
            str = m_cipher + " is not valid.";
            break;

        case eMatchInternalError:
            str = m_cipher + " caused an internal error";
            break;
        }*/
	return isCorrect;
}

void CCaesarDeDlg::ShowSub(int shift)			//显示替换表,即密钥
{
/*	CString str;
	char ch;
	this->GetDlgItemText(IDC_SUBA,str);
	ch = str.GetAt(0);*/
	CString strSub = _T("");
	strSub.Format("%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c " ,
		( 0 + shift ) % 26 + 'A' ,
		( 1 + shift ) % 26 + 'A' ,
		( 2 + shift ) % 26 + 'A' ,
		( 3 + shift ) % 26 + 'A' ,
		( 4 + shift ) % 26 + 'A' ,
		( 5 + shift ) % 26 + 'A' ,
		( 6 + shift ) % 26 + 'A' ,
		( 7 + shift ) % 26 + 'A' ,
		( 8 + shift ) % 26 + 'A' ,
		( 9 + shift ) % 26 + 'A' ,
		( 10 + shift ) % 26 + 'A' ,
		( 11 + shift ) % 26 + 'A' ,
		( 12 + shift ) % 26 + 'A' ,
		( 13 + shift ) % 26 + 'A' ,
		( 14 + shift ) % 26 + 'A' ,
		( 15 + shift ) % 26 + 'A' ,
		( 16 + shift ) % 26 + 'A' ,
		( 17 + shift ) % 26 + 'A' ,
		( 18 + shift ) % 26 + 'A' ,
		( 19 + shift ) % 26 + 'A' ,
		( 20 + shift ) % 26 + 'A' ,
		( 21 + shift ) % 26 + 'A' ,
		( 22 + shift ) % 26 + 'A' ,
		( 23 + shift ) % 26 + 'A' ,
		( 24 + shift ) % 26 + 'A' ,
		( 25 + shift ) % 26 + 'A' );
	this->SetDlgItemText(IDC_SUB,strSub);
}

int CCaesarDeDlg::CountMost(CString & str)	//计算个数最多的字符 
{
	int length = str.GetLength();
	int count[150];
	for ( int i = 'a' ; i <= 'z' ; i ++ ) count[i]=0;
	int max = 0;
	int most = 0;
	for ( i = 0 ; i < length ; i ++ )
	{
		if ( str.GetAt(i) >= 'a' && str.GetAt(i) <= 'z' )
			count[str.GetAt(i)]++;
		else if ( str.GetAt(i) >= 'A' && str.GetAt(i) <= 'Z' )
			count[str.GetAt(i)-'A'+'a']++;
	}
	for ( i = 'a' ; i <= 'z' ; i ++ )
	{
		if ( count[i] > max )
		{
			max = count[i];
			most = i;
		}
	}
	return most;
}


void CCaesarDeDlg::OnRadioM() 
{
	// TODO: Add your control notification handler code here
	m_iMethod = 2;
	m_wndTaskbarNotifier->Show("无敌概率式:依照e,t,i,a,o...可能为最多字母的顺序进行匹配,使用此法最好应保证出现的词都在词库内,否则,以最多字母为e显示在明文框内");
}

void CCaesarDeDlg::DecipherM()//频率改进式
{
		// TODO: Add your control notification handler code here
	UpdateData();
	char alphaFreq[27] = "etiaonsrhcldpyumfbgwvkxqzj";
	int m_matched = 0;
	int m_matchedMost = 0;
	int m_finalChoice = 0;
	m_plainString = _T("");						//将明文置空
	int length = m_cipherString.GetLength();	//得到密文长度
//	int isCorrect = false;						//初始化“是否正确”标签
	CString original_cipher = m_cipherString;	//原始的密文,不变的
	CString str = m_cipherString;				//临时替换
	int most = CountMost ( m_cipherString );	//算哪个字母最多
	int diff = 26 - ( most - alphaFreq[cont] );	//初始化第一个最多的字母为'e'
	if ( str != _T("") )
	{
		for ( int i = 0 ; i < 26 ; i ++ )	//遇到isCorrect=true就跳出
		{
			diff = 26 - ( most - alphaFreq[i] );
			str = original_cipher;				//初始化str变量
			LPTSTR p = str.GetBuffer(length);	//将CString变为字符数组,便于操作
			for ( int j = 0 ; j < length ; j ++ )
			{
				if ( p[j] >= 'A' && p[j] <= 'Z' )	//密文字母 -> 密文字母 + cont
					p[j] = ( p[j] - 'A' + (diff%26) ) % 26 + 'A';
				else if ( p[j] >= 'a' && p[j] <= 'z' )
					p[j] = ( p[j] - 'a' + (diff%26) ) % 26 + 'a';
				else
					continue;
			}
			m_matched = CountString(str);		//判断移位后的密文是否所有单词都能找到
			if ( m_matched >= m_matchedMost )
			{
				ShowTime();
				m_matchedMost = m_matched;
				m_finalChoice = diff;
				GetDlgItem(IDC_PLAIN)->SetWindowText(str);
				UpdateWindow();
				str.ReleaseBuffer();
				ShowSub(26-diff%26);					//显示对应关系,即密钥
			}
		}
/*		str = original_cipher;				//初始化str变量
		LPTSTR p = str.GetBuffer(length);	//将CString变为字符数组,便于操作
		for ( int j = 0 ; j < length ; j ++ )
		{
			if ( p[j] >= 'A' && p[j] <= 'Z' )	//密文字母 -> 密文字母 + cont
				p[j] = ( p[j] - 'A' + (m_finalChoice%26) ) % 26 + 'A';
			else if ( p[j] >= 'a' && p[j] <= 'z' )
				p[j] = ( p[j] - 'a' + (m_finalChoice%26) ) % 26 + 'a';
			else
				continue;
		}
		str.ReleaseBuffer();
		GetDlgItem(IDC_PLAIN)->SetWindowText(str);
		ShowSub(26-m_finalChoice%26);				//显示最后结果*/
	}
}

int CCaesarDeDlg::CountString(CString &str)
{
	CString strRes;
	int cont = 0;
	int iStart = 0,iEnd =0, iLen=0, iStrLen;
	bool isCorrect = true;
	iStrLen = str.GetLength();
	while ( iEnd < iStrLen )
	{
		while ( iStart < iStrLen 
			&& !( ( str.GetAt(iStart) >= 'a' && str.GetAt(iStart) <= 'z' )
			|| ( str.GetAt(iStart) >= 'A' && str.GetAt(iStart) <= 'Z' ) ) )
			iStart ++;
		if (iStart == iStrLen)
			break;
		iEnd = iStart;
		while ( iEnd < iStrLen
			&& ( ( str.GetAt(iEnd) >= 'a' && str.GetAt(iEnd) <= 'z' )
			|| ( str.GetAt(iEnd) >= 'A' && str.GetAt(iEnd) <= 'Z' ) ) )
			iEnd ++;
		strRes = str.Mid(iStart,iEnd-iStart); //Extract the word
		if(m_pDictionary->IsWordListed(strRes)==eMatchPerfect 
			|| m_pDictionary->IsWordListed(strRes)==eMatchCapitalisedFirst
			|| m_pDictionary->IsWordListed(strRes)==eMatchMixedCase)//check for spelling
		{
			cont ++;
		}
		iStart = iEnd + 1;
	}
	return cont;
}

void CCaesarDeDlg::ShowTime()
{
	finish=clock();
//	time = _T("");
	time.Format("%d ms",finish-start);
//	time += _T(" ms");
	SetDlgItemText(IDC_TIME,time);
}

void CCaesarDeDlg::OnRadioK() 
{
	// TODO: Add your control notification handler code here
	m_iMethod = 3;
	m_wndTaskbarNotifier->Show("超级综合式:此法结合了无敌概率式快速的优点,以及频率改进式准确的特点,综合出一种在词库范围内快速,超出词库范围又准确的超级方法。");
}

void CCaesarDeDlg::OnRadioN() 
{
	// TODO: Add your control notification handler code here
	m_iMethod = 4;
	m_wndTaskbarNotifier->Show("无间绝杀式:此法不同于其他方法,允许密文中间无任何标点符号或者空格来分隔单词,而且效率非常高,强烈推荐使用!");
}

double GetScore(char *d)//将密文扫描一遍,计算得分
{
	int i;
	double sc = 0;
	for( i = 0 ; i < m_bpt-3 ; i ++ )//取log,避免数字过大
		sc = sc + log ( tetrfreq[d[m_buffer[i]-'a']-'a'][d[m_buffer[i+1]-'a']-'a']
		[d[m_buffer[i+2]-'a']-'a'][d[m_buffer[i+3]-'a']-'a'] + 1 ) * 0.25;
	return sc;
}

void Pemutation(int i ,char *d)
{
	for ( int j = 0 ; j < 26 ; j ++ )
	{
		d[j] = ( i + j ) % 26 + 'a';
	}
}

DWORD WINAPI TetraMethod(LPVOID lpParam)
{
	clock_t start=clock();
	clock_t finish;
//	CString times = _T("");
	int i,p;
	char dec[27];
	dec[26] = '\0';
	double bestscore , sc;
	m_bpt = 0;
	char str[100000];
	::GetDlgItemText((HWND)lpParam,IDC_CIPHER,str,100000);
	CString m_cipherString = str;
	int length = m_cipherString.GetLength();
	m_cipherString.GetBuffer(length);
	for( i = 0 ; i < length ; i ++ )
		if ( isalpha(m_cipherString[i]) != 0 )
			m_buffer[m_bpt++] = tolower(m_cipherString[i]);
	m_buffer[m_bpt] = '\0';
	m_buffout[length] = '\0';
	bestscore = -88.88;
	for ( p = 0 ; p < 26 ; p ++ )
	{
		Pemutation(p,dec);
		sc =  GetScore(dec);
		if ( sc > bestscore)
		{
			bestscore = sc;
			finish = clock();
//			times.Format("%d ms",finish - start);
//			SetDlgItemText( (HWND)lpParam , IDC_TIME , times);
			CString temp;
			temp.Format("%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c" , 
				dec[0] , dec[1] , dec[2] , dec[3] , dec[4] ,
				dec[5] , dec[6] , dec[7] , dec[8] , dec[9] ,
				dec[10] , dec[11] , dec[12] , dec[13] , dec[14] ,
				dec[15] , dec[16] , dec[17] , dec[18] , dec[19] ,
				dec[20] , dec[21] , dec[22] , dec[23] , dec[24] ,
				dec[25]);
			temp.MakeUpper();
			SetDlgItemText( (HWND)lpParam , IDC_SUB , temp );
			for ( i = 0 ; i < length ; i ++ )
			{
				if((m_cipherString[i]>='a')&&(m_cipherString[i]<='z'))
					m_buffout[i]=dec[m_cipherString[i]-'a'];
				else if((m_cipherString[i]>='A')&&(m_cipherString[i]<='Z'))
					m_buffout[i]=dec[m_cipherString[i]-'A']-'a'+'A';
				else
					m_buffout[i]=m_cipherString[i];
			}
			m_plain.Format( "%s" , m_buffout );
			SetDlgItemText((HWND)lpParam,IDC_PLAIN,m_plain);
			::UpdateWindow((HWND)lpParam);
		}
	}
	return 0;
}

void CCaesarDeDlg::DecipherN()
{
	if(m_hThread)
		TerminateThread(m_hThread,m_dwThreadId);
	m_hThread = CreateThread(
		NULL,
		0,
		TetraMethod,
		this->GetSafeHwnd(),
		0,
		&m_dwThreadId);
	if(m_hThread==NULL)
		AfxMessageBox("创建线程失败!");
}

BOOL CCaesarDeDlg::PreTranslateMessage(MSG* pMsg)
{
	// CG: The following block was added by the ToolTips component.	{		// Let the ToolTip process this message.		m_tooltip.RelayEvent(pMsg);	}	return CDialog::PreTranslateMessage(pMsg);	// CG: This was added by the ToolTips component.
}

void CCaesarDeDlg::Getsuggestion(CString &str)
{
	CString strRes,suggestion;
	int cont = 0;
	int iStart = 0,iEnd =0, iLen=0, iStrLen;
	iStrLen = str.GetLength();
	while ( iEnd < iStrLen )
	{
		while ( iStart < iStrLen 
			&& !( ( str.GetAt(iStart) >= 'a' && str.GetAt(iStart) <= 'z' )
			|| ( str.GetAt(iStart) >= 'A' && str.GetAt(iStart) <= 'Z' ) ) )
			iStart ++;
		if (iStart == iStrLen)
			break;
		iEnd = iStart;
		while ( iEnd < iStrLen
			&& ( ( str.GetAt(iEnd) >= 'a' && str.GetAt(iEnd) <= 'z' )
			|| ( str.GetAt(iEnd) >= 'A' && str.GetAt(iEnd) <= 'Z' ) ) )
			iEnd ++;
		strRes = str.Mid(iStart,iEnd-iStart); //Extract the word
		if(m_pDictionary->IsWordListed(strRes)==eMatchNone)//check for spelling
		{
			suggestion = suggestion + strRes + "在词库中无法找到,建议使用:" + BuildSuggestions(strRes , false) + "\r\n";
		}
		iStart = iEnd + 1;
	}
	SetDlgItemText(IDC_SUGGESTION,suggestion);
}

CString CCaesarDeDlg::BuildSuggestions(const CString &strWord, bool bCaseOnly)
{
	// Get the suggestions
    CStringArray straSuggestions;
    CString strSuggestions;
    m_pDictionary->GetSuggestions(strWord, straSuggestions, bCaseOnly);

    for (int nCount = 0; nCount < straSuggestions.GetSize(); nCount++)
    {
        if (nCount)
        {
            strSuggestions += ", ";
        }
        strSuggestions += straSuggestions[nCount];
    }

    return strSuggestions;
}

BOOL CCaesarDeDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	if ( pWnd == GetDlgItem(IDC_MODIFY_LEFT) 
		|| pWnd == GetDlgItem(IDC_MODIFY_RIGHT) 
		|| pWnd == GetDlgItem(IDC_DECIPHER)
		|| pWnd == GetDlgItem(IDC_SETKEY)
		)
		::SetCursor(m_hHand);
	else if ( pWnd == GetDlgItem(IDC_SUGGESTION)
		|| pWnd == GetDlgItem(IDC_CIPHER)
		|| pWnd == GetDlgItem(IDC_PLAIN)
		)
		::SetCursor(m_hBeam);
	else if ( pWnd == GetDlgItem(IDC_RADIO_P)
		|| pWnd == GetDlgItem(IDC_RADIO_H)
		|| pWnd == GetDlgItem(IDC_RADIO_M)
		|| pWnd == GetDlgItem(IDC_RADIO_K)
		|| pWnd == GetDlgItem(IDC_RADIO_N)
		)
		::SetCursor(m_hPen);
	else if ( nHitTest == HTCAPTION )
		::SetCursor(m_hMove);
	else
		::SetCursor(m_hArrow);
	return true;
	//return CDialog::OnSetCursor(pWnd, nHitTest, message);
}

⌨️ 快捷键说明

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