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


#include "stdafx.h" 
#include "KeyWordCipherSystem.h" 
 
////////////////////////////////////////////////////////////////////////////////////// 
////////////////        Implement class of KeyWordEncryptSystem     ////////////////// 
////////////////////////////////////////////////////////////////////////////////////// 
 
KeyWordEncryptSystem::KeyWordEncryptSystem() 
{ 
	int i; 
	for(i=0; i<26; i++) markArray[i] = false; 
} 
 
bool KeyWordEncryptSystem::GetKey(string& keyWord, char start) 
{ 
	if (start >= 'A' && start <= 'Z') start += 32; 
 
	if (start >= 'a' && start <= 'z') 
	{ 
		int i = int(start - 97); 
		int j; 
		for (j=0; j<(int)keyWord.length(); j++) 
		{ 
			if (keyWord[j] >= 'A' && keyWord[j] <= 'Z') keyWord[j] += 32; 
			if (keyWord[j] >= 'a' && keyWord[j] <= 'z' && !markArray[keyWord[j] - 97]) 
			{ 
				int temp = i % 26; 
				encryptArray[temp] = keyWord[j]; 
				 
				markArray[keyWord[j] - 97] = true; 
				decryptArray[keyWord[j] - 97] = char(temp + 97); 
				i++; 
			} 
		} 
 
		for (j=0; j<26; j++) 
		{ 
			if (!markArray[j]) 
			{ 
				int temp = i % 26; 
				encryptArray[temp] = char(j + 97); 
 
				markArray[j] = true; 
				decryptArray[j] = char(temp + 97); 
				i++; 
			} 
		} 
		return true; 
	} 
 
	else return false; 
} 
 
void KeyWordEncryptSystem::Encrypt() 
{ 
	int j; 
	for (j=0; j<(int)text.length(); j++) text[j] = encryptArray[text[j] - 97]; 
} 
 
void KeyWordEncryptSystem::Decrypt() 
{ 
	int i; 
	for (i=0; i<(int)text.length(); i++) text[i] = decryptArray[text[i] - 97]; 
} 
 
//////////////////////////////////////////////////////////////////////////////////////