Ins 5
Ins 5
Ins 5
Aim:
To implement a program to show encryption and decryption through Play-Fair Cipher.
Algorithm:
• First, a plaintext message is split into pairs of two letters (digraphs). If there
is an odd number of letters, a Z is added to the last letter. Let us say we
want to encrypt the message "Programming". It will be written as – Pr og
ra mm in gZ
• The rules of encryption are - o If both the letters are in the same column,
take the letter below each one (going back to the top if at the bottom)
o If both letters are in the same row, take the letter to the right of each
one (going back to the left if at the farthest right)
o If neither of the preceding two rules are true, form a rectangle with
the two letters and take the letters on the horizontal opposite corner
of the rectangle.
Code:
#include <bits/stdc++.h>
class Playfair
string key;
int find_in_key(char a)
if (a == key[i])
return i;
return -1;
if (x_a == x_b)
if (y_a == y_b)
if (x_a == x_b)
if (y_a == y_b)
else
public:
Playfair(string KEY)
key = KEY;
if (plainText.length() % 2 == 1)
plainText += "x";
{
pair<char, char> t = encipher_pair(plainText[i], plainText[i + 1]);
cipherText[i] = t.first;
cipherText[i + 1] = t.second;
return cipherText;
if (cipherText.length() % 2 == 1)
cipherText += "x";
plainText[i] = t.first;
plainText[i + 1] = t.second;
return plainText;
};
int main()
Playfair p(key);
string plainText;