watery87 0 Light Poster

hi guys, im currently working on playfair cipher. However i got a serious problem in implementing it on certain conditions

You can find background information here http://en.wikipedia.org/wiki/Playfair_cipher.
The problem im facing is that when Playfair cipher decrypts by bigrams and encounters the same character e.g 'ee', my program adds a 'x' character to them. 'X' character is chosen as it is one of the least common appearing character in the alphabet (z will do fine too). The problem is that when i input plaintext of 'xxxx', the program goes haywire mode while decrypting. When i input plaintext of 'yyyy', decryption of it is 'yxyxyxyx' which is completely different. I would like to ask

- Is there anyway to remove only 'x' character without affecting the original text

- Is there a way to solve the 'xxxx' problem

#include<iostream>
#include<string>
using namespace std;

const int num=50;

char matrix[5][5],text[num];
string key,key2;
string plaintext,plaintext1;
string cryptograph;
char character[26]="abcdefghiklmnopqrstuvwxyz";
string temp;

int makematrix(string key)
{
	int j=0;
	int k=-1;
	fflush(stdin);
	for(int i=0;i<key.length();i++)
	{
		for(int n=0;n<=i-1;n++)
		{
			if(key[n]==key[i])
			{  i++;n=0;}
			if(i>=key.length())break;
		}
		if(i<key.length()){
		if(k<4){
			k++;
			matrix[j][k]=key[i];
			
		}
		else
		{
			k=-1;
			j++;
			k++;
			matrix[j][k]=key[i];
		}
		}

	}
	k++;
    int h=0;
	while(j<5)
	{
		if(k<5){
			for(int n=0;n<key.length();n++){
				if(character[h]==key[n]){h++;n=-1;}}
			matrix[j][k]=character[h];
			h++;
			k++;}
		else {
			j++;
			k=0;
		}

	}
	return 1;
}

void showmatrix()
{
	for(int i=0;i<5;i++)
	{
		for(int j=0;j<5;j++)
			cout<<matrix[i][j]<<" ";
	cout<<endl;
	}

}

void encrypt()
{
	int i=0;
	int j=0;
	int pos1[2];
	int pos2[2];
	for(int k=0;k<plaintext.length()-1;k=k+2)
		if(plaintext[k]==plaintext[k+1]&&plaintext[k+1]!='x')
		if(plaintext[k]==plaintext[k+1])
			plaintext.insert(k+1,"x");
    cout<<plaintext;
     for(int k=0;k<plaintext.length()-1;k=k+2)
	 {
		 for(i=0;i<5;i++){
			 for(j=0;j<5;j++)
			 {
				 if(matrix[i][j]==plaintext[k]){pos1[0]=i;pos1[1]=j;}
				 if(matrix[i][j]==plaintext[k+1]){pos2[0]=i;pos2[1]=j;}
			 }
		 }
/*
		 cout<<pos1[0]<<pos1[1]<<endl;
		 cout<<pos2[0]<<pos2[1]<<endl;
*/		

		 if(pos1[0]==pos2[0])                     //Same line
		 {
			 if(pos1[1]<pos2[1])
			 cryptograph=cryptograph+matrix[pos1[0]][(pos1[1]+1)%5]
	    		 +matrix[pos2[0]][(pos2[1]+1)%5];
			 else if(pos1[1]>pos2[1])
             cryptograph=cryptograph+matrix[pos2[0]][(pos1[1]+1)%5]
	    		 +matrix[pos2[0]][(pos2[1]+1)%5];
		 }
		 else if(pos1[1]==pos2[1])
		 {
			 if(pos1[0]<pos2[0])
			 cryptograph=cryptograph+matrix[(pos1[0]+1)%5][pos2[1]]
		    	 +matrix[(pos2[0]+1)%5][pos2[1]];
		 	 else if(pos1[0]>pos2[0])
             cryptograph=cryptograph+matrix[(pos1[0]+1)%5][pos1[1]]
		    	 +matrix[(pos2[0]+1)%5][pos1[1]];
		 }
		 else 
		 {
			 cryptograph=cryptograph+matrix[pos1[0]][pos2[1]]
	     		 +matrix[pos2[0]][pos1[1]];
		 }



		      

	 }
	 
	 cout<<"The cryptograph is:"<<endl;
	 cout<<cryptograph<<endl;

}

	 
void decrypt()
{
	int i=0;
	int j=0;
	int pos1[2];
	int pos2[2];


     for(int k=0;k<cryptograph.length()-1;k=k+2)
	 {
		 for(i=0;i<5;i++){
			 for(j=0;j<5;j++)
			 {
				 if(matrix[i][j]==cryptograph[k]){pos1[0]=i;pos1[1]=j;}
				 if(matrix[i][j]==cryptograph[k+1]){pos2[0]=i;pos2[1]=j;}
			 }
		 }
		 


		 if(pos1[0]==pos2[0])                     //Same line
		 {
			 if(pos1[1]<pos2[1])
			 plaintext1=plaintext1+matrix[pos1[0]][(pos1[1]+4)%5]
	    		 +matrix[pos2[0]][(pos2[1]+4)%5];
			 else if(pos1[1]>pos2[1])
             plaintext1=plaintext1+matrix[pos2[0]][(pos1[1]+4)%5]
	    		 +matrix[pos2[0]][(pos2[1]+4)%5];
		 }
		 else if(pos1[1]==pos2[1])
		 {
			 if(pos1[0]<pos2[0])
			 plaintext1=plaintext1+matrix[(pos1[0]+4)%5][pos2[1]]
		    	 +matrix[(pos2[0]+4)%5][pos2[1]];
		 	 else if(pos1[0]>pos2[0])
             plaintext1=plaintext1+matrix[(pos1[0]+4)%5][pos1[1]]
		    	 +matrix[(pos2[0]+4)%5][pos1[1]];
		 }
		 else 
		 {
			 plaintext1=plaintext1+matrix[pos1[0]][pos2[1]]
	     		 +matrix[pos2[0]][pos1[1]];
		 }
	
		 
    

		      

	 }
	 cout<<"The plaintextis:"<<endl;
	 cout<<plaintext1<<endl;
}

