Caesar Cipher
Caesar Cipher
import java.util.*;
do {
System.out.println("\nMenu:");
System.out.println("1. Encrypt");
System.out.println("2. Decrypt");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
sc.nextLine(); // Consume newline
switch (choice) {
case 1:
System.out.print("Enter plaintext: ");
String plaintext = sc.nextLine();
System.out.print("Enter shift value: ");
int encryptShift = sc.nextInt();
sc.nextLine();
String encrypted = encrypt(plaintext, encryptShift);
System.out.println("Encrypted: " + encrypted);
break;
case 2:
System.out.print("Enter ciphertext: ");
String ciphertext = sc.nextLine();
System.out.print("Enter shift value: ");
int decryptShift = sc.nextInt();
sc.nextLine();
String decrypted = decrypt(ciphertext, decryptShift);
System.out.println("Decrypted: " + decrypted);
break;
case 3:
System.out.println("Exiting the program.");
break;
default:
System.out.println("Invalid choice. Please try
again.");
}
} while (choice != 3);
sc.close();
}
}
//Playfair cipher
import java.util.*;
do {
System.out.println("\nMenu:");
System.out.println("1. Encrypt");
System.out.println("2. Decrypt");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
System.out.print("Enter the plaintext: ");
String plaintext = scanner.nextLine();
String encrypted = encrypt(plaintext, key);
System.out.println("Encrypted: " + encrypted);
break;
case 2:
System.out.print("Enter the ciphertext: ");
String ciphertext = scanner.nextLine();
String decrypted = decrypt(ciphertext, key);
System.out.println("Decrypted: " + decrypted);
break;
case 3:
System.out.println("Exiting the program.");
break;
default:
System.out.println("Invalid choice. Please try
again.");
}
} while (choice != 3);
scanner.close();
}
}
//Hillcipher
import java.util.*;
while (len % n != 0) {
plainText += 'X';
len++;
}
int det = 0;
for (int i = 0; i < n; i++) {
det += (i % 2 == 0 ? 1 : -1) * matrix[0][i] *
determinant(minor(matrix, 0, i, n), n - 1);
}
return det;
}
public static int[][] minor(int[][] matrix, int row, int col, int n) {
int[][] minor = new int[n - 1][n - 1];
for (int i = 0, mi = 0; i < n; i++) {
if (i == row) continue;
for (int j = 0, mj = 0; j < n; j++) {
if (j == col) continue;
minor[mi][mj++] = matrix[i][j];
}
mi++;
}
return minor;
}
scanner.nextLine();
do {
System.out.println("\nMenu:");
System.out.println("1. Encrypt");
System.out.println("2. Decrypt");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.print("Enter the plaintext: ");
String plainText = scanner.nextLine();
System.out.println("Encrypted: " + encrypt(keyMatrix,
plainText, n));
break;
case 2:
System.out.print("Enter the ciphertext: ");
String cipherText = scanner.nextLine();
System.out.println("Decrypted: " + decrypt(keyMatrix,
cipherText, n));
break;
case 3:
System.out.println("Exiting the program.");
break;
default:
System.out.println("Invalid choice. Please try
again.");
}
} while (choice != 3);
scanner.close();
}
}
Enter the size of the key matrix:
3
6 24 1
13 16 10
20 17 15
Menu:
1. Encrypt
2. Decrypt
3. Exit
Encrypted: VKMNOGDAI