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

📄 gudianmima.cpp

📁 古典加密算法,非常实用
💻 CPP
字号:
#include "stdafx.h"
#include "Crpt.h"
#include "CrptDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// CCrptDlg dialog

CCrptDlg::CCrptDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCrptDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCrptDlg)
	m_strCt1 = _T("");
	m_strPt1 = _T("");
	m_iKey1 = 0;
	m_strCt2 = _T("");
	m_iKey2a = 0;
	m_iKey2b = 0;
	m_strPt2 = _T("");
	m_strKey3 = _T("");
	m_strPt3 = _T("");
	m_strCt3 = _T("");
	m_strPt4 = _T("");
	m_strKey4 = _T("");
	m_strCt4 = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCrptDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCrptDlg)
	DDX_Text(pDX, IDC_CT1, m_strCt1);
	DDX_Text(pDX, IDC_PT1, m_strPt1);
	DDX_Text(pDX, IDC_KEY1, m_iKey1);
	DDX_Text(pDX, IDC_CT2, m_strCt2);
	DDX_Text(pDX, IDC_KEY2a, m_iKey2a);
	DDX_Text(pDX, IDC_KEY2b, m_iKey2b);
	DDX_Text(pDX, IDC_PT2, m_strPt2);
	DDX_Text(pDX, IDC_KEY3, m_strKey3);
	DDX_Text(pDX, IDC_PT3, m_strPt3);
	DDX_Text(pDX, IDC_CT3, m_strCt3);
	DDX_Text(pDX, IDC_PT4, m_strPt4);
	DDX_Text(pDX, IDC_KEY4, m_strKey4);
	DDX_Text(pDX, IDC_CT4, m_strCt4);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCrptDlg, CDialog)
	//{{AFX_MSG_MAP(CCrptDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_METH1, OnMeth1)
	ON_BN_CLICKED(IDC_KEY1SET, OnKey1set)
	ON_BN_CLICKED(IDC_E1, OnE1)
	ON_BN_CLICKED(IDC_D1, OnD1)
	ON_BN_CLICKED(IDC_CLEARPT1, OnClearpt1)
	ON_BN_CLICKED(IDC_CLEARCT1, OnClearct1)
	ON_BN_CLICKED(IDC_METH2, OnMeth2)
	ON_BN_CLICKED(IDC_KEY2SET, OnKey2set)
	ON_BN_CLICKED(IDC_E2, OnE2)
	ON_BN_CLICKED(IDC_D2, OnD2)
	ON_BN_CLICKED(IDC_CLEARPT2, OnClearpt2)
	ON_BN_CLICKED(IDC_CLEARCT2, OnClearct2)
	ON_BN_CLICKED(IDC_METH3, OnMeth3)
	ON_BN_CLICKED(IDC_KEY3SET, OnKey3set)
	ON_BN_CLICKED(IDC_E3, OnE3)
	ON_BN_CLICKED(IDC_D3, OnD3)
	ON_BN_CLICKED(IDC_CLEARPT3, OnClearpt3)
	ON_BN_CLICKED(IDC_CLEARCT3, OnClearct3)
	ON_BN_CLICKED(IDC_METH4, OnMeth4)
	ON_BN_CLICKED(IDC_KEY4SET, OnKey4set)
	ON_BN_CLICKED(IDC_E4, OnE4)
	ON_BN_CLICKED(IDC_D4, OnD4)
	ON_BN_CLICKED(IDC_CLEARPT4, OnClearpt4)
	ON_BN_CLICKED(IDC_CLEARCT4, OnClearct4)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CCrptDlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	m_bJ1 = 0;
	m_bJ2 = 0;
	m_bJ3 = 0;
	m_bJ4 = 0;
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCrptDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CCrptDlg::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 CCrptDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCrptDlg::OnMeth1() 
{
	// TODO: Add your control notification handler code here
	AfxMessageBox(" 移位密码介绍:\r 字母表用整数0 - 25来标识, 密钥K为整数,且取值空间为0到25\r 加密函数:x = x + k (mod 26)\r 解密函数:x = x - k (mod 26)\r 当K=3时,为凯撒密码\r 不识别空格", MB_ICONINFORMATION);
}

void CCrptDlg::OnKey1set() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_bJ1 = 1;
}

void CCrptDlg::OnE1() 
{
	// TODO: Add your control notification handler code here
	if (m_bJ1 == 0)
	{
		AfxMessageBox("请检查是否设置了密钥或输入了明文");
	}
	else
	{
		UpdateData();
		int i = m_strPt1.GetLength();
		m_strCt1.Empty();		
		for (int j = 0; j < i; j++)
		{
			m_strCt1 += (m_strPt1[j] - 97 + m_iKey1) % 26 + 97;
			
		}
		UpdateData(FALSE);
	}
}

void CCrptDlg::OnD1() 
{
	// TODO: Add your control notification handler code here
	if (m_bJ1 == 0)
	{
		AfxMessageBox("请检查是否设置了密钥或输入了密文");
	}
	else
	{
		UpdateData();
		int i = m_strCt1.GetLength();
		m_strPt1.Empty();		
		for (int j = 0; j < i; j++)
		{
			if (m_strCt1[j] - 97 - m_iKey1 >= 0)
			{
				m_strPt1 += (m_strCt1[j] - 97 - m_iKey1) % 26 + 97;
			}
			else
			{
				m_strPt1 += m_strCt1[j] +  26 - m_iKey1;
			}
			
			
		}
		UpdateData(FALSE);
	}
}

void CCrptDlg::OnClearpt1() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_strPt1.Empty();
	UpdateData(FALSE);
}

void CCrptDlg::OnClearct1() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_strCt1.Empty();
	UpdateData(FALSE);
}

void CCrptDlg::OnMeth2() 
{
	// TODO: Add your control notification handler code here
	AfxMessageBox(" 仿射密码介绍:\r 字母表用整数0 - 25来标识, 密钥对由a、b组成\r 整数a满足 gcd(a, 26) = 1 \r 整数b的取值空间为0到25\r 加密函数:x = ax + b(mod 26)\r 解密函数:x = a*y - a*b (mod 26)\r 当a=1,b=3时,为凯撒密码\r 不识别空格", MB_ICONINFORMATION);
}

void CCrptDlg::OnKey2set() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_bJ2 = 1;
}

void CCrptDlg::OnE2() 
{
	// TODO: Add your control notification handler code here
	if (m_bJ2 == 0)
	{
		AfxMessageBox("请检查是否设置了密钥或输入了明文");
	}
	else
	{
		UpdateData();
		int i = m_strPt2.GetLength();
		m_strCt2.Empty();		
		for (int j = 0; j < i; j++)
		{
			m_strCt2 += ((m_strPt2[j] - 97) * m_iKey2a + m_iKey2b) % 26 + 97;
			
		}
		UpdateData(FALSE);
	}
}

void CCrptDlg::OnD2() 
{
	// TODO: Add your control notification handler code here
	if (m_bJ2 == 0)
	{
		AfxMessageBox("请检查是否设置了密钥或输入了密文");
	}
	else
	{
		UpdateData();
		int i = m_strCt2.GetLength();
		m_strPt2.Empty();		
		int a = transt(m_iKey2a);
	
		for (int j = 0; j < i; j++)
		{
			if (m_strCt2[j] - 97 - m_iKey2b < 0)
			{
				m_strPt2 += ((m_strCt2[j] - 97 - m_iKey2b) * a + 26) % 26 + 97;
			}
			else
			{
				m_strPt2 += ((m_strCt2[j] - 97 - m_iKey2b) * a) % 26 + 97;
			}
		}
		UpdateData(FALSE);
	}
}

void CCrptDlg::OnClearpt2() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_strPt2.Empty();
	UpdateData(FALSE);
}

void CCrptDlg::OnClearct2() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_strCt2.Empty();
	UpdateData(FALSE);
}

void CCrptDlg::OnMeth3() 
{
	// TODO: Add your control notification handler code here
	AfxMessageBox("维吉尼亚密码介绍: \r 首先确定密钥长度(本例中密钥只采取个位数字,所以取决于输入密钥的长度)如6\r 然后输入满足这个长度的向量,如K = (1,2,3,4,5,6)\r 加密:取明文第一个字母并将之移k1位,这里k1=1,第二个字母移k2位,k2=2,一旦到了密钥末尾,又从头开始\r 不识别空格", MB_ICONINFORMATION);
}

void CCrptDlg::OnKey3set() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_bJ3 = 1;
}
void CCrptDlg::OnE3() 
{
	// TODO: Add your control notification handler code here
	if (m_bJ3 == 0)
	{
		AfxMessageBox("请检查是否设置了密钥或输入了明文");
	}
	else
	{
		UpdateData();
		m_strCt3.Empty();
		int keylen = m_strKey3.GetLength();
		int ptlen = m_strPt3.GetLength();
		int i, j, m, n;

			i = ptlen / keylen;
			j = ptlen % keylen;
			for (n = 0; n < i; ++n)
			{
				for (m = 0; m < keylen; ++m)
				{
					if (m_strPt3[n * keylen + m] + m_strKey3[m] - 'a' > 'z')
					{
						m_strCt3 += m_strPt3[n * keylen + m] + m_strKey3[m] - 'a' - 26;
					}
					else
					{
						m_strCt3 += m_strPt3[n * keylen + m] + m_strKey3[m] - 'a';
					}
					
				}
			}
			for (m = 0; m < j; ++m)
			{
				if (m_strPt3[n * keylen + m] + m_strKey3[m] - 'a' > 'z')
					{
						m_strCt3 += m_strPt3[n * keylen + m] + m_strKey3[m] - 'a' - 26;
					}
					else
					{
						m_strCt3 += m_strPt3[n * keylen + m] + m_strKey3[m] - 'a';
					}
			}
		UpdateData(FALSE);
	}
}

