0% found this document useful (0 votes)
12 views9 pages

D22CS096 CRNS P2

This document is a laboratory manual for the Cryptography & Network Security course (CS361) at the Faculty of Technology and Engineering, detailing the implementation of the Playfair cipher for message decryption. It includes the cipher encryption algorithm, code for both encryption and decryption, and a cryptanalysis section discussing the strengths and weaknesses of the Playfair cipher. The practical aims to demonstrate the application of the cipher and its cryptanalysis, concluding with successful implementation.

Uploaded by

Karan
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)
12 views9 pages

D22CS096 CRNS P2

This document is a laboratory manual for the Cryptography & Network Security course (CS361) at the Faculty of Technology and Engineering, detailing the implementation of the Playfair cipher for message decryption. It includes the cipher encryption algorithm, code for both encryption and decryption, and a cryptanalysis section discussing the strengths and weaknesses of the Playfair cipher. The practical aims to demonstrate the application of the cipher and its cryptanalysis, concluding with successful implementation.

Uploaded by

Karan
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/ 9

Cryptography & Network Security - CS361 D22CS096- KARAN DER

Faculty of Technology and Engineering


Chandubhai S Patel Institute of Technology
Department of Computer Science & Engineering
Laboratory Manual
Academic Year : 2022-23 Semester : 6

Course code : CS361 Course name : Cryptography & Network Security

Name : KARAN DER Seat Number : D22CS096

Practical - 2

Date of Performance:

Aim: Soldier from field wants to send a message to base. Implement the cipher to
decrypt the message. Using Playfair, Decrypt the message: BWPNRSMUALAW, Use
key : pearlharbour

[A] Cipher Encryption Algorithm:


● Mention background equations and introduction of algorithms.
● Code and output of Cipher

[B] Cryptanalysis of Cipher:


● Mention in points attacks that can occur and the corresponding code.
● Also take screenshots of Output

[A] Introduction to Playfair Cipher

The Playfair cipher was the first practical digraph substitution cipher. In playfair cipher, unlike
traditional cipher we encrypt a pair of alphabets(digraphs) instead of a single alphabet.

The plaintext is split into pairs of two letters (digraphs). If there is an odd number of letters, X
is added to the last letter.

Department of Computer Science & Engineering Page 1


Cryptography & Network Security - CS361 D22CS096- KARAN DER

● Pair cannot be made with the same letter. Break the letter in single and add a bogus
letter to the previous letter.

● If the letter is standing alone in the process of pairing, then add an extra bogus letter
with the alone letter

Rules for Encryption: -

1. If both the letters are in the same column: Take the letter below each one (going
back to the top if at the bottom).

2. If both the letters are in the same row: Take the letter to the right of each one (going
back to the leftmost if at the rightmost position).

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

Code:

1. For Encryption
def toLowerCase(text):
return text.lower()

def removeSpaces(text):
newText = ""
for i in text:
if i == " ":
continue
else:
newText = newText + i
return newText

def Diagraph(text):
Diagraph = []
group = 0
for i in range(2, len(text), 2):
Diagraph.append(text[group:i])
group = i
Diagraph.append(text[group:])
return Diagraph

def FillerLetter(text):
k = len(text)
if k % 2 == 0:
for i in range(0, k, 2):
if text[i] == text[i+1]:
new_word = text[0:i+1] + str('x') + text[i+1:]
new_word = FillerLetter(new_word)
break
else:
new_word = text
else:
for i in range(0, k-1, 2):
if text[i] == text[i+1]:

Department of Computer Science & Engineering Page 2


Cryptography & Network Security - CS361 D22CS096- KARAN DER

new_word = text[0:i+1] + str('x') + text[i+1:] new_word =


FillerLetter(new_word)
break
else:
new_word = text
return new_word

list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l', 'm','n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

def generateKeyTable(word, list1): key_letters = []


for i in word:
if i not in key_letters:
key_letters.append(i)

compElements = [] for i
in key_letters:
if i not in compElements:
compElements.append(i)
for i in list1:
if i not in compElements:
compElements.append(i)

matrix = []
while compElements != []: matrix.append(compElements[:5])
compElements = compElements[5:]
return matrix

def search(mat, element): for i in


range(5):
for j in range(5):
if (mat[i][j] == element): return
i, j

def encrypt_RowRule(matr, e1r, e1c, e2r, e2c): char1 = ''


if e1c == 4:
char1 = matr[e1r][0] else:
char1 = matr[e1r][e1c+1] char2 = ''
if e2c == 4:
char2 = matr[e2r][0] else:
char2 = matr[e2r][e2c+1] return
char1, char2

def encrypt_ColumnRule(matr, e1r, e1c, e2r, e2c): char1 = ''


if e1r == 4:
char1 = matr[0][e1c] else:

Department of Computer Science & Engineering Page 3


Cryptography & Network Security - CS361 D22CS096- KARAN DER

char1 = matr[e1r+1][e1c] char2 = ''


if e2r == 4:
char2 = matr[0][e2c] else:
char2 = matr[e2r+1][e2c] return
char1, char2

def encrypt_RectangleRule(matr, e1r, e1c, e2r, e2c): char1 = ''


char1 = matr[e1r][e2c] char2 = ''
char2 = matr[e2r][e1c] return
char1, char2

def encryptByPlayfairCipher(Matrix, plainList):


CipherText = []
for i in range(0, len(plainList)): c1 = 0
c2 = 0
ele1_x, ele1_y = search(Matrix, plainList[i][0]) ele2_x,
ele2_y = search(Matrix, plainList[i][1]) if ele1_x == ele2_x:
c1, c2 = encrypt_RowRule(Matrix, ele1_x, ele1_y, ele2_x, ele2_y) elif ele1_y ==
ele2_y:
c1, c2 = encrypt_ColumnRule(Matrix, ele1_x, ele1_y, ele2_x, ele2_y) else:
c1, c2 = encrypt_RectangleRule(
Matrix, ele1_x, ele1_y, ele2_x, ele2_y) cipher = c1
+ c2
CipherText.append(cipher) return
CipherText

text_Plain = 'enemyishere'
text_Plain = removeSpaces(toLowerCase(text_Plain)) PlainTextList =
Diagraph(FillerLetter(text_Plain))
if len(PlainTextList[-1]) != 2: PlainTextList[-1] =
PlainTextList[-1]+'x'
key = "pearlharbour" key =
toLowerCase(key)
Matrix = generateKeyTable(key, list1) print()
print("=========== Playfair Cipher ===========") print()
print("Key:", key)
print("Plain Text:", text_Plain)
CipherList = encryptByPlayfairCipher(Matrix, PlainTextList) CipherText = ""
for i in CipherList:
CipherText += i print("CipherText:",
CipherText)
print(" ")

Department of Computer Science & Engineering Page 4


Cryptography & Network Security - CS361 D22CS096- KARAN DER

2. For Decryption
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 30

// Convert all the characters


// of a 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;
}
}

// Remove all spaces in a string


// can be extended to remove punctuation
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;
}

// generates the 5x5 key square


void generateKeyTable(char key[], int ks, char keyT[5][5])
{
int i, j, k, flag = 0, *dicty;

// a 26 character hashmap
// to store count of the alphabet
dicty = (int *)calloc(26, sizeof(int));

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;

Department of Computer Science & Engineering Page 5


Cryptography & Network Security - CS361 D22CS096- KARAN DER

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;
}
}
}
}

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

Department of Computer Science & Engineering Page 6


Cryptography & Network Security - CS361 D22CS096- KARAN DER

// Function to find the modulus with 5 int


mod5(int a)
{
if (a < 0)
a += 5;
return (a % 5);
}

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


}

// Driver code

Department of Computer Science & Engineering Page 7


Cryptography & Network Security - CS361 D22CS096- KARAN DER

int main()
{
char str[SIZE], key[SIZE];
printf("\n");
printf("=========== Playfair Cipher ===========\n");
// Key to be encrypted
strcpy(key, "pearlharbour");
printf("Key text: %s\n", key);

// Ciphertext to be decrypted
strcpy(str, "bwpnrsmualaw");
printf("Cipher text: %s\n", str);

// encrypt using Playfair Cipher


decryptByPlayfairCipher(str, key);

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


printf(" ");
return 0;
}

Output:
For Encryption:

For Decryption:

Department of Computer Science & Engineering Page 8


Cryptography & Network Security - CS361 D22CS096- KARAN DER

[B] Cryptanalysis of Playfair Cipher

● It is significantly harder to break since the frequency analysis technique used to break
simple substitution ciphers is difficult but still can be used on (25*25) = 625 digraphs
rather than 25 monographs which is difficult.

● Frequency analysis thus requires more cipher text to crack the encryption.

● An interesting weakness is the fact that a digraph in the ciphertext (AB) and it’s reverse
(BA) will have corresponding plaintexts like UR and RU (and also ciphertext UR and RU
will correspond to plaintext AB and BA, i.e. the substitution is self-inverse).

● That can easily be exploited with the aid of frequency analysis, if the language of the
plaintext is known.

● Another disadvantage is that playfair cipher is a symmetric cipher thus same key is used
for both encryption and decryption.

Conclusion: -
By the end of this practical, we have successfully implemented Playfair Cipher and we have
performed the cryptanalysis of Playfair cipher at the end.

Grade / Marks Sign of Lab Teacher with Date


( / 10)

Department of Computer Science & Engineering Page 9

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