0% found this document useful (0 votes)
25 views7 pages

Playfair Cipher Algorithm

Practical on Playfair Cipher Algorithm

Uploaded by

Aaima Faisal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views7 pages

Playfair Cipher Algorithm

Practical on Playfair Cipher Algorithm

Uploaded by

Aaima Faisal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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 = key.toUpperCase().replace(/J/g, 'I');

let keySet = new Set(key);

const alphabet = 'ABCDEFGHIKLMNOPQRSTUVWXYZ';

let remainingLetters = Array.from(alphabet).filter(c => !keySet.has(c));

let table = [];

let combinedKey = key + remainingLetters.join('');

for (let i = 0; i < 5; i++) {

table.push(combinedKey.slice(i * 5, i * 5 + 5).split(''));

return table;

// Display the 5x5 matrix on the frontend

function generateMatrix() {

let key = document.getElementById('key').value;

let table = generatePlayfairTable(key);

let matrixContainer = document.getElementById('matrix-container');


NAME: Aaima Faisal Seat No: B21110106001

matrixContainer.innerHTML = ''; // Clear the previous matrix

table.forEach(row => {

row.forEach(cell => {

let cellElement = document.createElement('div');

cellElement.className = 'matrix-cell';

cellElement.textContent = cell;

matrixContainer.appendChild(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 = document.getElementById('key').value;

let plaintext = document.getElementById('plaintext').value.toUpperCase().replace(/J/g, 'I');

// Add padding X if odd length

if (plaintext.length % 2 !== 0) {

plaintext += 'X';

let table = generatePlayfairTable(key);

let ciphertext = '';

for (let i = 0; i < plaintext.length; i += 2) {

let char1 = plaintext[i];

let char2 = plaintext[i + 1];

let pos1 = searchPosition(table, char1);

let pos2 = searchPosition(table, char2);

if (pos1.row === pos2.row) {

ciphertext += table[pos1.row][(pos1.col + 1) % 5] + table[pos2.row][(pos2.col + 1) % 5];

} else if (pos1.col === pos2.col) {

ciphertext += table[(pos1.row + 1) % 5][pos1.col] + table[(pos2.row + 1) % 5][pos2.col];

} else {

ciphertext += table[pos1.row][pos2.col] + table[pos2.row][pos1.col];

document.getElementById('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:

You might also like