void CCrptDlg::OnD3() 
{
	// TODO: Add your control notification handler code here
	if (m_bJ3 == 0)
	{
		AfxMessageBox("请检查是否设置了密钥或输入了密文");
	}
	else
	{
		UpdateData();
	m_strPt3.Empty();
	int keylen = m_strKey3.GetLength();
	int ctlen = m_strCt3.GetLength();
	int i, j, m, n;
			
			i = ctlen / keylen;
			j = ctlen % keylen;
			for (n = 0; n < i; ++n)
			{
				for (m = 0; m < keylen; ++m)
				{
					if (m_strCt3[n * keylen + m] - m_strKey3[m] + 'a' < 'a')
					{
						m_strPt3 += m_strCt3[n * keylen + m] - m_strKey3[m] + 'a' + 26;
					}
					else
					{
						m_strPt3 += m_strCt3[n * keylen + m] - m_strKey3[m] + 'a';
					}
				}
			}
			for (m = 0; m < j; ++m)
			{
				if (m_strCt3[n * keylen + m] - m_strKey3[m] + 'a' < 'a')
					{
						m_strPt3 += m_strCt3[n * keylen + m] - m_strKey3[m] + 'a' + 26;
					}
					else
					{
						m_strPt3 += m_strCt3[n * keylen + m] - m_strKey3[m] + 'a';
					}
			}

	UpdateData(FALSE);
	}
}

void CCrptDlg::OnClearpt3() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_strPt3.Empty();
	UpdateData(FALSE);
}

void CCrptDlg::OnClearct3() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_strCt3.Empty();
	UpdateData(FALSE);
}

void CCrptDlg::OnMeth4() 
{
	// TODO: Add your control notification handler code here
	AfxMessageBox("换位密码介绍:\r 确定密钥长度,如5 \r 输入长度为5的0到4的整数序列,如14032作为密钥 \r 将明文分成每5个字母一组,每组字母按照密钥进行换位\r 不识别空格", MB_ICONINFORMATION);
}

void CCrptDlg::OnKey4set() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_bJ4 = 1;
}

void CCrptDlg::OnE4() 
{
	// TODO: Add your control notification handler code here
	if (m_bJ4 == 0)
	{
		AfxMessageBox("请检查是否设置了密钥或输入了明文");
	}
	else
	{
		UpdateData();
		m_strCt4.Empty();
		int keylen = m_strKey4.GetLength();
		int ptlen = m_strPt4.GetLength();
		int i = ptlen / keylen;
		int j = ptlen % keylen;
		if (0 != j)
		{
			for(int m = 0; m < keylen - j; ++m)
			{
				m_strPt4 += 'x';
			}
			i++;
		}
		
		int n, m, k;
		for (n = 0; n < i; ++n)
		{
			for (m = 0; m < keylen; ++m)
			{
				for (k = 0; k < keylen; ++k)
				{
					if (m_strKey4[k] == '0' + m)
					{
						m_strCt4 += m_strPt4[n * keylen + k];
					}
				}
			}
		}
		UpdateData(FALSE);
	}
}

void CCrptDlg::OnD4() 
{
	// TODO: Add your control notification handler code here
	if (m_bJ4 == 0)
	{
		AfxMessageBox("请检查是否设置了密钥或输入了密文");
	}
	else
	{
		UpdateData();
		m_strPt4.Empty();
		int keylen = m_strKey4.GetLength();
		int ctlen = m_strCt4.GetLength();
		
		CString key;
		key.Empty();
		for (int e = 0; e < keylen; ++e)
		{
			for (int f = 0; f < keylen; ++f)
			{
				if (e == m_strKey4[f])
				{
					key += m_strKey4[f];
				}
			}
		}

		int i = ctlen / keylen;
		int j = ctlen % keylen;
		if (0 != j)
		{
			AfxMessageBox("请检查密文格式是否正确");
		}
		
		else
		{
			int n, k;
			for (n = 0; n < i; ++n)
			{
				for (k = 0; k < keylen; ++k)
				{
					m_strPt4 += m_strCt4[n * keylen + m_strKey4[k] - '0'];
				}
			}		
		}
		UpdateData(FALSE);
	}
}

void CCrptDlg::OnClearpt4() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_strPt4.Empty();
	UpdateData(FALSE);
}

void CCrptDlg::OnClearct4() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_strCt4.Empty();
	UpdateData(FALSE);
}

int CCrptDlg::transt(int a)
{
	if (1 == a)
	{
		return 1;
	}
	else
	{
		int b, q, r, q0, x, x0, x1;
	    r = 1;
		b = 26;
		x1 = 1;
		x0 = 0;
		q0 = -1;
		while (0 != r)
		{
			q = b / a;
			r = b % a;
			x = q0 * (-1) * x0 + x1;
			q0 = q;
			x1 = x0;
			x0 = x;
			b = a;
			a = r;
		}
		return x;
	}
}

⌨️ 快捷键说明

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