Is LAB 1st Program
Is LAB 1st Program
ALGORITHM
1. In Ceaser Cipher each letter in the plaintext is replaced by a letter some
fixed number of positions down the alphabet.
2. For example, with a left shift of 3, D would be replaced by A, E would
become B, and so on.
3. The encryption can also be represented using modular arithmetic by first
transforming the letters into numbers, according to the scheme, A = 0, B =
1,Z = 25.
4. Encryption of a letter x by a shift n can be described mathematically as,
En(x) = (x + n) mod26
5. Decryption is performed similarly,
Dn (x)=(x - n) mod26
PROGRAM:
class caesarCipher {
public static String encode(String enc, int offset) {
offset = offset % 26 + 26;
StringBuilder encoded = new StringBuilder();
for (char i : enc.toCharArray()) {
if (Character.isLetter(i)) {
if (Character.isUpperCase(i)) {
encoded.append((char) ('A' + (i - 'A' + offset) % 26));
} else {
encoded.append((char) ('a' + (i - 'a' + offset) % 26));
}
} else {
encoded.append(i);
}
}
return encoded.toString();
}
public static String decode(String enc, int offset) {
return encode(enc, 26 - offset);
}
public static void main(String[] args) throws java.lang.Exception {
String msg = "LBRCE CSE";
System.out.println("Simulating Caesar Cipher\n------------------------");
System.out.println("Input : " + msg);
System.out.printf("Encrypted Message : ");
System.out.println(caesarCipher.encode(msg, 3));
System.out.printf("Decrypted Message : ");
System.out.println(caesarCipher.decode(caesarCipher.encode(msg, 3), 3));
}
}
Output:
Simulating Caesar Cipher
------------------------
Input : LBRCE CSE
Encrypted Message : OEUFH FVH
Decrypted Message : LBRCE CSE
RESULT:
Thus the program for ceaser cipher encryption and decryption algorithm has
been implemented and the output verified successfully.
b) Playfair Cipher
AIM:
ALGORITHM:
1. To encrypt a message, one would break the message into digrams (groups of
2 letters)
2. For example, "HelloWorld" becomes "HE LL OW OR LD".
3. These digrams will be substituted using the key table.
5. The two letters of the digram are considered opposite corners of a rectangle
in the key table. To perform the substitution, apply the following 4 rules, in
order, to each pair of letters in the plaintext:
PROGRAM:
playfairCipher.java
import java.awt.Point;
class playfairCipher {
private static char[][] charTable;
}
private static void createTbl(String key, boolean chgJtoI) {
chgJtoI);
int len = s.length();
k++;
}
}
}
private static String codec(StringBuilder txt, int dir) {
int len = txt.length();
for (int i = 0; i < len; i += 2) {
char a = txt.charAt(i);
col2 = tmp;
}
txt.setCharAt(i, charTable[row1][col1]);
txt.setCharAt(i + 1, charTable[row2][col2]);
}
return txt.toString();
}
private static String encode(String s) {
StringBuilder sb = new StringBuilder(s);
for (int i = 0; i < sb.length(); i += 2) {
if (i == sb.length() - 1) {
sb.append(sb.length() % 2 == 1 ? 'X' : "");
} else if (sb.charAt(i) == sb.charAt(i + 1)) {
sb.insert(i + 1, 'X');
}
}
return codec(sb, 1);
}
createTbl(key, chgJtoI);
String enc = encode(prepareText(txt, chgJtoI));
System.out.println("Simulating Playfair Cipher\n----------------------");
System.out.println("Input Message : " + txt);
}
}
OUTPUT: