NAME: Aaima Faisal Seat No: B21110106001
Play Fair Cipher Algorithm
The Playfair cipher was the first practical digraph substitution cipher. The scheme was invented in 1854
by Charles Wheatstone but was named after Lord Playfair who promoted the use of the cipher. In
playfair cipher unlike traditional cipher we encrypt a pair of alphabets(digraphs) instead of a single
alphabet.
It was used for tactical purposes by British forces in the Second Boer War and in World War I and for the
same purpose by the Australians during World War II. This was because Playfair is reasonably fast to use
and requires no special equipment.
The Playfair Cipher Encryption Algorithm:
The Algorithm consists of 2 steps:
1. Generate the key Square(5×5):
The key square is a 5×5 grid of alphabets that acts as the key for encrypting the
plaintext. Each of the 25 alphabets must be unique and one letter of the alphabet
(usually J) is omitted from the table (as the table can hold only 25 alphabets). If the
plaintext contains J, then it is replaced by I.
The initial alphabets in the key square are the unique alphabets of the key in the order
in which they appear followed by the remaining letters of the alphabet in order.
2. Algorithm to encrypt the plain text:
The plaintext is split into pairs of two letters (digraphs). If there is an odd number of
letters, a Z is added to the last letter.
NAME: Aaima Faisal Seat No: B21110106001
Practical # 5
Objective:
Write a program for Play Fair Cipher.
Source Code:
// Generate Playfair table
function generatePlayfairTable(key) {
key = [Link]().replace(/J/g, 'I');
let keySet = new Set(key);
const alphabet = 'ABCDEFGHIKLMNOPQRSTUVWXYZ';
let remainingLetters = [Link](alphabet).filter(c => );
let table = [];
let combinedKey = key + [Link]('');
for (let i = 0; i < 5; i++) {
[Link]([Link](i * 5, i * 5 + 5).split(''));
return table;
// Display the 5x5 matrix on the frontend
function generateMatrix() {
let key = [Link]('key').value;
let table = generatePlayfairTable(key);
let matrixContainer = [Link]('matrix-container');
NAME: Aaima Faisal Seat No: B21110106001
[Link] = ''; // Clear the previous matrix
[Link](row => {
[Link](cell => {
let cellElement = [Link]('div');
[Link] = 'matrix-cell';
[Link] = cell;
[Link](cellElement);
});
});
// Search position of character in table
function searchPosition(table, char) {
for (let i = 0; i < 5; i++) {
for (let j = 0; j < 5; j++) {
if (table[i][j] === char) {
return { row: i, col: j };
// Prepare and encrypt text
function encryptPlayfair() {
NAME: Aaima Faisal Seat No: B21110106001
let key = [Link]('key').value;
let plaintext = [Link]('plaintext').[Link]().replace(/J/g, 'I');
// Add padding X if odd length
if ([Link] % 2 !== 0) {
plaintext += 'X';
let table = generatePlayfairTable(key);
let ciphertext = '';
for (let i = 0; i < [Link]; i += 2) {
let char1 = plaintext[i];
let char2 = plaintext[i + 1];
let pos1 = searchPosition(table, char1);
let pos2 = searchPosition(table, char2);
if ([Link] === [Link]) {
ciphertext += table[[Link]][([Link] + 1) % 5] + table[[Link]][([Link] + 1) % 5];
} else if ([Link] === [Link]) {
ciphertext += table[([Link] + 1) % 5][[Link]] + table[([Link] + 1) % 5][[Link]];
} else {
ciphertext += table[[Link]][[Link]] + table[[Link]][[Link]];
[Link]('output').value = ciphertext;
NAME: Aaima Faisal Seat No: B21110106001
Output:
For Encoding Without Matrix Generation:
NAME: Aaima Faisal Seat No: B21110106001
For Encoding With Matrix Generation:
NAME: Aaima Faisal Seat No: B21110106001
For Decoding: