D22CS096 CRNS P2
D22CS096 CRNS P2
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
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.
● 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
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]:
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']
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
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(" ")
2. For Decryption
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 30
// a 26 character hashmap
// to store count of the alphabet
dicty = (int *)calloc(26, sizeof(int));
i = 0;
j = 0;
for (k = 0; k < ks; k++)
{
if (dicty[key[k] - 97] == 2)
{
dicty[key[k] - 97] -= 1;
if (a == 'j')
a = 'i';
else if (b == 'j') b =
'i';
// 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]];
}
}
}
// Key
ks = strlen(key);
ks = removeSpaces(key, ks);
toLowerCase(key, ks);
// ciphertext
ps = strlen(str);
toLowerCase(str, ps);
ps = removeSpaces(str, ps);
// Driver code
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);
Output:
For Encryption:
For Decryption:
● 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.