0% found this document useful (0 votes)
11 views13 pages

KENIL INS Lab Manual 1-2

The document outlines practical implementations of various encryption techniques, including Caesar cipher, Monoalphabetic cipher, and Playfair cipher. Each section provides theoretical background, code examples, and explanations of the encryption and decryption processes. The document emphasizes the simplicity and vulnerabilities of these ciphers, particularly in relation to brute force attacks and frequency analysis.

Uploaded by

mangamanga1101
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)
11 views13 pages

KENIL INS Lab Manual 1-2

The document outlines practical implementations of various encryption techniques, including Caesar cipher, Monoalphabetic cipher, and Playfair cipher. Each section provides theoretical background, code examples, and explanations of the encryption and decryption processes. The document emphasizes the simplicity and vulnerabilities of these ciphers, particularly in relation to brute force attacks and frequency analysis.

Uploaded by

mangamanga1101
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/ 13

Faculty of Engineering & Technology

Information and Network security (203105311)


B. Tech CSE 4th Year 7th Semester

PRACTICAL: 01

AIM: Implement Caesar cipher encryption-decryption.

Theory: -
 Caesar cipher is a substitution cipher where each letter in the
plaintext is shifted a certain number of places down or up the
alphabet.
 It's a simple and historical method of encryption, but it's not
very secure due to its vulnerability to brute force attacks.
 The key idea is shifting letters by a fixed amount to encode
and decode messages.
 For Caesar cipher encryption, each letter in the plaintext is
shifted a fixed number of places down or up the alphabet.
 Let's say we have a shift of 3. So, 'A' becomes 'D', 'B'
becomes 'E', and so on.
 To decrypt, you simply shift back by the same number of
places.
 It's a straightforward method but not very secure.
 The key is crucial for both encryption and decryption.

A) Caesar Cipher Encryption

Code: -
//encryption
import java.util.Scanner;

class lab1 {
public static StringBuffer encrypt(String text, int s) {
StringBuffer result = new StringBuffer();

for (int i = 0; i < text.length(); i++) {


if (Character.isUpperCase(text.charAt(i))) {
char ch = (char) (((int) text.charAt(i) +
s - 65) % 26 + 65);
result.append(ch);
} else {
char ch = (char) (((int) text.charAt(i) +
Enrollment No.: 210303105169
CHAUHAN PURVANK
Page| 1
Div: 25(CSE)
Faculty of Engineering & Technology
Information and Network security (203105311)
B. Tech CSE 4th Year 7th Semester

s - 97) % 26 + 97);
result.append(ch);
}
}
return result;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.print("Enter Plain Text : ");


String plainText = sc.nextLine();
System.out.println();

System.out.print("Enter Key : ");


int Key = sc.nextInt();
System.out.println();

System.out.print("Cipher Text : " + encrypt(plainText,


Key));

}
}
Output:

B) Caesar Cipher Decryption

Enrollment No.: 210303105169


CHAUHAN PURVANK
Page| 2
Div: 25(CSE)
Faculty of Engineering & Technology
Information and Network security (203105311)
B. Tech CSE 4th Year 7th Semester

Code: -
//decrpytion
import java.util.Scanner;

class lab2 {
public static StringBuffer encrypt(String text, int s) {
StringBuffer result = new StringBuffer();

for (int i = 0; i < text.length(); i++) {


if (Character.isUpperCase(text.charAt(i))) {
char ch = (char) (((int) text.charAt(i) -
s - 65) % 26 + 65);
result.append(ch);
} else {
char ch = (char) (((int) text.charAt(i) -
s - 97) % 26 + 97);
result.append(ch);
}
}
return result;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.print("Enter Cipher Text : ");


String plainText = sc.nextLine();
System.out.println();

System.out.print("Enter Key : ");


int Key = sc.nextInt();
System.out.println();

System.out.print("Plain Text : " + encrypt(plainText,


Key));
}
}

Enrollment No.: 210303105169


CHAUHAN PURVANK
Page| 3
Div: 25(CSE)
Faculty of Engineering & Technology
Information and Network security (203105311)
B. Tech CSE 4th Year 7th Semester

Output:

Enrollment No.: 210303105169


CHAUHAN PURVANK
Page| 4
Div: 25(CSE)
Faculty of Engineering & Technology
Information and Network security (203105311)
B. Tech CSE 4th Year 7th Semester

PRACTICAL: 02

AIM: Implement Monoalphabetic cipher encryption-decryption.

Theory: -
 A monoalphabetic cipher is a substitution cipher where each
letter in the plaintext is replaced by a corresponding letter
from a fixed substitution alphabet.
 This method ensures that each letter consistently maps to the
same substitute letter throughout the message.
 Although simple to implement, monoalphabetic ciphers are
highly vulnerable to frequency analysis, as the patterns in
letter frequency remain unchanged from the plaintext to the
ciphertext.
 Historical examples, like the Caesar cipher, demonstrate the
fundamental concept of monoalphabetic encryption and its
straightforward yet easily breakable nature.

Code: -
public class lab4 {
public static char givenChar[] = {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
};

public static char encryChar[] = {


'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O',
'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K',
'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M'
};

public static String encryptionString(String s) {


String emptyString = "";

for (int i = 0; i < s.length(); i++) {


for (int j = 0; j < 26; j++) {
Enrollment No.: 210303105169
CHAUHAN PURVANK
Page| 5
Div: 25(CSE)
Faculty of Engineering & Technology
Information and Network security (203105311)
B. Tech CSE 4th Year 7th Semester

if (s.charAt(i) == givenChar[j]) {
emptyString += encryChar[j];
break;
}

if (s.charAt(i) < 'a' || s.charAt(i) > 'z') {


emptyString += s.charAt(i);
break;
}
}
}
return emptyString;
}

public static String decryptionString(String s) {


String empty = "";

for (int i = 0; i < s.length(); i++) {


for (int j = 0; j < 26; j++) {

if (s.charAt(i) == encryChar[j]) {
empty += givenChar[j];
break;
}

if(s.charAt(i)<'A'||s.charAt(i)>'Z'){
empty +=s.charAt(i);
break;
}
}
}
return empty;
}

public static void main(String[] args) {


String plainText = "Hello Kenil";

Enrollment No.: 210303105169


CHAUHAN PURVANK
Page| 6
Div: 25(CSE)
Faculty of Engineering & Technology
Information and Network security (203105311)
B. Tech CSE 4th Year 7th Semester

System.out.println("Plain Text: " + plainText);

String emptyString =
encryptionString(plainText.toLowerCase());

System.out.println("Encrypted String: " +


encryptionString(plainText));
System.out.println("Decrypted String:
"+decryptionString(emptyString));
}
}
Output:

Enrollment No.: 210303105169


CHAUHAN PURVANK
Page| 7
Div: 25(CSE)
Faculty of Engineering & Technology
Information and Network security (203105311)
B. Tech CSE 4th Year 7th Semester

PRACTICAL: 03

AIM: Implement Playfair cipher encryption-decryption.

Theory: -
 Alphabet Pairing: The Playfair cipher encrypts pairs of
letters (digraphs), making it harder for frequency analysis to
crack the code.
 5x5 Grid: It uses a 5x5 grid filled with a keyword (without
repeating letters) followed by the remaining letters of the
alphabet. 'I' and 'J' are often combined to fit into the 25 cells.
 Rule-Based Encryption: Encryption rules depend on the
relative positions of the letter pairs in the grid: same row,
same column, or rectangle formation.
 Handling Duplicates and Odd Letters: If a pair consists of
the same letter, an 'X' is inserted between them. If the
plaintext has an odd number of letters, an 'X' is added at the
end.
 Decryption: The decryption process reverses the encryption
rules, restoring the original message by mapping the
encrypted pairs back through the grid.

Code: -
import java.awt.Point;
import java.util.Scanner;

public class PlayfairCipher {


private int length = 0;
private String[][] table;

public static void main(String args[]) {


PlayfairCipher pf = new PlayfairCipher();
}
private PlayfairCipher() {
System.out.print("Enter the key for playfair cipher: ");
Scanner sc = new Scanner(System.in);
String key = parseString(sc);
Enrollment No.: 210303105169
CHAUHAN PURVANK
Page| 8
Div: 25(CSE)
Faculty of Engineering & Technology
Information and Network security (203105311)
B. Tech CSE 4th Year 7th Semester

while (key.equals(""))
key = parseString(sc);
table = this.cipherTable(key);
System.out.print("Enter the plaintext to be encipher: ");
String input = parseString(sc);
while (input.equals(""))
input = parseString(sc);
String output = cipher(input);
String decodedOutput = decode(output);
this.keyTable(table);
this.printResults(output, decodedOutput);
}
private String parseString(Scanner sc) {
String parse = sc.nextLine();
parse = parse.toUpperCase();
parse = parse.replaceAll("[^A-Z]", "");
parse = parse.replace("J", "I");
return parse;
}
private String[][] cipherTable(String key) {
String[][] playfairTable = new String[5][5];
String keyString = key +
"ABCDEFGHIKLMNOPQRSTUVWXYZ";
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
playfairTable[i][j] = "";
for (int k = 0; k < keyString.length(); k++) {
boolean repeat = false;
boolean used = false;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (playfairTable[i][j].equals("" +
keyString.charAt(k))) {
repeat = true;
} else if (playfairTable[i][j].equals("") && !repeat
&& !used) {
playfairTable[i][j] = "" + keyString.charAt(k);

Enrollment No.: 210303105169


CHAUHAN PURVANK
Page| 9
Div: 25(CSE)
Faculty of Engineering & Technology
Information and Network security (203105311)
B. Tech CSE 4th Year 7th Semester

used = true;
}
}
}
}
return playfairTable;
}

private String cipher(String in) {


length = (int) in.length() / 2 + in.length() % 2;

for (int i = 0; i < (length - 1); i++) {


if (in.charAt(2 * i) == in.charAt(2 * i + 1)) {
in = new StringBuffer(in).insert(2 * i + 1,
'X').toString();
length = (int) in.length() / 2 + in.length() % 2;
}
}
String[] digraph = new String[length];
for (int j = 0; j < length; j++) {
if (j == (length - 1) && in.length() / 2 == (length - 1))
in = in + "X";
digraph[j] = in.charAt(2 * j) + "" + in.charAt(2 * j + 1);
}
String out = "";
String[] encDigraphs = new String[length];
encDigraphs = encodeDigraph(digraph);
for (int k = 0; k < length; k++)
out = out + encDigraphs[k];
return out;
}
private String[] encodeDigraph(String di[]) {
String[] encipher = new String[length];
for (int i = 0; i < length; i++) {
char a = di[i].charAt(0);
char b = di[i].charAt(1);
int r1 = (int) getPoint(a).getX();
Enrollment No.: 210303105169
CHAUHAN PURVANK
P a g e | 10
Div: 25(CSE)
Faculty of Engineering & Technology
Information and Network security (203105311)
B. Tech CSE 4th Year 7th Semester

int r2 = (int) getPoint(b).getX();


int c1 = (int) getPoint(a).getY();
int c2 = (int) getPoint(b).getY();
if (r1 == r2) {
c1 = (c1 + 1) % 5;
c2 = (c2 + 1) % 5;
}

else if (c1 == c2) {


r1 = (r1 + 1) % 5;
r2 = (r2 + 1) % 5;
}
else {
int temp = c1;
c1 = c2;
c2 = temp;
}
encipher[i] = table[r1][c1] + "" + table[r2][c2];
}
return encipher;
}
private String decode(String out) {
String decoded = "";
for (int i = 0; i < out.length() / 2; i++) {
char a = out.charAt(2 * i);
char b = out.charAt(2 * i + 1);
int r1 = (int) getPoint(a).getX();
int r2 = (int) getPoint(b).getX();
int c1 = (int) getPoint(a).getY();
int c2 = (int) getPoint(b).getY();
if (r1 == r2) {
c1 = (c1 + 4) % 5;
c2 = (c2 + 4) % 5;
} else if (c1 == c2) {
r1 = (r1 + 4) % 5;
r2 = (r2 + 4) % 5;
} else {

Enrollment No.: 210303105169


CHAUHAN PURVANK
P a g e | 11
Div: 25(CSE)
Faculty of Engineering & Technology
Information and Network security (203105311)
B. Tech CSE 4th Year 7th Semester

int temp = c1;


c1 = c2;
c2 = temp;
}
decoded = decoded + table[r1][c1] + table[r2][c2];
}
return decoded;
}
private Point getPoint(char c) {
Point pt = new Point(0, 0);
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
if (c == table[i][j].charAt(0))
pt = new Point(i, j);
return pt;
}
private void keyTable(String[][] printTable) {
System.out.println("Playfair Cipher Key Matrix: ");
System.out.println();
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(printTable[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
private void printResults(String encipher, String dec) {
System.out.print("Encrypted Message: ");
System.out.println(encipher);
System.out.println();
System.out.print("Decrypted Message: ");
System.out.println(dec);
}
}

Enrollment No.: 210303105169


CHAUHAN PURVANK
P a g e | 12
Div: 25(CSE)
Faculty of Engineering & Technology
Information and Network security (203105311)
B. Tech CSE 4th Year 7th Semester

Output:

Enrollment No.: 210303105169


CHAUHAN PURVANK
P a g e | 13
Div: 25(CSE)

You might also like