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

📄 vigneardlg.cpp

📁 维吉尼亚密码的破译。用VC++作为工具
💻 CPP
字号:
// VignearDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Vignear.h"
#include "VignearDlg.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CVignearDlg dialog
#define M 1000

class sq
{
public:
	int a[M];
	int length;
	

    int f[26];
	void freq();

    float Ic(int n);
	sq()
	{
		int i;
		for(i=0;i<M;i++) a[i]=0;
		length=0;
		for(i=0;i<26;i++) f[i]=0;
	}
};

void sq::freq()
{
	int i,j;
	for(i=0;i<26;i++) f[i]=0;
	for(i=0;i<length;i++)
	{
		for(j=0;j<26;j++)
		{
			if (a[i]==j) f[j]++;
		}
	}
	
}

float sq::Ic(int n)
{
	int i;
	float s=0;
	for(i=0;i<26;i++)
	{
		s+=float(f[i]*(f[i]-1));
	}
	s=s/n/(n-1);
	return s;

}
int ctext[M];
int key[M];
int lth=0;
int keylength;
sq squence;



CVignearDlg::CVignearDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVignearDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CVignearDlg)
	m_edit1 = _T("");
	m_edit2 = _T("");
	m_edit3 = _T("");
	m_edit4 = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CVignearDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVignearDlg)
	DDX_Control(pDX, IDC_EDIT4, c_edit4);
	DDX_Control(pDX, IDC_EDIT3, c_edit3);
	DDX_Control(pDX, IDC_EDIT2, c_edit2);
	DDX_Control(pDX, IDC_EDIT1, c_edit1);
	DDX_Text(pDX, IDC_EDIT1, m_edit1);
	DDX_Text(pDX, IDC_EDIT2, m_edit2);
	DDX_Text(pDX, IDC_EDIT3, m_edit3);
	DDX_Text(pDX, IDC_EDIT4, m_edit4);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CVignearDlg, CDialog)
	//{{AFX_MSG_MAP(CVignearDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVignearDlg message handlers

BOOL CVignearDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CVignearDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CVignearDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CVignearDlg::OnButton1() 
{
	int i=0;
	int t;
	UpdateData(TRUE);
	c_edit1.GetWindowText(m_edit1);
	while (i<m_edit1.GetLength()) 
	{ 
		t=m_edit1.GetAt(i)-'A';
		if ((t<0)||(t>25)) 
		{
			MessageBox("输入有错,每位必须是26个大写英文字母'A' 到 'Z'!","错误!",MB_ICONEXCLAMATION);
			c_edit1.SetFocus();
	        UpdateData(FALSE);
			return;
		}
		else i++;
	}
	i=0;
	while (i<m_edit1.GetLength())
	{
		t=m_edit1.GetAt(i)-'A';
		ctext[i]=t;
		i++;
		
	}
	lth=m_edit1.GetLength();
}



	


void CVignearDlg::OnButton2() 
{
	int i,j,m=2;
	int n;
	float I[M];
	float averI=0;
	double min=1;
	UpdateData(TRUE);   
	m_edit2.Empty();
	UpdateData(FALSE);

	
	if(!lth)
	{
        MessageBox("未输入密文!请按第一步按钮输入确定.",
			       "提示!",MB_ICONINFORMATION);
		
		return;
	}

	while(1)
	{

		for(i=0;i<=m-1;i++)
		{
			n=(int)lth/m;
			squence.length=n;
			for(j=0;j<n;j++)
			{
				squence.a[j]=ctext[i+j*m];
			}
			squence.freq();
            I[i]=squence.Ic(n);
		}
		averI=0;
        for(i=0;i<=m-1;i++)
		{
			averI+=I[i];
		}
		averI=averI/m;
		if (fabs(averI-0.065)<=0.005) break;
		
		else m++;
	}
    keylength=m;
    UpdateData(TRUE);
	m_edit2=m+'0';
	UpdateData(FALSE);


}

void CVignearDlg::OnButton3() 
{
	double p[26]={0.082,0.015,0.028,0.043,0.127,0.022,0.020,0.061,0.070,0.002,0.008,0.040,0.024,
		          0.067,0.075,0.019,0.001,0.060,0.063,0.091,0.028,0.010,0.023,0.001,0.020,0.001};
	int i,j,k,g;
	int n;
	double Mg=0,min=1;

	UpdateData(TRUE);   
	m_edit3.Empty();
	UpdateData(FALSE);

	if(!keylength)
	{
        MessageBox("未知密钥长度!请按第二步按钮生成.",
			       "提示!",MB_ICONINFORMATION);
		
		return;
	}
	for(i=0;i<keylength;i++)
	{
			n=(int)lth/keylength;
			squence.length=n;
			for(j=0;j<n;j++)
			{
				squence.a[j]=ctext[i+j*keylength];
			}
			squence.freq();
			min=1;

			for(g=0;g<26;g++)
			{   
				Mg=0;
				for(k=0;k<26;k++)
				{
					Mg+=(p[k]*squence.f[(k+g)%26])/n;
				}
				if(fabs(Mg-0.065)<=0.01) 	
				{
					if(fabs(Mg-0.065)<min)	
					{
						key[i]=g;
						min=fabs(Mg-0.065);
					}
				}
			
			}

	}
	UpdateData(TRUE);
	for(i=0;i<keylength;i++)
	{
		m_edit3+=key[i]+'A';
	}
	UpdateData(FALSE);

}

void CVignearDlg::OnButton4() 
{
	int i,j;
	int n;
	int rest;
	int s=0;

	UpdateData(TRUE);   
	m_edit4.Empty();
	UpdateData(FALSE);

	n=(int)lth/keylength;
	rest=lth%keylength;
    for(j=0;j<keylength;j++) s+=key[j];
    
	if(!s)
	{
        MessageBox("未知密钥!请按第三步按钮生成.",
			       "提示!",MB_ICONINFORMATION);
		
		return;
	}


	UpdateData(TRUE);
	for(i=0;i<n;i++)
	{
		for(j=0;j<keylength;j++)
		{
			m_edit4+=(ctext[i*keylength+j]+26-key[j])%26+'a';
		}
	}
	for(j=0;j<rest;j++)
	{
		m_edit4+=(ctext[i*keylength+j]+26-key[j])%26+'a';
	}
	UpdateData(FALSE);

}

void CVignearDlg::OnButton5() 
{
	CString strFileName,t;
	int i;
	UpdateData(TRUE);   
	strFileName="test_";
	CFileDialog dlg(FALSE,"ctext",strFileName, //FALSE为"打开文件"窗口,"ctext"为确定时默认的文件类型
		OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
		"初始配置文件(*.ctext)|*.ctext||",NULL);
	dlg.m_ofn.lpstrInitialDir=".\\res";
	if(dlg.DoModal()==IDCANCEL) return;
	strFileName = dlg.GetPathName();

	
	CString Output,temp;
	Output.Empty();

	for (i=0;i<m_edit1.GetLength();i++) Output.Insert(65535,m_edit1.GetAt(i));


	CFile file;
	file.Open(strFileName,CFile::modeCreate|CFile::modeWrite);

	file.Write(Output,Output.GetLength());
	file.Close();

    CVignearDlg::MessageBox("保存完毕!","提示",MB_ICONINFORMATION);

}

void CVignearDlg::OnButton6() 
{
	CString strFileName;
	CFile file;
	int i,j;
	int sum;
	char buffer[1000];
	for(i=0;i<1000;i++) buffer[i]=' ';
	UpdateData(TRUE);   
	m_edit1.Empty();
	UpdateData(FALSE);
	
	CFileDialog dlg(TRUE,"ctext",".ctext", //TRUE为"打开文件"窗口,"ctext"为确定时默认的文件类型
		OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
		"初始配置文件(*.ctext)|*.ctext||",NULL);
	dlg.m_ofn.lpstrInitialDir=".\\res";
	if(dlg.DoModal()==IDCANCEL) return;
	strFileName = dlg.GetPathName();

	file.Open(strFileName,CFile::modeRead);
	file.Read(buffer,file.GetLength());
	file.Close();
	
	i=0;
	sum=0;
	while(buffer[i]!=' ')
	{
		i++;
		sum++;
	}
	
    for(j=0;j<sum;j++)
	{
	    m_edit1+=buffer[j];
		
	}
	UpdateData(FALSE);
    
	CVignearDlg::MessageBox("提取完毕!","提示",MB_ICONINFORMATION);

}

⌨️ 快捷键说明

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