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

📄 playfairdlg.cpp

📁 1、对于凯撒密文
💻 CPP
字号:
// PlayfairDlg.cpp : implementation file
//

#include "stdafx.h"
#include "030300816.h"
#include "PlayfairDlg.h"

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

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


CPlayfairDlg::CPlayfairDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPlayfairDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPlayfairDlg)
	m_key = _T("firewall security");
	m_radio = 0;
	m_key1 = _T("F");
	m_key10 = _T("U");
	m_key11 = _T("T");
	m_key12 = _T("Y");
	m_key13 = _T("B");
	m_key14 = _T("D");
	m_key15 = _T("G");
	m_key16 = _T("H");
	m_key17 = _T("K");
	m_key18 = _T("M");
	m_key19 = _T("N");
	m_key2 = _T("I");
	m_key20 = _T("O");
	m_key21 = _T("P");
	m_key22 = _T("Q");
	m_key23 = _T("V");
	m_key24 = _T("X");
	m_key25 = _T("Z");
	m_key3 = _T("R");
	m_key4 = _T("E");
	m_key5 = _T("W");
	m_key6 = _T("A");
	m_key7 = _T("L");
	m_key8 = _T("S");
	m_key9 = _T("C");
	m_cipherFile = _T("");
	m_cipherString = _T("");
	m_plainFile = _T("");
	m_plainString = _T("");
	//}}AFX_DATA_INIT
}


void CPlayfairDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPlayfairDlg)
	DDX_Text(pDX, IDC_KEY, m_key);
	DDX_Radio(pDX, IDC_RADIO_STRING, m_radio);
	DDX_Text(pDX, IDC_KEY1, m_key1);
	DDX_Text(pDX, IDC_KEY10, m_key10);
	DDX_Text(pDX, IDC_KEY11, m_key11);
	DDX_Text(pDX, IDC_KEY12, m_key12);
	DDX_Text(pDX, IDC_KEY13, m_key13);
	DDX_Text(pDX, IDC_KEY14, m_key14);
	DDX_Text(pDX, IDC_KEY15, m_key15);
	DDX_Text(pDX, IDC_KEY16, m_key16);
	DDX_Text(pDX, IDC_KEY17, m_key17);
	DDX_Text(pDX, IDC_KEY18, m_key18);
	DDX_Text(pDX, IDC_KEY19, m_key19);
	DDX_Text(pDX, IDC_KEY2, m_key2);
	DDX_Text(pDX, IDC_KEY20, m_key20);
	DDX_Text(pDX, IDC_KEY21, m_key21);
	DDX_Text(pDX, IDC_KEY22, m_key22);
	DDX_Text(pDX, IDC_KEY23, m_key23);
	DDX_Text(pDX, IDC_KEY24, m_key24);
	DDX_Text(pDX, IDC_KEY25, m_key25);
	DDX_Text(pDX, IDC_KEY3, m_key3);
	DDX_Text(pDX, IDC_KEY4, m_key4);
	DDX_Text(pDX, IDC_KEY5, m_key5);
	DDX_Text(pDX, IDC_KEY6, m_key6);
	DDX_Text(pDX, IDC_KEY7, m_key7);
	DDX_Text(pDX, IDC_KEY8, m_key8);
	DDX_Text(pDX, IDC_KEY9, m_key9);
	DDX_Text(pDX, IDC_CIPHER_FILE, m_cipherFile);
	DDX_Text(pDX, IDC_CIPHER_STRING, m_cipherString);
	DDX_Text(pDX, IDC_PLAIN_FILE, m_plainFile);
	DDX_Text(pDX, IDC_PLAIN_STRING, m_plainString);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPlayfairDlg, CDialog)
	//{{AFX_MSG_MAP(CPlayfairDlg)
	ON_BN_CLICKED(IDC_SETKEY, OnSetkey)
	ON_BN_CLICKED(IDC_RADIO_STRING, OnRadioString)
	ON_BN_CLICKED(IDC_RADIO_FILE, OnRadioFile)
	ON_BN_CLICKED(IDC_OPEN_PLAIN, OnOpenPlain)
	ON_BN_CLICKED(IDC_OPEN_CIPHER, OnOpenCipher)
	ON_BN_CLICKED(IDC_ENCIPHER, OnEncipher)
	ON_BN_CLICKED(IDC_DECIPHER, OnDecipher)
	ON_WM_SETCURSOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

void CPlayfairDlg::OnSetkey() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	if ( "" == m_key )
		MessageBox("Please input a key.",": (",MB_OK);
	else
	{
		Setkey(m_key);
		Showkey();
		UpdateData(false);
	}
}

void CPlayfairDlg::OnRadioString() 
{
	// TODO: Add your control notification handler code here
	ShowStringGroup(true);
	ShowFileGroup(false);
	m_iMethod = 0;
}

void CPlayfairDlg::OnRadioFile() 
{
	// TODO: Add your control notification handler code here
	ShowStringGroup(false);
	ShowFileGroup(true);
	m_iMethod = 1;
}

void CPlayfairDlg::OnOpenPlain() 
{
	// TODO: Add your control notification handler code here
	CFileDialog oFileOpen(TRUE);
	oFileOpen.m_ofn.lpstrFilter = "Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
	oFileOpen.m_ofn.lpstrDefExt="txt";
	CString oStrDir;
	if(IDOK == oFileOpen.DoModal())
	{
		CString oStrFilePath = oFileOpen.GetPathName();
		TCHAR szDir[MAX_PATH+1];
		_tcscpy(szDir, LPCTSTR(oStrFilePath));
		oStrFilePath = CString(szDir);
		SetDlgItemText(
			IDC_PLAIN_FILE,
			oStrFilePath
			);
	}
}

void CPlayfairDlg::OnOpenCipher() 
{
	// TODO: Add your control notification handler code here
	CFileDialog oFileOpen(TRUE);
	oFileOpen.m_ofn.lpstrFilter = "Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
	oFileOpen.m_ofn.lpstrDefExt="txt";
	CString oStrDir;
	if(IDOK == oFileOpen.DoModal())
	{
		CString oStrFilePath = oFileOpen.GetPathName();
		TCHAR szDir[MAX_PATH+1];
		_tcscpy(szDir, LPCTSTR(oStrFilePath));
		oStrFilePath = CString(szDir);
		SetDlgItemText(
			IDC_CIPHER_FILE,
			oStrFilePath
			);
	}
}

void CPlayfairDlg::OnEncipher() 
{
	// TODO: Add your control notification handler code here
	if ( 0 == m_iMethod )
	{
		EncipherString();
	}
	else // File
	{
		EncipherFile();
	}
}

void CPlayfairDlg::OnDecipher() 
{
	// TODO: Add your control notification handler code here
	if ( 0 == m_iMethod )
	{
		DecipherString();
	}
	else // File
	{
		DecipherFile();
	}
}

void CPlayfairDlg::ShowStringGroup(bool bShow)
{
	CWnd* poWnd;
	poWnd = GetDlgItem(IDC_STATIC_STRING1);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
	poWnd = GetDlgItem(IDC_STATIC_STRING2);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
	poWnd = GetDlgItem(IDC_STATIC_STRING3);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
	poWnd = GetDlgItem(IDC_PLAIN_STRING);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
	poWnd = GetDlgItem(IDC_CIPHER_STRING);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
}

void CPlayfairDlg::ShowFileGroup(bool bShow)
{
	CWnd* poWnd;
	poWnd = GetDlgItem(IDC_STATIC_FILE1);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
	poWnd = GetDlgItem(IDC_STATIC_FILE2);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
	poWnd = GetDlgItem(IDC_STATIC_FILE3);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
	poWnd = GetDlgItem(IDC_PLAIN_FILE);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
	poWnd = GetDlgItem(IDC_CIPHER_FILE);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
	poWnd = GetDlgItem(IDC_OPEN_PLAIN);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
	poWnd = GetDlgItem(IDC_OPEN_CIPHER);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
}

BOOL CPlayfairDlg::PreTranslateMessage(MSG* pMsg)
{
	// CG: The following block was added by the ToolTips component.	{		// Let the ToolTip process this message.		m_tooltip.RelayEvent(pMsg);	}	return CDialog::PreTranslateMessage(pMsg);	// CG: This was added by the ToolTips component.
}

