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


#include "stdafx.h" 
#include "PlayFairCipherSystem.h" 
 
 
 
////////////////////////////////////////////////////////////////////////////////////// 
////////////////        Implement class of PlayFairEncryptSystem    ////////////////// 
////////////////////////////////////////////////////////////////////////////////////// 
 
 
PlayFairEncryptSystem::PlayFairEncryptSystem() : NO_USE_CHAR('x') 
{ 
	int i; 
	for(i=0; i<26; i++) markArray[i] = false; 
	markArray[int('j' - 97)] = true; 
} 
 
void PlayFairEncryptSystem::GetData(const string& str) 
{ 
	text = ""; 
	string temp; 
 
	int i; 
	for (i=0; i<(int)str.length(); i++) 
	{ 
		if (str[i] >= 'A' && str[i] <= 'Z') temp += str[i] + 32; 
		else if (str[i] >= 'a' && str[i] <= 'z')  
		{ 
			if (str[i] == 'j') temp += 'i'; 
			else temp += str[i]; 
		} 
	} 
 
	if (temp.length() == 0) return; 
 
	text += temp[0];  
	for (i=1; i<(int)temp.length(); i++) 
	{ 
		if (text[text.length() - 1] == temp[i]) text += NO_USE_CHAR; 
		text += temp[i]; 
	} 
	if (text.length() % 2 != 0) text += NO_USE_CHAR; 
 
	return; 
} 
 
bool PlayFairEncryptSystem::GetKey(string& keyWord) 
{ 
	int row = 0; 
	int col = 0; 
	int j; 
	for (j=0; j<(int)keyWord.length(); j++) 
	{ 
		if (keyWord[j] >= 'A' && keyWord[j] <= 'Z') keyWord[j] += 32; 
		if (keyWord[j] == 'j') keyWord[j] = 'i'; 
		if (keyWord[j] >= 'a' && keyWord[j] <= 'z' && !markArray[keyWord[j] - 97]) 
		{ 
			encryptMatrix[row][col] = keyWord[j]; 
			charRow[keyWord[j] - 97] = row; 
			charCol[keyWord[j] - 97] = col; 
			col++; 
			if (col >= 5)  
			{ 
				col = 0;  
				row++; 
			} 
 
			markArray[keyWord[j] - 97] = true; 
		} 
	} 
 
	for (j=0; j<26; j++) 
	{ 
		if (!markArray[j]) 
		{ 
			encryptMatrix[row][col] = char(j + 97); 
			charRow[j] = row; 
			charCol[j] = col; 
			col++; 
			if (col >= 5)  
			{ 
				col = 0;  
				row++; 
			} 
			markArray[j] = true; 
		} 
	} 
 
	return true; 
} 
 
void PlayFairEncryptSystem::Encrypt() 
{ 
	if (text.length() == 0) return; 
	int forward = 1; 
	int follow = 0; 
 
	while (forward < (int)text.length()) 
	{ 
		int m1Row = charRow[int(text[follow] - 97)]; 
		int m1Col = charCol[int(text[follow] - 97)]; 
		int m2Row = charRow[int(text[forward] - 97)]; 
		int m2Col = charCol[int(text[forward] - 97)]; 
 
		if (m1Row == m2Row) 
		{ 
			text[follow] = encryptMatrix[m1Row][(m1Col + 1) % 5]; 
			text[forward] = encryptMatrix[m2Row][(m2Col + 1) % 5]; 
		} 
		else if (m1Col == m2Col) 
		{ 
			text[follow] = encryptMatrix[(m1Row + 1) % 5][m1Col]; 
			text[forward] = encryptMatrix[(m2Row + 1) % 5][m2Col]; 
		} 
		else 
		{ 
			text[follow] = encryptMatrix[m1Row][m2Col]; 
			text[forward] = encryptMatrix[m2Row][m1Col]; 
		} 
		forward += 2; 
		follow += 2; 
	} 
	return; 
} 
 
void PlayFairEncryptSystem::Decrypt() 
{ 
	int forward = 1; 
	int follow = 0; 
 
	while (forward < (int)text.length()) 
	{ 
		int m1Row = charRow[int(text[follow] - 97)]; 
		int m1Col = charCol[int(text[follow] - 97)]; 
		int m2Row = charRow[int(text[forward] - 97)]; 
		int m2Col = charCol[int(text[forward] - 97)]; 
 
		if (m1Row == m2Row) 
		{ 
			text[follow] = encryptMatrix[m1Row][(m1Col + 5 - 1) % 5]; 
			text[forward] = encryptMatrix[m2Row][(m2Col + 5 - 1) % 5]; 
		} 
		else if (m1Col == m2Col) 
		{ 
			text[follow] = encryptMatrix[(m1Row + 5 - 1) % 5][m1Col]; 
			text[forward] = encryptMatrix[(m2Row + 5 - 1) % 5][m2Col]; 
		} 
		else 
		{ 
			text[follow] = encryptMatrix[m1Row][m2Col]; 
			text[forward] = encryptMatrix[m2Row][m1Col]; 
		} 
		forward += 2; 
		follow += 2; 
	} 
	return; 
} 
 
//////////////////////////////////////////////////////////////////////////////////////