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

📄 playfair_cipher.cpp

📁 这是我编的包括古典密码
💻 CPP
字号:
// Playfair_Cipher.cpp : implementation file
//

#include "stdafx.h"
#include "MY_CAP.h"
#include "Playfair_Cipher.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPlayfair_Cipher dialog

CMY_CAPDlg *Main_CapP;
CPlayfair_Cipher::CPlayfair_Cipher(CWnd* pParent /*=NULL*/)
	: CDialog(CPlayfair_Cipher::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPlayfair_Cipher)
	m_KeyWord = _T("");
	m_ORKeyWord = _T("");
	//}}AFX_DATA_INIT
}


void CPlayfair_Cipher::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPlayfair_Cipher)
	DDX_Text(pDX, IDC_KEYWORD, m_KeyWord);
	DDX_Text(pDX, IDC_RKeyWord, m_ORKeyWord);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPlayfair_Cipher, CDialog)
	//{{AFX_MSG_MAP(CPlayfair_Cipher)
	ON_COMMAND(IDD_DECIPHER, OnDecipher)
	ON_COMMAND(IDD_ENCIPHER, OnEncipher)
	ON_COMMAND(IDD_SETKEY, OnSetkey)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPlayfair_Cipher message handlers

void CPlayfair_Cipher::OnDecipher() 
{
	// TODO: Add your command handler code here
	UpdateData(TRUE);
	Main_CapP -> UpdateData(TRUE);
	m_Ciphertext = Main_CapP -> m_Ciphertext;
	int i = 0,Position_l,Position_r,count,row[2],tier[2];
	CString temp;
	m_Plaintext = "";
	if( m_Ciphertext == "" ) MessageBox("请输入密文!");
	if( m_Ciphertext.GetLength() % 2 != 0 ) MessageBox("你输入的密钥必须为偶数个!");
	else
	{
		count = m_Ciphertext.GetLength();
		while( i < count)
		{
			if(m_Ciphertext[i] >= 'a' && m_Ciphertext[i] <= 'z'
				|| m_Ciphertext[i] >= 'A' &&m_Ciphertext[i] <= 'Z')
			{
				temp = m_Ciphertext[i];
				Position_l = m_RKeyWord.FindOneOf(temp);
				row[0] = Position_l / 5;
				tier[0] = Position_l % 5;
				temp = m_Ciphertext[i+1];
				Position_r = m_RKeyWord.FindOneOf(temp);
				row[1] = Position_r / 5;
				tier[1] = Position_r % 5;
				if( Position_l == Position_r)//the same completely(防止用户输入出错)
				{
					row[0] = row[1] = (row[0] + 4) % 5;
				}
				else if( row[0] == row[1] )//the same row
				{
					tier[0] = (tier[0]+4) % 5;
					tier[1] = (tier[1]+4) % 5;			
				}
				else if( tier[0] == tier[1] )//the same tier
				{
					row[0] = (row[0]+4) % 5;
					row[1] = (row[1]+4) % 5;
				}
				else //the different row and tier
				{
					int temp;
					temp = tier[0];
					tier[0] = tier[1];
					tier[1] = temp;
				}
				m_Plaintext += m_RKeyWord[ row[0] * 5 + tier[0] ];
				m_Plaintext += m_RKeyWord[ row[1] * 5 + tier[1] ];
				i += 2;
			}
			else i++;
		}
		Main_CapP-> m_Plaintext = m_Plaintext;
		Main_CapP -> UpdateData(FALSE);
		if (Main_CapP ->m_Plaintext.GetLength () != 0)
			Main_CapP -> OnSave_Plaintext ();
	}
}

void CPlayfair_Cipher::OnEncipher() 
{
	// TODO: Add your command handler code here
	UpdateData(TRUE);
	Main_CapP->UpdateData(TRUE);
	m_Plaintext = Main_CapP->m_Plaintext;
	int i = 0,Position_l,Position_r,count,row[2],tier[2];
	m_Ciphertext = "";
	CString temp;
	if( m_Plaintext != "" )
	{
	//	m_RKeyWord = Scan_KeyWord(m_KeyWord);
		m_RPlaintext = Settle_Plaintext(m_Plaintext);
		count = m_RPlaintext.GetLength();
		while( i < count)
		{
			temp = m_RPlaintext[i];
			Position_l = m_RKeyWord.FindOneOf(temp);
			row[0] = Position_l / 5;
			tier[0] = Position_l % 5;
			temp = m_RPlaintext[i+1];
			Position_r = m_RKeyWord.FindOneOf(temp);
			row[1] = Position_r / 5;
			tier[1] = Position_r % 5;
			if( Position_l == Position_r)//the same completely(可以略去,为了对称解密防止用户的出错处理)
			{
				row[0] = row[1] = (row[0] + 1) % 5;
			}
			else if( row[0] == row[1] )//the same row
			{
				tier[1] = (tier[1]+1) % 5;
				tier[0] = (tier[0]+1) % 5;	
			}
			else if( tier[0] == tier[1] )//the same tier
			{
				row[1] = (row[1]+1) % 5;
				row[0] = (row[0]+1) % 5;
			}
			else //the different row and tier
			{
				int temp;
				temp = tier[1];
				tier[1] = tier[0];
				tier[0] = temp;
			}
			m_Ciphertext += m_RKeyWord[ row[0] * 5 + tier[0] ];
			m_Ciphertext += m_RKeyWord[ row[1] * 5 + tier[1] ];
			i += 2;
		}
		Main_CapP -> m_Ciphertext = m_Ciphertext;
		Main_CapP -> UpdateData(FALSE);
	    if (Main_CapP ->m_Ciphertext.GetLength () != 0)
			Main_CapP -> OnSave_Ciphertext ();
	}
}

CString CPlayfair_Cipher::Scan_KeyWord(CString phrase)
{
	CString Phrase,temp;
	int i = 0;
	while( i < phrase.GetLength() )
	{
		if( phrase[i] == 'j' || phrase[i] == 'J') 
		{
			temp = 'i';
			if( Phrase.FindOneOf(temp) == -1 ) Phrase += phrase[i];
		}
		else if( phrase[i] >= 'a' && phrase <= 'z')
		{
			temp=phrase[i];
			if( Phrase.FindOneOf(temp) == -1 ) Phrase += phrase[i];
		}
		else if( phrase[i] >= 'A' && phrase <= 'Z')
		{
			temp=phrase[i] - 'A' + 'a';
			if( Phrase.FindOneOf(temp) == -1 ) Phrase += temp;
		}
		i++;
	}
	i=0;
	while( i < 26)
	{
		temp = 'a' + i;
		if( Phrase.FindOneOf(temp) == -1 && temp != "j") Phrase += temp;
		i++;
	}
	return Phrase;
}

CString CPlayfair_Cipher::Settle_Plaintext(CString phrase)
{
	CString Phrase;
	int i = 0,count = phrase.GetLength();
	while( i < count)
	{
		if( phrase[i] >= 'a' && phrase[i] <= 'z' 
			|| phrase[i] >= 'A' && phrase[i] <= 'Z')
			Phrase += phrase[i];
		i++;
	}
	phrase = Phrase;
	Phrase = "";
	count = phrase.GetLength();
	i = 0;
	while( i < count)
	{
		if( i == phrase.GetLength() - 1 ) 
		{
			if ( phrase[i] == 'x' )
			{
				Phrase += phrase[i];
				Phrase += 'q';
			}
			else
			{
				Phrase += phrase[i];
				Phrase += 'x';
			}
		}
		else if( phrase[i] != phrase [i+1] )
		{
			Phrase += phrase[i];
			Phrase += phrase[i+1];
		}
		else if( phrase[i] == phrase[i+1] )
		{
			if ( phrase[i] == 'x' )
			{
				Phrase += phrase[i];
				Phrase += 'q';
			}
			else
			{
				Phrase += phrase[i];
				Phrase += 'x';
			}
			i--;
		}
		i += 2;
	}
	return Phrase;
}

void CPlayfair_Cipher::Trans_Dialog(CWnd *temp)
{
	Main_CapP = (CMY_CAPDlg *)temp;
}

void CPlayfair_Cipher::OnSetkey() 
{
	// TODO: Add your command handler code here
	m_RKeyWord = Scan_KeyWord(m_KeyWord);
	int i=0;
	while( i < 25 )
	{
		if( i % 5 == 0 )
		{
			m_ORKeyWord += 13;
			m_ORKeyWord += '\n';
		}
		m_ORKeyWord += m_RKeyWord[i];
		m_ORKeyWord += "  ";
		i++;
	}
	UpdateData(FALSE);
}

⌨️ 快捷键说明

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