caeser.cpp

来自「1、对于凯撒密文」· C++ 代码 · 共 102 行

CPP
102
字号
// Caeser.cpp: implementation of the CCaeser class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "030300816.h"
#include "Caeser.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CCaeser::CCaeser()
{

}

CCaeser::~CCaeser()
{

}

void CCaeser::Encipher()
{
	CString m_tempString = m_plainString;
	CString m_temp = _T("");
//	m_tempString.MakeLower();
	int length = m_tempString.GetLength();
	for ( int i = 0 ; i < length ; i ++ )
		if ( m_tempString[i] <= 'z' && m_tempString[i] >= 'a' )
			m_temp += ( ( m_tempString[i]- 'a' + m_key ) % 26 + 'a' );
		else if ( m_tempString[i] <= 'Z' && m_tempString[i] >= 'A' )
			m_temp += ( ( m_tempString[i]- 'A' + m_key ) % 26 + 'A' );
		else
			m_temp += m_tempString[i];
	m_cipherString = m_temp;
/*	int length = m_plainString.GetLength() +1;
	unsigned char * cipherBlock = (unsigned char*)malloc(length);
	memset(cipherBlock, 0, length);
	memcpy(cipherBlock, m_plainString , length -1);
	for ( int i = 0 ; i < (length-1) ; i ++ )
	{
		if ( cipherBlock[i] == ' ' )
			continue;
		else if ( cipherBlock[i] <= 'z' && cipherBlock[i] >= 'a' )
			cipherBlock[i] = ( cipherBlock[i]- 'a' + m_key ) % 26 + 'a';
		else if ( cipherBlock[i] <= 'Z' && cipherBlock[i] >= 'A' )
			cipherBlock[i] = ( cipherBlock[i]- 'A' + m_key ) % 26 + 'A';
		else
		{
			MessageBox(NULL,"a-z,A-Z allowed only!",NULL,MB_OK);
			memset(cipherBlock, 0, length);
			break;
		}
	}
	m_cipherString = cipherBlock;
	free(cipherBlock);*/
}

void CCaeser::Decipher()
{
	CString m_tempString = m_cipherString;
	CString m_temp = _T("");
	m_tempString.MakeLower();
	int length = m_tempString.GetLength();
	for ( int i = 0 ; i < length ; i ++ )
		if ( m_tempString[i] <= 'z' && m_tempString[i] >= 'a' )
			m_temp += ( ( m_tempString[i]- 'a' + 26 - m_key ) % 26 + 'a' );
		else if ( m_tempString[i] <= 'Z' && m_tempString[i] >= 'A' )
			m_temp += ( ( m_tempString[i]- 'A' + 26 - m_key ) % 26 + 'A' );
		else
			m_temp += m_tempString[i];
	m_plainString = m_temp;
/*	int length = m_cipherString.GetLength() +1;
	unsigned char * cipherBlock = (unsigned char*)malloc(length);
	memset(cipherBlock, 0, length);
	memcpy(cipherBlock, m_cipherString, length -1);
	for ( int i = 0 ; i < (length-1) ; i ++ )
	{
		if ( cipherBlock[i] == ' ' )
			continue;
		else if ( cipherBlock[i] <= 'z' && cipherBlock[i] >= 'a' )
			cipherBlock[i] = ( cipherBlock[i]- 'a' + 26 - m_key ) % 26 + 'a';
		else if ( cipherBlock[i] <= 'Z' && cipherBlock[i] >= 'A' )
			cipherBlock[i] = ( cipherBlock[i]- 'A' + 26 - m_key ) % 26 + 'A';
		else
		{
			MessageBox(NULL,"a-z,A-Z allowed only!",NULL,MB_OK);
			memset(cipherBlock, 0, length);
			break;
		}
	}
	m_plainString = cipherBlock;
	free(cipherBlock);*/
}

⌨️ 快捷键说明

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