Professional Documents
Culture Documents
Playfair
Playfair
string key;
char playfair[5][5];
typedef struct {
int row, column;
} Pair;
int main()
{
string msg, cipherText;
cout << "Enter Key : ";
cin >> key;
key = removeDuplicates(key);
generateMatrix(key);
cipherText = encrypt(msg);
cout << endl << "Cipher Text : " << cipherText << endl;
string pt = decrypt(cipherText);
cout << "\nDecrypted Message : " << pt << endl;
return 0;
}
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;
}
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;
}
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;
}