KENIL INS Lab Manual 1-2
KENIL INS Lab Manual 1-2
PRACTICAL: 01
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.
Code: -
//encryption
import java.util.Scanner;
class lab1 {
public static StringBuffer encrypt(String text, int s) {
StringBuffer result = new StringBuffer();
s - 97) % 26 + 97);
result.append(ch);
}
}
return result;
}
}
}
Output:
Code: -
//decrpytion
import java.util.Scanner;
class lab2 {
public static StringBuffer encrypt(String text, int s) {
StringBuffer result = new StringBuffer();
Output:
PRACTICAL: 02
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'
};
if (s.charAt(i) == givenChar[j]) {
emptyString += encryChar[j];
break;
}
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;
}
String emptyString =
encryptionString(plainText.toLowerCase());
PRACTICAL: 03
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;
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);
used = true;
}
}
}
}
return playfairTable;
}
Output: