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

📄 subdedlg.cpp

📁 1、对于凯撒密文
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// SubDeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "030300816.h"
#include "SubDeDlg.h"
#include "Dictionary.h"
#include "Winuser.h"
#include "Windows.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int tetfreq[26][26][26][26],ctype,bpt;
char freq[26];
DWORD WINAPI DecipherP(LPVOID lpParam);
DWORD WINAPI DecipherH(LPVOID lpParam);
bool CheckString(CString &str);
VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime);
CString m_plainString,timep;
char buffer[100000];
char buffout[100000];
CDictionary *m_pDictionary;
HWND hWnd;
HANDLE hTimerThread;
/////////////////////////////////////////////////////////////////////////////
// CSubDeDlg dialog


CSubDeDlg::CSubDeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSubDeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSubDeDlg)
	m_radio = 0;
	m_cipherString = _T("");
	m_plainString = _T("");
	m_time = _T("");
	//}}AFX_DATA_INIT
}


void CSubDeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSubDeDlg)
	DDX_Radio(pDX, IDC_RADIO_P, m_radio);
	DDX_Text(pDX, IDC_CIPHER, m_cipherString);
	DDX_Text(pDX, IDC_PLAIN, m_plainString);
	DDX_Text(pDX, IDC_TIME, m_time);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSubDeDlg, CDialog)
	//{{AFX_MSG_MAP(CSubDeDlg)
	ON_BN_CLICKED(IDC_DECIPHER, OnDecipher)
	ON_BN_CLICKED(IDC_RADIO_P, OnRadioP)
	ON_BN_CLICKED(IDC_RADIO_H, OnRadioH)
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_PAUSE, OnPause)
	ON_BN_CLICKED(IDC_RESUME, OnResume)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_STOP, OnStop)
	ON_WM_SETCURSOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSubDeDlg message handlers
//DWORD WINAPI pppp( LPVOID lpParam );
/*
void gpp(UINT i,HWND hWnd)
{
	while(i<100)
//	SetDlgItemInt(IDC_PPPP,i,true);
	{
		::SetDlgItemInt(hWnd,IDC_PPPP,i++,true);
		
	}
}
*/
/*
DWORD WINAPI pppp( LPVOID lpParam ) 
{
	UINT i = 0;
	char str[100000];
//	::GetDlgItemText((HWND)lpParam,IDC_CIPHER,str,100000);
	::GetDlgItemText((HWND)lpParam,IDC_CIPHER,str,100000);
	CString m_cipherString = str;
	::SetDlgItemText((HWND)lpParam,IDC_PLAIN,m_cipherString);
		::UpdateWindow((HWND)lpParam);
	gpp(i,(HWND)lpParam);
	HWND hWnd = (HWND)lpParam;
	
	return 0;
}
*/
void CSubDeDlg::OnDecipher() 
{
	// TODO: Add your control notification handler code here
	
	CWnd* poWnd;
	poWnd = GetDlgItem(IDC_DECIPHER);
	poWnd->EnableWindow(false);
	poWnd = GetDlgItem(IDC_PAUSE);
	poWnd->EnableWindow(true);
	poWnd = GetDlgItem(IDC_RESUME);
	poWnd->EnableWindow(false);
	poWnd = GetDlgItem(IDC_STOP);
	poWnd->EnableWindow(true);
	if ( 0 == m_iMethod )
	{
		if(hThread)
			TerminateThread(hThread,dwThreadId);
		hThread = CreateThread(
			NULL,
			0,
			DecipherP,
			this->GetSafeHwnd(),
			0,
			&dwThreadId);
		if(hThread==NULL)
			AfxMessageBox("创建线程失败!");
		hTimerThread = hThread;
	}
	else
	{
		if(hThread)
			TerminateThread(hThread,dwThreadId);
		hThread = CreateThread(
			NULL,
			0,
			DecipherH,
			this->GetSafeHwnd(),
			0,
			&dwThreadId);
		if(hThread==NULL)
			AfxMessageBox("创建线程失败!");
		hTimerThread = hThread;
	}
//	else
//	{
//		DecipherH();
//	}
	
}

