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

📄 caesardlg.cpp

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

#include "stdafx.h"
#include "030300816.h"
#include "CaesarDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CCaesarDlg dialog


CCaesarDlg::CCaesarDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCaesarDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCaesarDlg)
	m_key = 0;
	m_cipherString = _T("");
	m_plainString = _T("");
	//}}AFX_DATA_INIT
}


void CCaesarDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCaesarDlg)
	DDX_Text(pDX, IDC_KEY, m_key);
	DDV_MinMaxInt(pDX, m_key, 0, 25);
	DDX_Text(pDX, IDC_CIPHER_STRING, m_cipherString);
	DDX_Text(pDX, IDC_PLAIN_STRING, m_plainString);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCaesarDlg, CDialog)
	//{{AFX_MSG_MAP(CCaesarDlg)
	ON_BN_CLICKED(IDC_ENCIPHER, OnEncipher)
	ON_BN_CLICKED(IDC_DECIPHER, OnDecipher)
	ON_BN_CLICKED(IDC_RESET, OnReset)
	ON_BN_CLICKED(IDC_MODIFY_RIGHT, OnModifyRight)
	ON_BN_CLICKED(IDC_MODIFY_LEFT, OnModifyLeft)
	ON_WM_SETCURSOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCaesarDlg message handlers

void CCaesarDlg::OnEncipher() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	CCaeser caeser;
	caeser.m_plainString = m_plainString;
	caeser.m_key = m_key;
	ShowSub(m_key);
	caeser.Encipher();
//	m_plainString = "";
	m_cipherString = caeser.m_cipherString;
	UpdateData(FALSE);
}

void CCaesarDlg::OnDecipher() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	CCaeser caeser;
	caeser.m_cipherString = m_cipherString;
	caeser.m_key = m_key;
	ShowSub(m_key);
	caeser.Decipher();
	m_plainString = caeser.m_plainString;
//	m_cipherString = "";
	UpdateData(FALSE);
}

void CCaesarDlg::OnReset() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_plainString = "";
	m_cipherString = "";
	UpdateData(FALSE);
}

BOOL CCaesarDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	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);
	m_key = 3;
//	pSkin2->ApplySkin((long)m_hWnd);
	m_wndTaskbarNotifier->Show("欢迎进入凯撒密码加密解密系统!\r\n本系统提供了非常方便的通过给定密钥方式的凯撒法加密解密!");
	UpdateData(false);
	// CG: The following block was added by the ToolTips component.	{		// Create the ToolTip control.		m_tooltip.Create(this);		m_tooltip.Activate(TRUE);		m_tooltip.AddTool(GetDlgItem(IDC_ENCIPHER), "对明文进行加密");
		m_tooltip.AddTool(GetDlgItem(IDC_DECIPHER), "对密文进行解密");
		m_tooltip.AddTool(GetDlgItem(IDC_RESET), "清空明文密文");
		m_tooltip.AddTool(GetDlgItem(IDC_MODIFY_LEFT), "手动调整");
		m_tooltip.AddTool(GetDlgItem(IDC_MODIFY_RIGHT), "手动调整");
		m_tooltip.AddTool(GetDlgItem(IDC_KEY), "此处输入密钥");
		m_tooltip.AddTool(GetDlgItem(IDC_CIPHER_STRING), "请输入密文");
		m_tooltip.AddTool(GetDlgItem(IDC_PLAIN_STRING), "请输入明文");
		// 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>");	}
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CCaesarDlg::ShowSub(int shift)
{
	CString strSub = _T("");
	strSub.Format("%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c " ,
		( 0 + shift ) % 26 + 'A' ,
		( 1 + shift ) % 26 + 'A' ,
		( 2 + shift ) % 26 + 'A' ,
		( 3 + shift ) % 26 + 'A' ,
		( 4 + shift ) % 26 + 'A' ,
		( 5 + shift ) % 26 + 'A' ,
		( 6 + shift ) % 26 + 'A' ,
		( 7 + shift ) % 26 + 'A' ,
		( 8 + shift ) % 26 + 'A' ,
		( 9 + shift ) % 26 + 'A' ,
		( 10 + shift ) % 26 + 'A' ,
		( 11 + shift ) % 26 + 'A' ,
		( 12 + shift ) % 26 + 'A' ,
		( 13 + shift ) % 26 + 'A' ,
		( 14 + shift ) % 26 + 'A' ,
		( 15 + shift ) % 26 + 'A' ,
		( 16 + shift ) % 26 + 'A' ,
		( 17 + shift ) % 26 + 'A' ,
		( 18 + shift ) % 26 + 'A' ,
		( 19 + shift ) % 26 + 'A' ,
		( 20 + shift ) % 26 + 'A' ,
		( 21 + shift ) % 26 + 'A' ,
		( 22 + shift ) % 26 + 'A' ,
		( 23 + shift ) % 26 + 'A' ,
		( 24 + shift ) % 26 + 'A' ,
		( 25 + shift ) % 26 + 'A' );
	this->SetDlgItemText(IDC_SUB,strSub);
}

void CCaesarDlg::OnModifyRight() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	CString temp = _T("");
	GetDlgItemText(IDC_SUB,temp);
	m_key = temp.GetAt(0) - 'A';
	m_key = ( m_key - 1 + 26) % 26;
	char strr[100000];
	GetDlgItemText(IDC_PLAIN_STRING,strr,100000);
	CString str = strr;
	int length = str.GetLength();
	LPTSTR p = str.GetBuffer(length);	//将CString变为字符数组,便于操作
	for ( int j = 0 ; j < length ; j ++ )
	{
		if ( p[j] >= 'A' && p[j] <= 'Z' )	//密文字母 -> 密文字母 + cont
			p[j] = ( p[j] - 'A' + (m_key%26) ) % 26 + 'A';
		else if ( p[j] >= 'a' && p[j] <= 'z' )
			p[j] = ( p[j] - 'a' + (m_key%26) ) % 26 + 'a';
		else
			continue;
	}
	str.ReleaseBuffer();
	GetDlgItem(IDC_CIPHER_STRING)->SetWindowText(str);
	ShowSub ( m_key );
	SetDlgItemInt(IDC_KEY,m_key,false);
}

void CCaesarDlg::OnModifyLeft() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	CString temp = _T("");
	GetDlgItemText(IDC_SUB,temp);
	m_key = temp.GetAt(0) - 'A';
	m_key = ( m_key + 1 ) % 26;
	char strr[100000];
	GetDlgItemText(IDC_PLAIN_STRING,strr,100000);
	CString str = strr;
	int length = str.GetLength();
	LPTSTR p = str.GetBuffer(length);	//将CString变为字符数组,便于操作
	for ( int j = 0 ; j < length ; j ++ )
	{
		if ( p[j] >= 'A' && p[j] <= 'Z' )	//密文字母 -> 密文字母 + cont
			p[j] = ( p[j] - 'A' + (m_key%26) ) % 26 + 'A';
		else if ( p[j] >= 'a' && p[j] <= 'z' )
			p[j] = ( p[j] - 'a' + (m_key%26) ) % 26 + 'a';
		else
			continue;
	}
	str.ReleaseBuffer();
	GetDlgItem(IDC_CIPHER_STRING)->SetWindowText(str);
	ShowSub ( m_key );
	SetDlgItemInt(IDC_KEY,m_key,false);
}

BOOL CCaesarDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	if ( pWnd == GetDlgItem(IDC_RESET) 
		|| pWnd == GetDlgItem(IDC_ENCIPHER)
		|| pWnd == GetDlgItem(IDC_DECIPHER)
		|| pWnd == GetDlgItem(IDC_MODIFY_LEFT)
		|| pWnd == GetDlgItem(IDC_MODIFY_RIGHT)
		)
		::SetCursor(m_hHand);
	else if ( pWnd == GetDlgItem(IDC_PLAIN_STRING)
		|| pWnd == GetDlgItem(IDC_CIPHER_STRING)
		|| pWnd == GetDlgItem(IDC_KEY)
		)
		::SetCursor(m_hBeam);
	else if ( nHitTest == HTCAPTION )
		::SetCursor(m_hMove);
	else
		::SetCursor(m_hArrow);
	return true;
	//return CDialog::OnSetCursor(pWnd, nHitTest, message);
}

BOOL CCaesarDlg::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.
}

⌨️ 快捷键说明

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