BOOL CPlayfairDlg::OnInitDialog()
{
	CDialog::OnInitDialog();	// CG: This was added by the ToolTips component.	// CG: The following block was added by the ToolTips component.	{		// Create the ToolTip control.		m_tooltip.Create(this);		m_tooltip.Activate(TRUE);		ShowStringGroup(true);
		ShowFileGroup(false);
		m_iMethod = 0;
		grid[0][0] = 'F';
		grid[0][1] = 'I';
		grid[0][2] = 'R';
		grid[0][3] = 'E';
		grid[0][4] = 'W';
		grid[1][0] = 'A';
		grid[1][1] = 'L';
		grid[1][2] = 'S';
		grid[1][3] = 'C';
		grid[1][4] = 'U';
		grid[2][0] = 'T';
		grid[2][1] = 'Y';
		grid[2][2] = 'B';
		grid[2][3] = 'D';
		grid[2][4] = 'G';
		grid[3][0] = 'H';
		grid[3][1] = 'K';
		grid[3][2] = 'M';
		grid[3][3] = 'N';
		grid[3][4] = 'O';
		grid[4][0] = 'P';
		grid[4][1] = 'Q';
		grid[4][2] = 'V';
		grid[4][3] = 'X';
		grid[4][4] = 'Z';
		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);
		Setkey(m_key);		// 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>");	}
//	pSkin2->ApplySkin((long)m_hWnd);	return TRUE;	// CG: This was added by the ToolTips component.
}

void CPlayfairDlg::Setkey(CString m_key)
{
	int length = m_key.GetLength();
	m_key.MakeUpper();
	int * alphabet = new int [92];
	for ( int i = 65 ; i < 91 ; i ++ )
		alphabet[i] = 1;
	alphabet['J'] = 0; //j
	int positionX = 0 , positionY = 0;
	for ( i = 0 ; i < length ; i ++ )
	{
		if ( m_key[i] == ' ' || !alphabet[m_key[i]] )
			continue;
		else
		{
			grid[positionX][positionY] = m_key[i];
			alphabet[m_key[i]] = 0;
			m_letterPosition[m_key[i]].first = positionX;
			m_letterPosition[m_key[i]].second = positionY;
			positionY ++;
			if ( positionY == 5 )
			{
				positionX ++;
				positionY = 0;
			}
		}
	}
	for ( ; positionX < 5 ; positionY ++ )
	{
		for ( i = 'A' ; i <= 'Z' ; i ++ )
		{
			if ( alphabet[i] )
			{
				grid[positionX][positionY] = i;
				m_letterPosition[i].first = positionX;
				m_letterPosition[i].second = positionY;
				alphabet[i] = 0;
				positionY ++;
				if ( positionY == 5 )
				{
					positionX ++;
					positionY = 0;
				}
			}
		}
	}
	m_letterPosition['J'].first = m_letterPosition['I'].first;
	m_letterPosition['J'].second = m_letterPosition['I'].second;
}

void CPlayfairDlg::Showkey()
{
	m_key1=grid[0][0];m_key2=grid[0][1];m_key3=grid[0][2];m_key4=grid[0][3];m_key5=grid[0][4];
	m_key6=grid[1][0];m_key7=grid[1][1];m_key8=grid[1][2];m_key9=grid[1][3];m_key10=grid[1][4];
	m_key11=grid[2][0];m_key12=grid[2][1];m_key13=grid[2][2];m_key14=grid[2][3];m_key15=grid[2][4];
	m_key16=grid[3][0];m_key17=grid[3][1];m_key18=grid[3][2];m_key19=grid[3][3];m_key20=grid[3][4];
	m_key21=grid[4][0];m_key22=grid[4][1];m_key23=grid[4][2];m_key24=grid[4][3];m_key25=grid[4][4];
}

void CPlayfairDlg::EncipherString()
{
	UpdateData();
	CPlayfair playfair;
	playfair.m_plainString = m_plainString;
	playfair.Encipher(grid,m_letterPosition);
	m_plainString = "";
	m_cipherString = playfair.m_cipherString;
	UpdateData(FALSE);
}

