0% found this document useful (0 votes)
44 views11 pages

ISassignment 2-20-SE-84

The document is an assignment submission that includes: 1) A question asking to implement the Playfair cipher for encryption and decryption in C++. 2) An answer explaining what the Playfair cipher is and how it works, including the encryption and decryption techniques. 3) C++ code implementing the Playfair cipher for encryption and decryption functions.

Uploaded by

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

ISassignment 2-20-SE-84

The document is an assignment submission that includes: 1) A question asking to implement the Playfair cipher for encryption and decryption in C++. 2) An answer explaining what the Playfair cipher is and how it works, including the encryption and decryption techniques. 3) C++ code implementing the Playfair cipher for encryption and decryption functions.

Uploaded by

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

INFORMATION AND SECURITY

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”.

What is a Playfair Cipher:


The Playfair cipher encryption technique can be used to encrypt or encode a message. It
operates exactly like typical encryption. The only difference is that it encrypts a digraph, or a
pair of two letters, as opposed to a single letter.
An initial 5×5 matrix key table is created. The plaintext encryption key is made out of the
matrix’s alphabetic characters. Be mindful that you shouldn’t repeat the letters. There are 26
alphabets, however, there are only 25 spaces in which we can place a letter. The matrix will
delete the extra letter because there is an excess of one letter (typically J). Despite this, J is
there in the plaintext before being changed to I.

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.

The following three scenarios are conceivable:

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.

ii) If a pair of letters (digraph) appears in the same column

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.
 

Decryption rules: Playfair Cipher:

Assuming the keyword is ‘Charles’, the decryption procedure would be as follows. A 5x5

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

// Function to convert the string to lowercase

void toLowerCase(char plain[], int ps)

{
int i;

for (i = 0; i < ps; i++) {

if (plain[i] > 64 && plain[i] < 91)

plain[i] += 32;

/ Function to remove all spaces in a string

int removeSpaces(char* plain, int ps)

int i, count = 0;

for (i = 0; i < ps; i++)

if (plain[i] != ' ')

plain[count++] = plain[i];

plain[count] = '\0';

return count;

// Function to generate the 5x5 key square

void generateKeyTable(char key[], int ks, char keyT[5][5])

int i, j, k, flag = 0;

// a 26 character hashmap

// to store count of the alphabet

int dicty[26] = { 0 };

for (i = 0; i < ks; i++) {

if (key[i] != 'j')

dicty[key[i] - 97] = 2;

dicty['j' - 97] = 1;

i = 0;

j = 0;

for (k = 0; k < ks; k++) {

if (dicty[key[k] - 97] == 2) {

dicty[key[k] - 97] -= 1;

keyT[i][j] = key[k];

j++;

if (j == 5) {
i++;

j = 0;

for (k = 0; k < 26; k++) {

if (dicty[k] == 0) {

keyT[i][j] = (char)(k + 97);

j++;

if (j == 5) {

i++;

j = 0;

// Function to search for the characters of a digraph

// in the key square and return their position

void search(char keyT[5][5], char a, char b, int arr[])

int i, j;

if (a == 'j')

a = 'i';

else if (b == 'j')

b = 'i';

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

for (j = 0; j < 5; j++) {

if (keyT[i][j] == a) {

arr[0] = i;

arr[1] = j;

else if (keyT[i][j] == b) {
arr[2] = i;

arr[3] = j;

// Function to find the modulus with 5

int mod5(int a) { return (a % 5); }

// Function to make the plain text length to be even

int prepare(char str[], int ptrs)

if (ptrs % 2 != 0) {

str[ptrs++] = 'z';

str[ptrs] = '\0';

return ptrs;

// Function for performing the encryption

void encrypt(char str[], char keyT[5][5], int ps)

int i, a[4];

for (i = 0; i < ps; i += 2) {

search(keyT, str[i], str[i + 1], a);

if (a[0] == a[2]) {

str[i] = keyT[a[0]][mod5(a[1] + 1)];

str[i + 1] = keyT[a[0]][mod5(a[3] + 1)];

else if (a[1] == a[3]) {

str[i] = keyT[mod5(a[0] + 1)][a[1]];

str[i + 1] = keyT[mod5(a[2] + 1)][a[1]];

else {

str[i] = keyT[a[0]][a[3]];

str[i + 1] = keyT[a[2]][a[1]];

}
}

// Function to encrypt using Playfair Cipher

void encryptByPlayfairCipher(char str[], char key[])

char ps, ks, keyT[5][5];

// 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);

generateKeyTable(key, ks, keyT);

encrypt(str, keyT, ps);}

// Function to decrypt

void decrypt(char str[], char keyT[5][5], int ps)

int i, a[4];

for (i = 0; i < ps; i += 2) {

search(keyT, str[i], str[i + 1], a);

if (a[0] == a[2]) {

str[i] = keyT[a[0]][mod5(a[1] - 1)];

str[i + 1] = keyT[a[0]][mod5(a[3] - 1)];

else if (a[1] == a[3]) {

str[i] = keyT[mod5(a[0] - 1)][a[1]];

str[i + 1] = keyT[mod5(a[2] - 1)][a[1]];

else {

str[i] = keyT[a[0]][a[3]];

str[i + 1] = keyT[a[2]][a[1]];

}
}

// Function to call decrypt

void decryptByPlayfairCipher(char str[], char key[])

char ps, ks, keyT[5][5];

// Key

ks = strlen(key);

ks = removeSpaces(key, ks);

toLowerCase(key, ks);

// ciphertext

ps = strlen(str);

toLowerCase(str, ps);

ps = removeSpaces(str, ps);

generateKeyTable(key, ks, keyT);

decrypt(str, keyT, ps);

int main()

char str[SIZE], key[SIZE];

printf("Enter the key text: ");

scanf("%s", key);

printf("Enter the plain text: ");

scanf(" %[^\n]s", str);

printf("Key text: %s\n", key);

printf("Plain text: %s\n", str);

encryptByPlayfairCipher(str, key);

printf("Cipher text: %s\n", str);

printf("Decrypting the cipher...\n");

decryptByPlayfairCipher(str, key);

printf("Plain text after decryption is: %s\n", str);

return 0;

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy