You are on page 1of 13

(CS361) Cryptography & Network Security 21DCS099

Faculty of Technology and Engineering


Devang Patel Institute of Advance Technology and Research
Department of Computer Science & Engineering
Date: 08 / 01 / 2024

Laboratory Manual
Academic Year : 2023-24 Semester : 6
Course code : CS361 Course name : Cryptography & Network Security
Name : Purvam Seat Number : 21DCS099
Prajapati

Practical - 2

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

[A] Cipher Encryption Algorithm:

 Please mention background equations and introduction of algorithm.


 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

7
(CS361) Cryptography & Network Security 21DCS099

Faculty of Technology and Engineering


Devang Patel Institute of Advance Technology and Research
Department of Computer Science & Engineering
Date: 08 / 01 / 2024

Laboratory Manual
Academic Year : 2022-23 Semester : 6
Course code : CS361 Course name : Cryptography & Network Security
Name : Purvam Seat Number : 21DCS099
Prajapati

(Hard copy to be prepared for this and to be brought in next lab for writing question
Answers)

Practical – 2

Introduction to the Playfair Cipher:

 The Playfair cipher is a classical symmetric encryption algorithm that operates on


pairs of letters, rather than individual letters.
 It uses a 5x5 matrix of letters, known as the key square, to encrypt and decrypt
messages.
 The matrix is filled with the unique letters of the keyword, followed by the remaining
letters of the alphabet (excluding 'J' which is often combined with 'I').
 The message is then encrypted by looking at pairs of letters in the plaintext and
applying specific rules based on their positions in the matrix.

Background Equations:

1. Key Square Generation:


 Input: The key used for encryption (in this case, "pearlharbour").
 Preprocessing: Remove any duplicate letters from the key and replace 'J' with 'I'.
 Unique Characters: Create a set of unique characters from the modified key.
8
(CS361) Cryptography & Network Security 21DCS099

 Key Square Matrix: Initialize a 5x5 matrix (key square) with empty slots.
 Filling Key Characters: Fill the matrix with the unique characters from the key,
row-wise. If not filled, add remaining alphabet characters row-wise, excluding 'J'.
 Matrix Structure: The resulting matrix serves as the key square for encryption and
decryption.
 Usage in Encryption/Decryption: During encryption and decryption, this key
square is used to find the positions of characters and determine substitutions based
on specific rules.

2. Pair Encryption Rules:


 If both letters of a pair are in the same row of the matrix, replace each letter with
the letter to its right (wrapping around to the start of the row if necessary).
 If both letters are in the same column, replace each letter with the letter below it
(wrapping around to the top if necessary).
 If the letters form a rectangle in the matrix, replace each letter with the letter in the
same row but opposite corner of the rectangle.

3. Handling Odd Number of Letters:


 If the message has an odd number of letters, a filler letter (usually 'X') is added to
make pairs.

Code for Playfair Cipher:

CASE 1 :

Numbers in ciphertext
Cipher text = “123BWPNRSMUALAW”

import java.util.Arrays;

public class PlayfairCipher {

static final int SIZE = 30;

public static void toLowerCase(char plain[], int ps) {


for (int i = 0; i < ps; i++) {
if (plain[i] > 64 && plain[i] < 91)
plain[i] += 32;
}
}

public static int removeSpaces(char[] plain, int ps) {


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

9
(CS361) Cryptography & Network Security 21DCS099

if (plain[i] != ' ')


plain[count++] = plain[i];
plain[count] = '\0';
return count;
}

public static void generateKeyTable(char[] key, int ks, char[][] keyT) {


int i, j, k, flag = 0;
int[] dicty = new int[26];

Arrays.fill(dicty, 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;
}
}
}
}

public static void search(char[][] keyT, char a, char b, int[] arr) {

10
(CS361) Cryptography & Network Security 21DCS099

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

public static int mod5(int a) {


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

public static void decrypt(char[] str, char[][] keyT, int ps) {


int[] a = new int[4];
for (int 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]];
}
}
System.out.println("Deciphered text: " + new String(str));
}

11
(CS361) Cryptography & Network Security 21DCS099

public static void decryptByPlayfairCipher(char[] str, char[] key) {


int ps, ks;
char[][] keyT = new char[5][5];

ks = key.length;
ks = removeSpaces(key, ks);
toLowerCase(key, ks);

ps = str.length;
toLowerCase(str, ps);
ps = removeSpaces(str, ps);

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


if (!Character.isAlphabetic(str[i])) {
System.out.println("Invalid input. The text should contain only alphabetic
characters.");
return;
}
}

generateKeyTable(key, ks, keyT);

decrypt(str, keyT, ps);


}

public static void main(String[] args) {


char[] str = new char[SIZE];
char[] key = new char[SIZE];

System.arraycopy("pearlharbour ".toCharArray(), 0, key, 0, "pearlharbour ".length());


System.out.println("Key Text: " + new String(key));

System.arraycopy("123BWPNRSMUALAW".toCharArray(), 0, str, 0,
"123BWPNRSMUALAW".length());
System.out.println("Cipher text: " + new String(str));

decryptByPlayfairCipher(str, key);
}
}