void CPlayfairDlg::EncipherFile()
{
	UpdateData();
	if (m_cipherFile=="" || m_plainFile=="")
	{
		MessageBox("Please set file path correctly!","Error",MB_ICONSTOP);
		return;
	}
	CFile rfile(
		m_plainFile,
		CFile::modeRead 
		);
	char *pBuf;
	DWORD dwFileLen;
	dwFileLen=rfile.GetLength();
	pBuf=new char[dwFileLen+1];
	pBuf[dwFileLen]=0;
	rfile.Read(pBuf,dwFileLen);
	rfile.Close();
	CPlayfair playfair;
	playfair.m_plainString = pBuf;
	playfair.Encipher(grid,m_letterPosition);
//	CCaeser caeser;
//	caeser.m_plainString = pBuf;
//	caeser.Encipher();
	int length = playfair.m_cipherString.GetLength() +1;
	unsigned char * cipherBlock = (unsigned char*)malloc(length);
	memset(cipherBlock, 0, length);
	memcpy(cipherBlock, playfair.m_cipherString, length -1);
	CFile wfile(m_cipherFile,CFile::modeCreate | CFile::modeWrite);
	wfile.Write(cipherBlock,length);
	wfile.Close();
	CString succeed = "File encryption succeed!\nThe enciphed file is saved at\n" + 
		m_cipherFile + "\n\npreview:\n" + cipherBlock;
	MessageBox(succeed,"Encrption finish ^o^",MB_ICONINFORMATION);
	UpdateData(FALSE);
}

void CPlayfairDlg::DecipherString()
{
	UpdateData();
	CPlayfair playfair;
	playfair.m_cipherString = m_cipherString;
	playfair.Decipher(grid,m_letterPosition);
	m_cipherString = "";
	m_plainString = playfair.m_plainString;
	UpdateData(FALSE);
}

void CPlayfairDlg::DecipherFile()
{
	UpdateData();
	if (m_cipherFile=="" || m_plainFile=="")
	{
		MessageBox("Please set file path correctly!","Error",MB_ICONSTOP);
		return;
	}
	CFile rfile(
		m_plainFile,
		CFile::modeRead 
		);
	char *pBuf;
	DWORD dwFileLen;
	dwFileLen=rfile.GetLength();
	pBuf=new char[dwFileLen+1];
	pBuf[dwFileLen]=0;
	rfile.Read(pBuf,dwFileLen);
	rfile.Close();
	CPlayfair playfair;
	playfair.m_cipherString = pBuf;
	playfair.Decipher(grid,m_letterPosition);
//	CCaeser caeser;
//	caeser.m_cipherString = pBuf;
//	caeser.m_key = m_key;
//	caeser.Decipher();
	int length = playfair.m_cipherString.GetLength() +1;
	unsigned char * cipherBlock = (unsigned char*)malloc(length);
	memset(cipherBlock, 0, length);
	memcpy(cipherBlock, playfair.m_plainString, length -1);
	CFile wfile(m_cipherFile,CFile::modeCreate | CFile::modeWrite);
	wfile.Write(cipherBlock,length);
	wfile.Close();
	CString succeed = "File Decryption succeed!\nThe deciphed file is saved at\n" + 
		m_cipherFile + "\n\npreview:\n" + cipherBlock;
	MessageBox(succeed,"Decrption finish ^o^",MB_ICONINFORMATION);
	UpdateData(FALSE);
}

BOOL CPlayfairDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	if ( pWnd == GetDlgItem(IDC_ENCIPHER) 
		|| pWnd == GetDlgItem(IDC_DECIPHER)
		|| pWnd == GetDlgItem(IDC_OPEN_PLAIN)
		|| pWnd == GetDlgItem(IDC_OPEN_CIPHER)
		|| pWnd == GetDlgItem(IDC_SETKEY)
		)
		::SetCursor(m_hHand);
	else if ( pWnd == GetDlgItem(IDC_PLAIN_STRING)
		|| pWnd == GetDlgItem(IDC_CIPHER_STRING)
		|| pWnd == GetDlgItem(IDC_KEY)
		|| pWnd == GetDlgItem(IDC_PLAIN_FILE)
		|| pWnd == GetDlgItem(IDC_CIPHER_FILE)
		)
		::SetCursor(m_hBeam);
	else if ( pWnd == GetDlgItem(IDC_RADIO_STRING)
		|| pWnd == GetDlgItem(IDC_RADIO_FILE)
		)
		::SetCursor(m_hPen);
	else if ( nHitTest == HTCAPTION )
		::SetCursor(m_hMove);
	else
		::SetCursor(m_hArrow);
	return true;
}

⌨️ 快捷键说明

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