Professional Documents
Culture Documents
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
7
(CS361) Cryptography & Network Security 21DCS099
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
Background Equations:
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.
CASE 1 :
Numbers in ciphertext
Cipher text = “123BWPNRSMUALAW”
import java.util.Arrays;
9
(CS361) Cryptography & Network Security 21DCS099
Arrays.fill(dicty, 0);
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;
}
}
}
}
10
(CS361) Cryptography & Network Security 21DCS099
int i, j;
if (a == 'j')
a = 'i';
else if (b == 'j')
b = 'i';
11
(CS361) Cryptography & Network Security 21DCS099
ks = key.length;
ks = removeSpaces(key, ks);
toLowerCase(key, ks);
ps = str.length;
toLowerCase(str, ps);
ps = removeSpaces(str, ps);
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 :
import java.util.Arrays;
Arrays.fill(dicty, 0);
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;
}
}
}
}
if (a == 'j')
a = 'i';
else if (b == 'j')
b = 'i';
14
(CS361) Cryptography & Network Security 21DCS099
ks = key.length;
ks = removeSpaces(key, ks);
toLowerCase(key, ks);
ps = str.length;
toLowerCase(str, ps);
ps = removeSpaces(str, ps);
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;
Arrays.fill(dicty, 0);
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;
}
}
}
}
if (a == 'j')
a = 'i';
else if (b == 'j')
b = 'i';
17
(CS361) Cryptography & Network Security 21DCS099
a += 5;
return (a % 5);
}
ks = key.length;
ks = removeSpaces(key, ks);
toLowerCase(key, ks);
ps = str.length;
toLowerCase(str, ps);
ps = removeSpaces(str, ps);
18
(CS361) Cryptography & Network Security 21DCS099
decryptByPlayfairCipher(str, key);
}
}
Output of code:
19