Exercise 1- Caesar Cipher: public class CaesarCipher { // Encryption method public static String encrypt(String plaintext, int shift) { String encryptedText = ""; for (char ch : plaintext.toCharArray()) { if (Character.isLetter(ch)) { char base = Character.isUpperCase(ch) ? 'A' : 'a'; encryptedText=encryptedText +((char) ((ch - base + shift) % 26 + base)); } else { encryptedText+=ch; } } return encryptedText; } // Decryption method public static String decrypt(String ciphertext, int shift) { // Decryption is the same as encryption with a negative shift return encrypt(ciphertext, -shift); } public static void main(String[] args) { // Example Usage String plaintext = "Hello, World!"; int shift = 3; // Encryption String encryptedText = encrypt(plaintext, shift); System.out.println("Encrypted: " + encryptedText); // Decryption String decryptedText = decrypt(encryptedText, shift); System.out.println("Decrypted: " + decryptedText); } } Exercise 2- Affine Cipher: import java.util.Random; public class AffineCipherExample { public static void main(String[] args) { // Example Usage String plaintext = "Hello, World!"; // Replace with your desired plaintext // Generate random values for a and b (ensuring a is coprime with 26) int a = generateCoprimeWith26(); int b = new Random().nextInt(26); // b can be any random integer // Encryption String ciphertext = encrypt(plaintext, a, b); System.out.println("Plaintext: " + plaintext); System.out.println("Encrypted: " + ciphertext); // Decryption String decryptedText = decrypt(ciphertext, a, b); System.out.println("Decrypted: " + decryptedText); } // Method to check if two numbers are coprime private static boolean areCoprime(int a, int b) { while (b != 0) { int temp = b; b = a % b; a = temp; } return a == 1; } // Method to generate a random integer coprime with 26 private static int generateCoprimeWith26() { Random random = new Random(); int candidate; do { candidate = random.nextInt(25) + 1; // Generate a random integer between 1 and 25 } while (!areCoprime(candidate, 26)); return candidate; } // Affine Cipher Encryption private static String encrypt(String plaintext, int a, int b) { String encryptedText = ""; for (char ch : plaintext.toCharArray()) { if (Character.isLetter(ch)) { char base = Character.isUpperCase(ch) ? 'A' : 'a'; encryptedText+=((char) ((a * (ch - base) + b) % 26 + base)); } else { encryptedText+=ch; } } return encryptedText; } // Affine Cipher Decryption private static String decrypt(String ciphertext, int a, int b) { // Calculate modular multiplicative inverse of a modulo 26 int aInverse = 0; for (int i = 1; i < 26; i++) { if ((a * i) % 26 == 1) { System.out.println("a= "+ a); System.out.println("aInverse= "+ i); aInverse = i; break; } } String decryptedText = ""; for (char ch : ciphertext.toCharArray()) { if (Character.isLetter(ch)) { char base = Character.isUpperCase(ch) ? 'A' : 'a'; decryptedText+=((char) ((aInverse * (ch - base - b + 26) % 26) + base)); } else { decryptedText+=ch; } } return decryptedText; } }