Output of code:

12
(CS361) Cryptography & Network Security 21DCS099

CASE 2 :

Special characters in ciphertext


Cipher text = "@#HGS*&”

import java.util.Arrays;

public class PlayfairCipher {

static final int SIZE = 30;

public static void toLowerCase(char plain[], int ps) {


for (int i = 0; i < ps; i++) {
if (plain[i] > 64 && plain[i] < 91)
plain[i] += 32;
}
}

public static int removeSpaces(char[] plain, int ps) {


int count = 0;
for (int i = 0; i < ps; i++)
if (plain[i] != ' ')
plain[count++] = plain[i];
plain[count] = '\0';
return count;
}

public static void generateKeyTable(char[] key, int ks, char[][] keyT) {


int i, j, k, flag = 0;
int[] dicty = new int[26];

Arrays.fill(dicty, 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;

13
(CS361) Cryptography & Network Security 21DCS099

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

public static void search(char[][] keyT, 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;
}
}
}
}

public static int mod5(int a) {


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

public static void decrypt(char[] str, char[][] keyT, int ps) {


int[] a = new int[4];
for (int i = 0; i < ps; i += 2) {

14
(CS361) Cryptography & Network Security 21DCS099

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]];
}
}
System.out.println("Deciphered text: " + new String(str));
}

public static void decryptByPlayfairCipher(char[] str, char[] key) {


int ps, ks;
char[][] keyT = new char[5][5];

ks = key.length;
ks = removeSpaces(key, ks);
toLowerCase(key, ks);

ps = str.length;
toLowerCase(str, ps);
ps = removeSpaces(str, ps);

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


if (!Character.isAlphabetic(str[i])) {
System.out.println("Invalid input. The text should contain only alphabetic
characters.");
return;
}
}

generateKeyTable(key, ks, keyT);

decrypt(str, keyT, ps);


}

public static void main(String[] args) {


char[] str = new char[SIZE];
char[] key = new char[SIZE];

System.arraycopy("pearlharbour ".toCharArray(), 0, key, 0, "pearlharbour


".length());
System.out.println("Key Text: " + new String(key));

System.arraycopy("@#HGS*&".toCharArray(), 0, str, 0, "@#HGS*&".length());


System.out.println("Cipher text: " + new String(str));

15
(CS361) Cryptography & Network Security 21DCS099

decryptByPlayfairCipher(str, key);
}
}

Output of code:

CASE 3 :

Numbers in ciphertext
Cipher text = " YMSRM SM UALAW "

import java.util.Arrays;

public class PlayfairCipher {

static final int SIZE = 30;

public static void toLowerCase(char plain[], int ps) {


for (int i = 0; i < ps; i++) {
if (plain[i] > 64 && plain[i] < 91)
plain[i] += 32;
}
}

public static int removeSpaces(char[] plain, int ps) {


int count = 0;
for (int i = 0; i < ps; i++)
if (plain[i] != ' ')
plain[count++] = plain[i];
plain[count] = '\0';
return count;
}

public static void generateKeyTable(char[] key, int ks, char[][] keyT) {


int i, j, k, flag = 0;
int[] dicty = new int[26];

Arrays.fill(dicty, 0);

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


if (key[i] != 'j')
dicty[key[i] - 97] = 2;
}

16
(CS361) Cryptography & Network Security 21DCS099

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

public static void search(char[][] keyT, 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;
}
}
}
}

public static int mod5(int a) {


if (a < 0)

17
(CS361) Cryptography & Network Security 21DCS099

a += 5;
return (a % 5);
}

public static void decrypt(char[] str, char[][] keyT, int ps) {


int[] a = new int[4];
for (int 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]];
}
}
System.out.println("Deciphered text: " + new String(str));
}

public static void decryptByPlayfairCipher(char[] str, char[] key) {


int ps, ks;
char[][] keyT = new char[5][5];

ks = key.length;
ks = removeSpaces(key, ks);
toLowerCase(key, ks);

ps = str.length;
toLowerCase(str, ps);
ps = removeSpaces(str, ps);

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


if (!Character.isAlphabetic(str[i])) {
System.out.println("Invalid input. The text should contain only alphabetic
characters.");
return;
}
}

generateKeyTable(key, ks, keyT);

decrypt(str, keyT, ps);


}

public static void main(String[] args) {


char[] str = new char[SIZE];
char[] key = new char[SIZE];

18
(CS361) Cryptography & Network Security 21DCS099

System.arraycopy("pearlharbour ".toCharArray(), 0, key, 0, "pearlharbour ".length());


System.out.println("Key Text: " + new String(key));

System.arraycopy(" YMSRM SM UALAW ".toCharArray(), 0, str, 0, " YMSRM SM


UALAW ".length());
System.out.println("Cipher text: " + new String(str));

decryptByPlayfairCipher(str, key);
}
}
Output of code:

19

You might also like