Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

#include <iostream>

using namespace std;

string key;
char playfair[5][5];

typedef struct {
int row, column;
} Pair;

string removeDuplicates(string key);


void generateMatrix(string key);
int search_char(string key, char c);
Pair getIndex(char c);
string encrypt(string msg);
string decrypt(string cipherText);
void splitMsg(string msg, string pairs[]);

int main()
{
string msg, cipherText;
cout << "Enter Key : ";
cin >> key;
key = removeDuplicates(key);
generateMatrix(key);

cout << "\nPlayfair Matrix : " << endl;


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

cout << "\nEnter Message : ";


cin >> msg;

cipherText = encrypt(msg);
cout << endl << "Cipher Text : " << cipherText << endl;

string pt = decrypt(cipherText);
cout << "\nDecrypted Message : " << pt << endl;
return 0;
}

string encrypt(string msg)


{
string cipherText;
string pairs[msg.length()];
splitMsg(msg, pairs);

cout << "\nSplitted Message :" << endl;


for(string temp : pairs)
{
if(temp == "")
break;
cout << temp << '\t';
Pair i1 = getIndex(temp[0]);
Pair i2 = getIndex(temp[1]);

if(i1.row == i2.row)
{
cipherText += playfair[i1.row][(i1.column+ 1)%5];
cipherText += playfair[i2.row][(i2.column + 1)%5];
}
else if(i1.column == i2.column)
{
cipherText += playfair[(i1.row + 1)%5][i1.column];
cipherText += playfair[(i2.row + 1)%5][i2.column];
}
else
{
cipherText += playfair[i1.row][i2.column];
cipherText += playfair[i2.row][i1.column];
}
}
cout << endl;
return cipherText;
}

string decrypt(string cipherText)


{
string msg;
string pairs[cipherText.length()];
splitMsg(cipherText, pairs);

cout << "\nSplitted Cipher Text :" << endl;


for(string temp : pairs)
{
if(temp == "")
break;
cout << temp << '\t';
Pair i1 = getIndex(temp[0]);
Pair i2 = getIndex(temp[1]);

if(i1.row == i2.row)
{
msg += playfair[i1.row][(i1.column+ 4)%5];
msg += playfair[i2.row][(i2.column + 4)%5];
}
else if(i1.column == i2.column)
{
msg += playfair[(i1.row + 4)%5][i1.column];
msg += playfair[(i2.row + 4)%5][i2.column];
}
else
{
msg += playfair[i1.row][i2.column];
msg += playfair[i2.row][i1.column];
}
}
cout << endl;
return msg;
}

void splitMsg(string msg, string pairs[])


{
string temp = msg;
for(int i=0; i<temp.length(); i++)
if(temp[i] == 'j')
temp[i] = 'i';

for(int i=0, j=0; i<temp.length(); j++, i+=2)


{
if(i == temp.length() - 1)
{
temp = temp + "z";
pairs[j] = temp.substr(i, 2);
}
else if(temp[i] == temp[i+1])
{
temp = temp.substr(0, i+1) + "x" + temp.substr(i+1, temp.length()-i);
pairs[j] = temp.substr(i, 2);
}
else{
pairs[j] = temp.substr(i, 2);
}
}
}

string removeDuplicates(string key)


{
int flag = 1;
string temp;
for(int i=0; i<key.length(); i++)
{
flag = 1;
if(key[i] == 'j')
key[i] = 'i';
for(int j=0; j<temp.length(); j++)
{
if(temp[j] == key[i])
{
flag = -1;
break;
}
}
if(flag == -1)
continue;
temp += key[i];
}
return temp;
}

void generateMatrix(string key)


{
int key_cnt = 0;
int character = 0;
for(int row=0; row<5; row++)
{
for(int col=0; col<5; col++)
{
if(key_cnt < key.length())
{
playfair[row][col] = key[key_cnt];
key_cnt++;
}
else
{
while(search_char(key, 'a'+character) != -1 || 'a'+character ==
'j')
character++;

playfair[row][col] = (char)('a' + character);


character++;
}
}
}
}

int search_char(string key, char c)


{
int i;
for(i=0; i<key.length(); i++)
if(key[i] == c)
return i;
return -1;
}

Pair getIndex(char c)
{
Pair index;
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
if(playfair[i][j] == c)
{
index.row = i;
index.column = j;
return index;
}
}
}
return index;
}

You might also like