void CSubDeDlg::OnRadioP() 
{
	// TODO: Add your control notification handler code here
	m_iMethod = 0;
	m_wndTaskbarNotifier->Show("霹雳式:该法利用频率关系作为启发信息对密文进行解密,当遇到解出的中间明文中的单词都能在词库中找到时自动停止,否则,1分钟后自动暂停,用户可自行选择停止或者继续。");
}

void CSubDeDlg::OnRadioH() 
{
	// TODO: Add your control notification handler code here
	m_iMethod = 1;
	m_wndTaskbarNotifier->Show("无间式:该法与霹雳式类似,同样采用频率来作为启发式信息,但完全忽略单词间的空格与标点,达到“无间”的效果,但不进行词库匹配,用户可自行暂停或停止。否则,1分钟后自动暂停,等候用户响应。");
}

HBRUSH CSubDeDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	if (pWnd->GetDlgCtrlID() == IDC_SUB)
	{
		// Set the text color to red
		pDC->SetTextColor(RGB(255, 0, 0));
		
		// Set the background mode for text to transparent 
		// so background will show thru.
		pDC->SetBkMode(TRANSPARENT);
	}
	// TODO: Return a different brush if the default is not Ideaired
	return hbr;
}

void rndstr(char *d)//随机排序,得到一个新的dec字符数组
{
	int i , j , k;
	char n;
	strcpy( d , "abcdefghijklmnopqrstuvwxyz" );
	i = ( rand() % 1000 ) + 100;//100-1000次的调整
	while( i-- )
	{ 
		j = rand() % 26;
		k = rand() % 26;
		if( j != k )
		{ 
			n = d[j];
			d[j] = d[k];
			d[k] = n;
		}
	}
}

void freqsort(char *d)
{
	char alphaFreq[27] = "etiaonsrhcldpyumfbgwvkxqzj";
	int sort[26];
	int temp , rec;
	for ( int i = 0 ; i < 26 ; i ++ ) sort[i] = 0;
	for( i = 0 ; i < bpt ; i ++ )
	{
		if ( isalpha(buffer[i]) != 0 )
			sort[tolower(buffer[i])-'a']++;
	}
	for ( i = 0 ; i < 26 ; i ++ )
	{
		temp = -1;
		for ( int j = 0 ; j < 26 ; j ++ )
		{
			if ( sort[j] > temp )
			{
				temp = sort[j];
				rec = j;
			}
		}
		d[rec] = alphaFreq[i];
		sort[rec] = -1;
	}
}

void smalladj(char *d)//小范围调整
{
	int i , j , k;
	char n;
	i = ( rand() % 5 ) + 5;//5-10次的调整
	while( i-- )
	{ 
		j = rand() % 26;
		k = rand() % 26;
		if( j != k )
		{ 
			n = d[j];
			d[j] = d[k];
			d[k] = n;
		}
	}
}

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

DWORD WINAPI CTetra( LPVOID lpParam ) 
{
	FILE *tet;
//	char s[20];
	int d;
	tet = fopen( "tetra.txt" , "rt" );//读tetra.txt
	for ( int i = 0 ; i < 26 ; i ++ )
		for ( int j = 0 ; j < 26 ; j ++ )
			for ( int k = 0 ; k < 26 ; k ++ )
				for ( int l = 0 ; l < 26 ; l ++ )
				{
					fscanf ( tet , "%d" , &d );
					tetfreq[i][j][k][l] = d;
				}
/*	while(1)
	{ 
		if( fscanf( tet , "%s" , s ) != 1 )//读前4个字符
			break;
		fscanf( tet , "%d" , &d );//读频率
		tetfreq[s[0]-'a'][s[1]-'a'][s[2]-'a'][s[3]-'a'] = d;//存各自频率
	}*/
	fclose( tet );//read tetra over
	return 0;
}

BOOL CSubDeDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	
	m_iMethod = 0;
	m_hArrow = AfxGetApp()->LoadCursor(IDC_MYARROW);
	m_hHand = AfxGetApp()->LoadCursor(IDC_MYHAND);
	m_hBeam = AfxGetApp()->LoadCursor(IDC_MYBEAM);
	m_hPen = AfxGetApp()->LoadCursor(IDC_MYPEN);
	m_hMove = AfxGetApp()->LoadCursor(IDC_MYMOVE);
	CWnd* poWnd;
	poWnd = GetDlgItem(IDC_DECIPHER);
	poWnd->EnableWindow(true);
	poWnd = GetDlgItem(IDC_STOP);
	poWnd->EnableWindow(false);
	poWnd = GetDlgItem(IDC_PAUSE);
	poWnd->EnableWindow(false);
	poWnd = GetDlgItem(IDC_RESUME);
	poWnd->EnableWindow(false);
	::m_pDictionary = CSubDeDlg::m_pDictionary;
	::hWnd = GetSafeHwnd();
	hTetraThread = CreateThread(
			NULL,
			0,
			CTetra,
			&tetfreq,
			0,
			&dwTetraThreadId);
		if(hTetraThread==NULL)
			AfxMessageBox("创建线程失败!");
	m_wndTaskbarNotifier->Show("欢迎进入代入法解码系统!\r\n可点击单选按钮获得相关信息!");
//	pSkin2->ApplySkin((long)m_hWnd);
	// CG: The following block was added by the ToolTips component.	{		// Create the ToolTip control.		m_tooltip.Create(this);		m_tooltip.Activate(TRUE);		// TODO: Use one of the following forms to add controls:		// m_tooltip.AddTool(GetDlgItem(IDC_<name>), <string-table-id>);		// m_tooltip.AddTool(GetDlgItem(IDC_<name>), "<text>");	}
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

DWORD WINAPI DecipherP(LPVOID lpParam)
{
	::SetTimer(hWnd,3,60000,(TIMERPROC)TimerProc);
	clock_t start=clock();
	clock_t finish;
	CString times = _T("");
	bool isCorrect = 0;
	time_t t;
	int i , j , besti , bestj , smallperturb;
	char c , dec[27] , testd[26];
	dec[26] = '\0';
	double bestscore , sc , bestadj , bestoverall;
	srand( time(&t) );//根据时间生成随机数起始点
	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( ( m_cipherString[i] >= 'a' ) && ( m_cipherString[i] <= 'z' ) )
			buffer[bpt++] = m_cipherString[i];
		else if( ( m_cipherString[i] >= 'A' ) && ( m_cipherString[i] <= 'Z' ) )
			buffer[bpt++] = m_cipherString[i] - 'A' + 'a';*/
		if ( isalpha(m_cipherString[i]) != 0 )
			buffer[bpt++] = tolower(m_cipherString[i]);
	buffer[bpt] = '\0';
	buffout[length] = '\0';
	//strrev(buffer);//the end of buffer
	/*printf("密文初始化\n %s\n",buffer);
	printf("Analysing crypt:\n%.2lf %s\n",score("abcdefghijklmnopqrstuvwxyz"),buffer);//初始得分*/
//	rndstr(dec);//random sort
	freqsort(dec);	//sort by frequency first
	bestscore = score(dec);//score the random pemutation
	bestoverall = bestscore-1;//总体最高分初始化
	smallperturb = 1;
	while(!isCorrect)
	{
		finish = clock();
		times.Format("%1.2f",(finish-start)/1000.0);
		times += _T("s");
		::SetDlgItemText((HWND)lpParam,IDC_TIME,times);
		bestadj = bestscore;
		for( i = 0 ; i < 26 ; i ++ )
		{
			for( j = i+1 ; j < 26 ; j ++ )
			{ 
				strcpy( testd , dec );//拷贝排列
				c = testd[i];
				testd[i] = testd[j];
				testd[j] = c;
				sc = score(testd);//改两个字母替换后的分数
				if( sc > bestadj )
				{ 
					bestadj = sc;

⌨️ 快捷键说明

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