void main()
{
	int wrong=0;
	cout<<"***********playfair*********"<<endl;
	cout<<"please input your key(character string):"<<endl;
	while(1){
	   cin>>key;
	   for(int k=0;k<key.length();k++)
	   {
	    	if((key[k]<'a'||key[k]>'z')&&(key[k]<'A'||key[k]>'Z')) wrong=1;
	   }
    	if(wrong==1){cout<<"your input is wrong,please input again:"<<endl;wrong=0;}
    	else if(key.length()>25)cout<<"Sorry your input is too long,please input again,the number of your character must be less than 25"<<endl;
    	else break;
   	}

	for(int i=0;i<key.length();i++) //Combining i and j
   {
      if(key[i]=='j')
      key[i]='i';
   } 

	makematrix(key);

	cout<<"The matrix :"<<endl;
	showmatrix();
   
	fflush(stdin);
	cout<<"please input your plaintext:"<<endl;
	while(1){
	   cin>>plaintext;
	   for(int k=0;k<plaintext.length();k++)
	   {
	    	if((plaintext[k]<'a'||plaintext[k]>'z')&&(plaintext[k]<'A'||plaintext[k]>'Z')) wrong=1;
	   }
    	if(wrong==1){cout<<"your input is wrong,please input again:"<<endl;wrong=0;}
    	
    	else break;
   	}

	if((plaintext.length())%2!=0)plaintext=plaintext+"x";    

	for(int i=0;i<plaintext.length();i++)
		if(plaintext[i]=='j')plaintext[i]='i';                

	encrypt();
	cout<<endl<<endl<<endl;

    temp=plaintext;
	plaintext="";

	cout<<"please enter the key to decrypt:"<<endl;
	while(1){
	   cin>>key2;
	   for(int k=0;k<key2.length();k++)
	   {
	    	if((key2[k]<'a'||key[k]>'z')&&(key2[k]<'A'||key2[k]>'Z')) wrong=1;
	   }
    	if(wrong==1){cout<<"your input is wrong,please input again:"<<endl;wrong=0;}
    	else if(key2.length()>25)cout<<"Sorry your input is too long,please input again,the number of your character must be less than 25"<<endl;
    	else break;
   	}


	
	for(int i=0;i<key2.length();i++) 
   {
      if(key2[i]=='j')
      key2[i]='i';
   } 


	makematrix(key2);
	cout<<"The matrix :"<<endl;
	showmatrix();
     cout<<endl<<endl<<endl;
    decrypt();
    
	int exit;
	cout<<"press 1 to exit:"<<endl;
	cin>>exit;

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.