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

📄 vigenera.cpp

📁 1、对于凯撒密文
💻 CPP
字号:
// Vigenera.cpp: implementation of the CVigenera class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "030300816.h"
#include "Vigenera.h"

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

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

CVigenera::CVigenera()
{

}

CVigenera::~CVigenera()
{

}

void CVigenera::Encipher()
{
	/*initial*/
	int length = m_plainString.GetLength() +1;
	unsigned char * cipherBlock = (unsigned char*)malloc(length);
	memset(cipherBlock, 0, length);
	memcpy(cipherBlock, m_plainString, length -1);
	m_key.MakeLower();
	int keyLength = m_key.GetLength() +1;
	unsigned char * Key = (unsigned char*)malloc(keyLength);
	memset(Key, 0, keyLength);
	memcpy(Key, m_key, keyLength -1);
	keyLength--;
	///////////
	for ( int i = 0 , j = 0 ; i < (length-1) ; i ++ )
	{
		if ( cipherBlock[i] == ' ' )
			continue;
		else if ( cipherBlock[i] <= 'z' && cipherBlock[i] >= 'a' )
			cipherBlock[i] = 
			( cipherBlock[i]- 'a' + Key[j++ % keyLength] - 'a') % 26 + 'a';
		else if ( cipherBlock[i] <= 'Z' && cipherBlock[i] >= 'A' )
			cipherBlock[i] = 
			( cipherBlock[i]- 'A' + Key[j++ % keyLength] - 'a') % 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 CVigenera::Decipher()
{
	/*initial*/
	int length = m_cipherString.GetLength() +1;
	unsigned char * cipherBlock = (unsigned char*)malloc(length);
	memset(cipherBlock, 0, length);
	memcpy(cipherBlock, m_cipherString, length -1);
	m_key.MakeLower();
	int keyLength = m_key.GetLength() +1;
	unsigned char * Key = (unsigned char*)malloc(keyLength);
	memset(Key, 0, keyLength);
	memcpy(Key, m_key, keyLength -1);
	keyLength--;
	///////////
	for ( int i = 0 , j = 0 ; i < (length-1) ; i ++ )
	{
		if ( cipherBlock[i] == ' ' )
			continue;
		else if ( cipherBlock[i] <= 'z' && cipherBlock[i] >= 'a' )
			cipherBlock[i] = 
			( cipherBlock[i]- 'a' - Key[j++ % keyLength] + 'a' + 26) % 26 + 'a';
		else if ( cipherBlock[i] <= 'Z' && cipherBlock[i] >= 'A' )
			cipherBlock[i] = 
			( cipherBlock[i]- 'A' - Key[j++ % keyLength] + 'a' + 26) % 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -