ISassignment 2-20-SE-84
ISassignment 2-20-SE-84
ASSIGHNMENT # 2
SUBMITTED TO:
Dr. FAWAD RIASAT
SUBMITTED BY:
KINZA YASEEN YOUNAS
REG NO:
20-SE-84
DATED: 7/04/2023
QUESTION:
Implement playfair cipher for encryption and decryption in c++.
ANSWER:
In cryptography, a cipher (or cypher) is an algorithm for performing encryption or decryption
“a series of well-defined steps that can be followed as a procedure”.
Encryption Technique:
Encryption rules: Playfair Cipher:
Split the plaintext first into digraphs (pairs of two letters). If the plaintext has an odd
number of letters, append the letter Z at the end. Even the text in the basic form of
The APPLE plaintext, for instance, consists of five letters. Consequently, it is not
possible to make a digraph. As a result, we will change the plaintext to Z by adding
the letter Z at the end.
Divide the plaintext into digraphs after that (pair of two letters). For any letter that
appears twice, place an X there (side by side). For plaintext GREET, the digraph will
be GR EX ET, but for plaintext JAZZ, the digraph will be JA ZX ZX.
To identify the cipher (encryption) text, create a 5*5 key-matrix or key-table and fill it
with alphabetic letters as follows:
The first row, from left to right, should include the letters for the supplied keyword
(ATHENS). If there are any duplicate letters in a keyword, avoid using them. This
means a letter will only be taken into account once. Fill in the remaining letters
alphabetically after that. Let’s create a 5*5 key-matrix for the keyword ATHENS.
There are no duplicate letters in the above matrix. The first row of letters (in green) stands in
for the keyword, while the remaining letter sets are organized alphabetically.
i) If two letters occur together in a row as a digraph, Each letter in the digraph, in
this case, should be replaced with the letter that is immediate to its right. If there is
no letter to the right, the first letter in the same row is considered to be the right
letter. Assuming Z is a letter for which the appropriate letter is required, T will be
the appropriate letter in this situation.
In this case, replace each letter of the digraph with the letters immediately below them. If
there is no letter below, wrap around to the top of the same column. Suppose, W is a letter
whose below letter is required, in such case, V will be below W.
iii) If a digraph (a group of two letters) appears in its row and column
To make a pair of letters appear in the 3*3 matrix, in this case, choose a 3*3 matrix from a
5*5 matrix. Considering that they occupy two square corners in the matrix that are on
different sides of a square. The other corner will act as a cipher for the given digraph.
Consider the situation where we must develop a cipher for the digraph HY. As can be seen, H
and Y are each arranged in separate rows and columns.so,
Decryption Technique:
Decrypting the Playfair cipher is as simple as doing the same process in reverse. The receiver has the
same key and can create the same key table, and then decrypt any messages made using that key.
matrix is drawn, and letters are filled in each cell, starting with the keyword, followed by the
letters in the alphabet. I/J are filled in the same cell. All repeating letters are removed, giving
us this matrix -
If both the letters are in the same column: Take the letter above each one (going
back to the bottom if at the top).
my would be replaced by dt
yr would be replaced by ty
If both the letters are in the same row: Take the letter to the left of each one (going
back to the rightmost if at the leftmost position).
sd would be replaced by eb
gi would be replaced by ng
If neither of the above rules is true: Form a rectangle with the two letters and take
the letters on the horizontal opposite corner of the rectangle.
gd would be replaced by me
do would be replaced by et
Implementation in C++:
#include "stdio.h"
#include <stdlib.h>
#include <string.h>
#define SIZE 30
{
int i;
plain[i] += 32;
int i, count = 0;
plain[count++] = plain[i];
plain[count] = '\0';
return count;
int i, j, k, flag = 0;
// a 26 character hashmap
int dicty[26] = { 0 };
if (key[i] != 'j')
dicty[key[i] - 97] = 2;
dicty['j' - 97] = 1;
i = 0;
j = 0;
if (dicty[key[k] - 97] == 2) {
dicty[key[k] - 97] -= 1;
keyT[i][j] = key[k];
j++;
if (j == 5) {
i++;
j = 0;
if (dicty[k] == 0) {
j++;
if (j == 5) {
i++;
j = 0;
int i, j;
if (a == 'j')
a = 'i';
else if (b == 'j')
b = 'i';
if (keyT[i][j] == a) {
arr[0] = i;
arr[1] = j;
else if (keyT[i][j] == b) {
arr[2] = i;
arr[3] = j;
if (ptrs % 2 != 0) {
str[ptrs++] = 'z';
str[ptrs] = '\0';
return ptrs;
int i, a[4];
if (a[0] == a[2]) {
else {
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
}
}
// Key
ks = strlen(key);
ks = removeSpaces(key, ks);
toLowerCase(key, ks);
// Plaintext
ps = strlen(str);
toLowerCase(str, ps);
ps = removeSpaces(str, ps);
ps = prepare(str, ps);
// Function to decrypt
int i, a[4];
if (a[0] == a[2]) {
else {
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
}
}
// Key
ks = strlen(key);
ks = removeSpaces(key, ks);
toLowerCase(key, ks);
// ciphertext
ps = strlen(str);
toLowerCase(str, ps);
ps = removeSpaces(str, ps);
int main()
scanf("%s", key);
encryptByPlayfairCipher(str, key);
decryptByPlayfairCipher(str, key);
return 0;
OUTPUT: