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

📄 vigenere_cipher.cpp

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

#include "stdafx.h"
#include "MY_CAP.h"
#include "Vigenere_Cipher.h"

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

/////////////////////////////////////////////////////////////////////////////
// CVigenere_Cipher dialog

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


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


BEGIN_MESSAGE_MAP(CVigenere_Cipher, CDialog)
	//{{AFX_MSG_MAP(CVigenere_Cipher)
	ON_COMMAND(IDD_ENCIPHER, OnEncipher)
	ON_COMMAND(IDD_DECIPHER, OnDecipher)
	ON_COMMAND(IDD_QUIT, OnQuit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVigenere_Cipher message handlers

void CVigenere_Cipher::OnEncipher() 
{
	// TODO: Add your command handler code here
	UpdateData(TRUE);
	Main_CapV->UpdateData(TRUE);
	m_Plaintext = Main_CapV->m_Plaintext;
	m_Ciphertext = "";
	int i = 0, j = 0;
	m_RKeyWord = Translation(m_KeyWord);
	if( m_RKeyWord == "" ) MessageBox( "请输入密钥!" );
	else if( m_Plaintext != "" )
	{
		while( i < m_Plaintext.GetLength() )
		{
			if( j == m_KeyWord.GetLength() ) j=0;
			if( m_Plaintext[i] >= 'a' && m_Plaintext[i] <= 'z' )
			{
				m_Ciphertext += (m_Plaintext[i]-'a' + m_RKeyWord[j] - 'a') % 26 + 'a';
				j++;
			}
			else if( m_Plaintext[i] >= 'A' && m_Plaintext[i] <= 'Z' )
			{
				m_Ciphertext += (m_Plaintext[i]-'A' + m_RKeyWord[j] - 'a') % 26 + 'a';
				j++;
			}
/*			else
			{
				m_Ciphertext += m_Plaintext[i];
			}*/
			i++;
		}
	}
	Main_CapV -> m_Ciphertext = m_Ciphertext;
	Main_CapV -> UpdateData(FALSE);
	if (Main_CapV ->m_Ciphertext.GetLength () != 0)
		Main_CapV -> OnSave_Ciphertext();
}

void CVigenere_Cipher::OnDecipher() 
{
	// TODO: Add your command handler code here
	
	UpdateData(TRUE);
	Main_CapV->UpdateData(TRUE);
	m_Ciphertext = Main_CapV->m_Ciphertext;
	m_Plaintext = "";
	int i = 0, j = 0;
	m_RKeyWord = Translation(m_KeyWord);
	if( m_KeyWord == "" ) MessageBox( "请输入密钥!" );
	else if( m_Ciphertext != "" )
	{
		while( i < m_Ciphertext.GetLength() )
		{
			if( j == m_KeyWord.GetLength() ) j = 0;
			if( m_Ciphertext[i] >= 'a' && m_Ciphertext[i] <= 'z' )
			{
				m_Plaintext += ( m_Ciphertext[i] - m_RKeyWord[j] + 26 ) % 26 + 'a';
				j++;
			}
			else if( m_Ciphertext[i] >= 'A' && m_Ciphertext[i] <= 'Z' )
			{
				m_Plaintext += 
					(m_Ciphertext[i] - m_RKeyWord[j]  -'A' + 'a' + 26 ) % 26 + 'a';
				j++;
			}
/*			else
			{
				m_Plaintext += m_Ciphertext[i];
			}*/
			i++;
		}
	}
	Main_CapV -> m_Plaintext = m_Plaintext;
	Main_CapV -> UpdateData(FALSE);
	if (Main_CapV ->m_Plaintext.GetLength () != 0)
		Main_CapV -> OnSave_Plaintext ();
}

void CVigenere_Cipher::Trans_Dialog(CWnd *temp)
{
	Main_CapV = (CMY_CAPDlg *)temp;
}

CString CVigenere_Cipher::Translation(CString phrase)
{
	int i = 0;
	CString temp = "";
	while( i < phrase.GetLength() )
	{
	    if( phrase[i] >= 'A' && phrase[i] <= 'Z' )
			temp += (phrase[i] - 'A' + 'a');
		else if(phrase[i] >= 'a' && phrase[i] <= 'z')temp += phrase[i];
		i++;
	}
	return temp;
}

void CVigenere_Cipher::OnQuit() 
{
	// TODO: Add your command handler code here
	OnOK();
}

⌨️ 快捷键说明

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