www.pudn.com > CipherSystem.rar > VigenereCipherSystem.cpp


#include "stdafx.h" 
#include "VigenereCipherSystem.h" 
 
////////////////////////////////////////////////////////////////////////////////////// 
////////////////        Implement class of VigenereEncryptSystem    ////////////////// 
////////////////////////////////////////////////////////////////////////////////////// 
 
bool VigenereEncryptSystem::GetKey(const string& str) 
{ 
	keyWord = ""; 
	int i; 
	for (i=0; i<(int)str.length(); i++) 
	{ 
		if (str[i] >= 'A' && str[i] <= 'Z') keyWord += str[i] + 32; 
		else if (str[i] >= 'a' && str[i] <= 'z') keyWord += str[i]; 
	} 
 
	if (keyWord.length() == 0) return false; 
	else return true; 
} 
 
void VigenereEncryptSystem::Encrypt() 
{ 
	int len = (int)keyWord.length(); 
 
	int i; 
	for (i=0; i<(int)text.length(); i++) 
	{ 
		char col = keyWord[i % len]; 
		text[i] = (text[i] + col - 2 * 97) % 26 + 97; 
	} 
	return; 
} 
 
void VigenereEncryptSystem::Decrypt() 
{ 
	int len = (int)keyWord.length(); 
 
	int i; 
	for (i=0; i<(int)text.length(); i++) 
	{ 
		char row = keyWord[i % len]; 
		text[i] = (text[i] + 26 - row) % 26 + 97; 
	} 
	return; 
} 
 
//////////////////////////////////////////////////////////////////////////////////////