Playfair Cipher Algorithm Program in C/C++

In cryptography, a cipher (or cypher) is an algorithm for performing encryption or decryption—a series of well-defined steps that can be followed as a procedure. An alternative, less common term is encipherment. To encipher or encode is to convert information into cipher or code. In common parlance, “cipher” is synonymous with “code“, as they are both a set of steps that encrypt a message; however, the concepts are distinct in cryptography, especially classical cryptography.

Codes generally substitute different length strings of character in the output, while ciphers generally substitute the same number of characters as are input. There are exceptions and some cipher systems may use slightly more, or fewer, characters when output versus the number that was input.

In this post, we will discuss the Playfair Cipher. The Playfair cipher is a cryptographic technique that is used to encrypt/decrypt a message. The technique encrypts pairs of letters (bigrams or digrams), instead of single letters as in the simple substitution cipher and rather more complex Vigenère cipher systems then in use. 

Working

The Playfair cipher uses a 5 by 5 table of letters. To generate the key, we will first fill the table row-wise with the letters of the key. We omit the repeating letters. After this, we fill the table with the remaining letters. We usually omit the letter i or j so that the number of letters in the table is 25. After the table is generated, we divide the message into the pairs of 2. Then for each pair, we look up the position of the letters in the table. Suppose the pair is XY and X is at position (x1,y1) and Y is at position (x2,y2), we choose the letters which are at position (x2,y1) and (x1,y2). If the pairs are (x,y1) and (x,y2), then we choose the letters at (x,y1+1) and (x,y2+1).
If the pairs are (x1,y) and (x2,y), then we choose the letters at (x1+1,y) and (x2+1,y).

For example, if the message is “helloworld” and the key is “test”. We will generate the following table:

t e s a b 
c d f g h 
i k l m n 
o p q r u 
v w x y z

The original message and the encrypted message will be:

original message:  he lx ow or ld
encrypted message: db qs pv pu kf

We will use C++ to write this algorithm due to the standard template library support. Hence, we will write the program of the Playfair Cipher algorithm in C++, although, it’s very similar to C.

Encryption

INPUT:
line 1: message
line 2: key

OUTPUT:
line 1: Encrypted message

The following is the Playfair Cipher encryption algorithm program in C++.

#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
int main(){
	int i,j,k,n;
	cout<<"Enter the message"<<endl;
	string s,origin;
	getline(cin,origin);
	cout<<"Enter the key"<<endl;
	string key;
	cin>>key;
	for(i=0;i<origin.size();i++){
		if(origin[i]!=' ')
			s+= origin[i];
	}
	vector<vector<char> > a(5,vector<char>(5,' '));
	n=5;
	map<char,int> mp;
	k=0;
	int pi,pj;
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			while(mp[key[k]]>0&&k<key.size()){
				k++;
			}
			if(k<key.size()){
				a[i][j]=key[k];
				mp[key[k]]++;
				pi=i;
				pj=j;
			}
			if(k==key.size())
			break;
		}
		if(k==key.size())
			break;
	}
	k=0;
	for(;i<n;i++){
		for(;j<n;j++){
			while(mp[char(k+'a')]>0&&k<26){
				k++;
			}
			if(char(k+'a')=='j'){
				j--;
				k++;
				continue;
			}
			if(k<26){
				a[i][j]=char(k+'a');
				mp[char(k+'a')]++;
			}
		}
		j=0;
	}

	string ans;
	if(s.size()%2==1)
		s+="x";
	for(i=0;i<s.size()-1;i++){
		if(s[i]==s[i+1])
			s[i+1]='x';
	}

	map<char,pair<int,int> > mp2;

	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			mp2[a[i][j]] = make_pair(i,j);
		}
	}
	
	for(i=0;i<s.size()-1;i+=2){
		int y1 = mp2[s[i]].first;
		int x1 = mp2[s[i]].second;
		int y2 = mp2[s[i+1]].first;
		int x2 = mp2[s[i+1]].second;
		if(y1==y2){
			ans+=a[y1][(x1+1)%5];
			ans+=a[y1][(x2+1)%5];
		}
		else if(x1==x2){
			ans+=a[(y1+1)%5][x1];
			ans+=a[(y2+1)%5][x2];	
		}
		else {
			ans+=a[y1][x2];
			ans+=a[y2][x1];
		}
	}
	cout<<ans<<'\n';
	return 0;
}

OUTPUT:

Enter the message
helloworld
Enter the key
test
dbqspvpukf

Decryption

INPUT:
line 1: message
line 2: key

OUTPUT:
line 1: decrypted message

The following is the Playfair Cipher decryption algorithm program in C++.

#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
int main(){
	int i,j,k,n;
	cout<<"Enter the encrypted message\n";
	string s;
	cin>>s;
	cout<<"Enter the key\n";
	string key;
	cin>>key;
	vector<vector<char> > a(5,vector<char>(5,' '));
	n=5;
	map<char,int> mp;
	k=0;
	int pi,pj;
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			while(mp[key[k]]>0&&k<key.size()){
				k++;
			}
			if(k<key.size()){
				a[i][j]=key[k];
				mp[key[k]]++;
				pi=i;
				pj=j;
			}
			if(k==key.size())
			break;
		}
		if(k==key.size())
			break;
	}
	k=0;
	for(;i<n;i++){
		for(;j<n;j++){
			while(mp[char(k+'a')]>0&&k<26){
				k++;
			}
			if(char(k+'a')=='j'){
				j--;
				k++;
				continue;
			}
			if(k<26){
				a[i][j]=char(k+'a');
				mp[char(k+'a')]++;
			}
		}
		j=0;
	}

	string ans;

	map<char,pair<int,int> > mp2;

	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			mp2[a[i][j]] = make_pair(i,j);
		}
	}
	for(i=0;i<s.size()-1;i+=2){
		int y1 = mp2[s[i]].first;
		int x1 = mp2[s[i]].second;
		int y2 = mp2[s[i+1]].first;
		int x2 = mp2[s[i+1]].second;
		if(y1==y2){
			ans+=a[y1][(x1-1)%5];
			ans+=a[y1][(x2-1)%5];
		}
		else if(x1==x2){
			ans+=a[(y1-1)%5][x1];
			ans+=a[(y2-1)%5][x2];	
		}
		else {
			ans+=a[y1][x2];
			ans+=a[y2][x1];
		}
	}
	if(ans[ans.size()-1]=='x')
		ans[ans.size()-1]='\0';

	for(i=1;i<ans.size();i++){
		if(ans[i]=='x')
			ans[i]=ans[i-1];
	}
	
	cout<<ans<<'\n';




	return 0;
}

OUTPUT:

Enter the encrypted message
dbqspvpukf
Enter the key
test
heloworld

Let us know in the comments if you are having any questions regarding this cryptography cipher Algorithm.

And if you found this post helpful, then please help us by sharing this post with your friends. Thank You

Default image
Jazib
Android Developer | Competitive Programmer

Leave a Reply