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

📄 playfairdlg.cpp

📁 包涵了密码学教程里面的大部分加密算法
💻 CPP
字号:
// PlayfairDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Crypt.h"
#include "PlayfairDlg.h"

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

static char P[5][5] = {
	{'c', 'i', 'p', 'h', 'e'},
	{'r', 'a', 'b', 'd', 'f'},
	{'g', 'k', 'l', 'm', 'n'},
	{'o', 'q', 's', 't', 'u'},
	{'v', 'w', 'x', 'y', 'z'}};

/////////////////////////////////////////////////////////////////////////////
// CPlayfairDlg dialog

CPlayfairDlg::CPlayfairDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPlayfairDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPlayfairDlg)
	m_strPlainText = _T("");
	m_strCipher = _T("");
	//}}AFX_DATA_INIT
}


void CPlayfairDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPlayfairDlg)
	DDX_Text(pDX, IDC_PlainText, m_strPlainText);
	DDX_Text(pDX, IDC_Cipher, m_strCipher);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPlayfairDlg, CDialog)
	//{{AFX_MSG_MAP(CPlayfairDlg)
	ON_BN_CLICKED(IDC_Encrypt, OnPlayfairEncrypt)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPlayfairDlg message handlers

void CPlayfairDlg::OnPlayfairEncrypt() 
{
	// TODO: Add your control notification handler code here
	int i, j, k, l;
	int m_iPlainTextLen;
	CString m_strTemp;
	char ch1, ch2;
	int pos1, pos2, pos3, pos4;
       
	UpdateData(true);
	m_strTemp = m_strPlainText;
    m_iPlainTextLen = m_strPlainText.GetLength();
	char* m_chPlainText = new char[m_iPlainTextLen];
	m_chPlainText = m_strPlainText.GetBuffer(0);
    
	char m_chTemp[1000];
	for(l=0; l<1000; l++)
		m_chTemp[l] = '\0';
	int num = 0;
	while(*m_chPlainText != '\0')
	{
		m_chTemp[num] = *m_chPlainText;
		m_chPlainText++;
		num++;
	}
	
	if(m_iPlainTextLen <= 0)
	{
		AfxMessageBox("No PlainText to encrypt!", MB_ICONSTOP|MB_OK);
		exit(0);
	}

	if(m_iPlainTextLen % 2 != 0)
	{
		/*
		*(m_chPlainText+m_iPlainTextLen) = 'q';
		m_iPlainTextLen += 1;
		*/
		m_chTemp[m_iPlainTextLen] = 'q';
		m_iPlainTextLen ++;
	}
        
	//char* m_chCipher = new char[m_iPlainTextLen];
	char m_chCipher[1000];
	for(l=0; l<1000; l++)
		m_chCipher[l] = '\0';
	for(i=0; i<m_iPlainTextLen; i+=2)
	{
		ch1 = m_chTemp[i];
		ch2 = m_chTemp[i+1];

		//得出两个字母在密码表中的位置
		for(j=0; j<5; j++)
			for(k=0; k<5; k++)
			{
				if(P[j][k] == ch1)
				{
					pos1 = j;
					pos2 = k;
				}

				if(P[j][k] == ch2)
				{
					pos3 = j;
					pos4 = k;
				}
			}
        
		if(pos1 == pos3)
		{
			m_chCipher[i] = P[pos1][(pos2+1)%5];
			m_chCipher[i+1] = P[pos3][(pos4+1)%5];
		}
		else if(pos2 == pos4)
		{
            m_chCipher[i] = P[(pos1+1)%5][pos2];
			m_chCipher[i+1] = P[(pos3+1)%5][pos4];
		}
		else
		{
			m_chCipher[i] = P[pos1][pos4];
			m_chCipher[i+1] = P[pos3][pos2];
		}
	}

	m_strCipher.Format("%s", m_chCipher);
	UpdateData(false);
}

⌨️ 快捷键说明

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