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

📄 hilldlg.cpp

📁 我自己写的HILL加密/解密算法~~~~~[绝对原创]
💻 CPP
字号:
// HillDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Hill.h"
#include "HillDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
	int k[3][3],nik[3][3];
	int cipher[10]={0};
	int deter,nideter;
	char plain[500],out[10],cip[500];
	CString temp,ciphertext="";
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CHillDlg dialog

CHillDlg::CHillDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CHillDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CHillDlg)
	m_key = _T("");
	m_result = _T("");
	m_plain = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CHillDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHillDlg)
	DDX_Text(pDX, IDC_KEY,m_key);
	DDX_Text(pDX, IDC_EDIT4, m_result);
	DDX_Text(pDX, IDC_plain, m_plain);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CHillDlg, CDialog)
	//{{AFX_MSG_MAP(CHillDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHillDlg message handlers

BOOL CHillDlg::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
	SetDlgItemText(IDC_plain,"abc");
	SetDlgItemText(IDC_KEY,"17 17 5\r\n21 18 21\r\n2 2 19");
	temp.Format("提示:\r\n1.加密先在密钥框内输入3x3矩阵,然后在明文处输入长度为3的倍数的明文,点击开始加密,结果显示在结果框内。\r\n2.解密和加密方式雷同。\r\n3.本程序目前只支持小写字母加密/解密。\r\n\r\n\r\n\r\n周觅源    信息安全");
	SetDlgItemText(IDC_EDIT1,temp);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CHillDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	int length,time,i,tp=0;
	
	SetDlgItemText(IDC_EDIT4,"");
	ciphertext="";

	key();

	GetDlgItemText(IDC_plain,temp);
	length=strlen(temp);
	if (length%3!=0) 
	{
		MessageBox("明文长度必须是3的倍数!请重新输入!");
		SetDlgItemText(IDC_plain,"abc");
		goto end;
	}
	strcpy(plain,(LPCTSTR)temp);	//将文本框内容转换成字符数组
	time=length/3;
	for (i=1;i<=time;i++)
	{
		cipher[0]=(k[0][0]*(plain[tp]-97)+k[0][1]*(plain[tp+1]-97)+k[0][2]*(plain[tp+2]-97))%26;
		cipher[1]=(k[1][0]*(plain[tp]-97)+k[1][1]*(plain[tp+1]-97)+k[1][2]*(plain[tp+2]-97))%26;
		cipher[2]=(k[2][0]*(plain[tp]-97)+k[2][1]*(plain[tp+1]-97)+k[2][2]*(plain[tp+2]-97))%26;
		tp+=3;
		inttochar();
	}
	SetDlgItemText(IDC_EDIT4,ciphertext);

end: ;
}

void CHillDlg::key()
{
	bool flag;	//判定的字符输入是否为数字
	int a[100]={0};
	int n=0,m=0,i,j,tp;
	char t[1000];

	GetDlgItemText(IDC_KEY,temp);
	strcpy(t,(LPCTSTR)temp);//将文本框内容转换成字符数组

	for (i=0,flag=0;i<100;i++)//将字符数组转换为整数组
	{
		if (t[i]=='\0')				break;
		if (t[i]>='0' && t[i]<='9')
		{
			n=n*10+t[i]-'0';
			flag = 1;
		}
		else if (flag)
		{			
			a[m]=n;
			n=0;
			m++;
			flag=0;
		}
	}
	if (flag) //处理最后一项
	{			
		a[m]=n;
		n=0;
		m++;
		flag=0;
	}
		if(m==0)
	{
		AfxMessageBox("请输入3x3的矩阵密钥!");
		goto loop;
	}

	tp=0;
	for (i=0;i<=2;i++)
		for(j=0;j<=2;j++)
		{
			k[i][j]=a[tp];
			tp++;
		}
loop: ;
}
void CHillDlg::inttochar()
{
		int i;
		for (i=0;i<=2;i++)
		switch (cipher[i])
		{
			case 0 : ciphertext+='a';break;
			case 1 : ciphertext+='b';break;
			case 2 : ciphertext+='c';break;
			case 3 : ciphertext+='d';break;
			case 4 : ciphertext+='e';break;
			case 5 : ciphertext+='f';break;
			case 6 : ciphertext+='g';break;
			case 7 : ciphertext+='h';break;
			case 8 : ciphertext+='i';break;
			case 9 : ciphertext+='j';break;
			case 10 : ciphertext+='k';break;
			case 11 : ciphertext+='l';break;
			case 12 : ciphertext+='m';break;
			case 13 : ciphertext+='n';break;
			case 14 : ciphertext+='o';break;
			case 15 : ciphertext+='p';break;
			case 16 : ciphertext+='q';break;
			case 17 : ciphertext+='r';break;
			case 18 : ciphertext+='s';break;
			case 19 : ciphertext+='t';break;
			case 20 : ciphertext+='u';break;
			case 21 : ciphertext+='v';break;
			case 22 : ciphertext+='w';break;
			case 23 : ciphertext+='x';break;
			case 24 : ciphertext+='y';break;
			case 25 : ciphertext+='z';break;
			default: MessageBox("Error!");
		}
}

void CHillDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	int i,length,tp=0,time;

	ciphertext="";
	key();
	reverse();
	if (nideter==0)
	{
		MessageBox("所输入的矩阵的行列式不符合要求!请重新输入!");
		SetDlgItemText(IDC_KEY,"17 17 5\r\n21 18 21\r\n2 2 19");
		SetDlgItemText(IDC_plain,"abc");
		goto end;
	}
	GetDlgItemText(IDC_cipher,temp);
	length=strlen(temp);
	if (length%3!=0) 
	{
		MessageBox("密文长度必须是3的倍数!请重新输入!");
		SetDlgItemText(IDC_cipher,"");
		goto end;
	}
	strcpy(cip,(LPCTSTR)temp);	//将文本框内容转换成字符数组
	time=length/3;
	for (i=1;i<=time;i++)
	{
		cipher[0]=(nik[0][0]*(cip[tp]-97)+nik[0][1]*(cip[tp+1]-97)+nik[0][2]*(cip[tp+2]-97))%26;
		cipher[1]=(nik[1][0]*(cip[tp]-97)+nik[1][1]*(cip[tp+1]-97)+nik[1][2]*(cip[tp+2]-97))%26;
		cipher[2]=(nik[2][0]*(cip[tp]-97)+nik[2][1]*(cip[tp+1]-97)+nik[2][2]*(cip[tp+2]-97))%26;
		tp+=3;
		inttochar();
	}
	SetDlgItemText(IDC_EDIT4,ciphertext);	
end: ;
}

void CHillDlg::reverse()
{
	int i,j;
	deter=k[0][0]*(k[1][1]*k[2][2]-k[1][2]*k[2][1])-k[0][1]*(k[1][0]*k[2][2]-k[2][0]*k[1][2])+k[0][2]*(k[1][0]*k[2][1]-k[2][0]*k[1][1]);
	while (deter<0)
		deter+=26;
	deter=deter%26;	
	switch(deter)
	{
		case 3: nideter=9; break;
		case 5: nideter=21;break;
		case 7: nideter=15;break;
		case 9: nideter=3;break;
		case 11: nideter=19;break;
		case 15: nideter=7;break;
		case 17: nideter=23;break;
		case 19: nideter=11;break;
		case 21: nideter=5;break;
		case 23: nideter=17;break;
		case 25: nideter=25;break;
		default: nideter=0;
	}
		nik[0][0]=nideter*(k[1][1]*k[2][2]-k[1][2]*k[2][1]);
		nik[0][1]=-nideter*(k[0][1]*k[2][2]-k[2][1]*k[0][2]);
		nik[0][2]=nideter*(k[0][1]*k[1][2]-k[0][2]*k[1][1]);
		nik[1][0]=-nideter*(k[1][0]*k[2][2]-k[2][0]*k[1][2]);
		nik[1][1]=nideter*(k[0][0]*k[2][2]-k[2][0]*k[0][2]);
		nik[1][2]=-nideter*(k[0][0]*k[1][2]-k[1][0]*k[0][2]);
		nik[2][0]=nideter*(k[1][0]*k[2][1]-k[1][1]*k[2][0]);
		nik[2][1]=-nideter*(k[0][0]*k[2][1]-k[2][0]*k[0][1]);
		nik[2][2]=nideter*(k[0][0]*k[1][1]-k[1][0]*k[0][1]);
	for (i=0;i<=2;i++)
		for(j=0;j<=2;j++)
		{
			while(nik[i][j]<0)
				nik[i][j]+=26;
			nik[i][j]=nik[i][j]%26;
		}

}

void CHillDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	OnButton3();	
}

⌨️ 快捷键说明

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