3. Write a Java program to perform encryption and decryption using the following
algorithms:
a) Ceaser Cipher
b) Substitution Cipher
c) Hill Cipher
--------------------------------------------------------------------
a) Ceaser Cipher:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class CaesarCipher {
static Scanner sc = new Scanner(System.in);
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
System.out.print("Enter any String: ");
String str = br.readLine();
System.out.print("Enter the Key: ");
int key = sc.nextInt();
String encrypted = encrypt(str, key);
System.out.println("Encrypted String is: " + encrypted);
String decrypted = decrypt(encrypted, key);
System.out.println("Decrypted String is: " + decrypted);
}
public static String encrypt(String str, int key) {
StringBuilder encrypted = new StringBuilder();
key = key % 26;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isUpperCase(c)) {
c = (char) (c + key);
if (c > 'Z')
c = (char) (c - 26);
}
else if (Character.isLowerCase(c)) {
c = (char) (c + key);
if (c > 'z')
c = (char) (c - 26);
}
encrypted.append(c);
}
return encrypted.toString();
}
public static String decrypt(String str, int key) {
StringBuilder decrypted = new StringBuilder();
key = key % 26;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isUpperCase(c)) {
c = (char) (c - key);
if (c < 'A')
c = (char) (c + 26);
}
else if (Character.isLowerCase(c)) {
c = (char) (c - key);
if (c < 'a')
c = (char) (c + 26);
}
decrypted.append(c);
}
return decrypted.toString();
}
}
Sample Input & Output:
Input:
Enter any String: HelloWorld
Enter the Key: 3
Output:
Encrypted String is: KhoorZruog
Decrypted String is: HelloWorld
------------------------------------------------------------------
b) Substitution Cipher:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SubstitutionCipher {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
String a = "abcdefghijklmnopqrstuvwxyz";
String b = "zyxwvutsrqponmlkjihgfedcba";
System.out.print("Enter any string: ");
String str = br.readLine();
StringBuilder encrypted = new StringBuilder();
char c;
for (int i = 0; i < str.length(); i++) {
c = str.charAt(i);
// For lowercase letters
if (Character.isLowerCase(c)) {
int j = a.indexOf(c);
if (j != -1) {
encrypted.append(b.charAt(j));
}
}
// For uppercase letters
else if (Character.isUpperCase(c)) {
int j = a.indexOf(Character.toLowerCase(c));
if (j != -1) {
encrypted.append(Character.toUpperCase(b.charAt(j)));
}
}
// For spaces and special characters
else {
encrypted.append(c);
}
}
System.out.println("The encrypted data is: " + encrypted.toString());
}
}
Sample Input & Output:
Input:
Enter any string: Hello World
Output:
The encrypted data is: Svool Dliow
------------------------------------------------------------------------------------
c) Hill Cipher:
import java.util.Scanner;
public class HillCipherSimple {
static int[][] key = { {3, 3}, {2, 5} };
static int[][] inverseKey = { {15, 17}, {20, 9} };
// Encryption
static String encrypt(String text) {
text = text.toUpperCase().replaceAll("[^A-Z]", "");
if (text.length() % 2 != 0) {
text += "X";
}
StringBuilder cipher = new StringBuilder();
for (int i = 0; i < text.length(); i += 2) {
int a = text.charAt(i) - 'A';
int b = text.charAt(i + 1) - 'A';
int c1 = (key[0][0] * a + key[0][1] * b) % 26;
int c2 = (key[1][0] * a + key[1][1] * b) % 26;
cipher.append((char) (c1 + 'A'));
cipher.append((char) (c2 + 'A'));
}
return cipher.toString();
}
// Decryption
static String decrypt(String cipher) {
cipher = cipher.toUpperCase();
StringBuilder plain = new StringBuilder();
for (int i = 0; i < cipher.length(); i += 2) {
int a = cipher.charAt(i) - 'A';
int b = cipher.charAt(i + 1) - 'A';
int p1 = (inverseKey[0][0] * a + inverseKey[0][1] * b) % 26;
int p2 = (inverseKey[1][0] * a + inverseKey[1][1] * b) % 26;
plain.append((char) (p1 + 'A'));
plain.append((char) (p2 + 'A'));
}
return plain.toString();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Plain Text: ");
String text = sc.nextLine();
String cipher = encrypt(text);
System.out.println("Encrypted Text: " + cipher);
String decrypted = decrypt(cipher);
System.out.println("Decrypted Text: " + decrypted);
sc.close();
}
}
Sample Input & Output:
Input:
HELLO
Output:
Enter Plain Text: HELLO
Encrypted Text: HIOZHN
Decrypted Text: HELLOX