Question: Can you make this code meet this condition ( Your system should encrypt / decrypt all text files in a given folder or single text

Can you make this code meet this condition(Your system should encrypt/decrypt all text files in a given folder or single text file) to this code import java.util.Scanner; public class AffineCipher {// Function to calculate the greatest common divisor (gcd) of two numbers public static int gcd(int a, int m){ while (m !=0){ int temp = m; m = a % m; a = temp; } return a; // Return the gcd }// Function to encrypt the text using the Affine Cipher algorithm public static String encrypt(String text, int a, int b){ String result = new String(); // Create an empty string to hold the encrypted text for (int i =0; i < text.length(); i++){// Loop through each character in the text char ch = text.charAt(i); // Get the current character if (Character.isLetter(ch)){// Check if the character is a letter char base; // Variable to determine the base character (A or a) if (Character.isUpperCase(ch)){// If the character is uppercase base ='A'; // Set base for uppercase } else { base ='a'; // Set base for lowercase } int x = ch - base; // Calculate the position of the character relative to the base char encryptedChar =(char)((a * x + b)%26+ base); // Apply the encryption formula result += encryptedChar; // Append the encrypted character to the result } else { result += ch; // If it's not a letter, append the character as is }} return result; // Return the encrypted text }// Function to decrypt the encrypted text public static String decrypt(String cipher, int a, int b){ String result = new String(); // Create an empty string to hold the decrypted text int a_inv =0; // Variable to hold the multiplicative inverse of a for (int i =0; i <26; i++){// Find the multiplicative inverse of a if ((a * i)%26==1){// If the result is 1, we found the inverse a_inv = i; // Set the inverse value break; // Exit the loop }} for (int i =0; i < cipher.length(); i++){// Loop through each character in the cipher text char ch = cipher.charAt(i); // Get the current character if (Character.isLetter(ch)){// Check if the character is a letter char base; // Variable to determine the base character if (Character.isUpperCase(ch)){// If the character is uppercase base ='A'; // Set base for uppercase } else { base ='a'; // Set base for lowercase } int y = ch - base; // Calculate the position of the character relative to the base char decryptedChar =(char)((a_inv *(y - b +26))%26+ base); // Apply the decryption formula result += decryptedChar; // Append the decrypted character to the result } else { result += ch; // If it's not a letter, append the character as is }} return result; // Return the decrypted text }// Function to ensure the input number is coprime with 26 public static int getValidA(Scanner scanner){ int a; // Variable to store the value of a while (true){// Infinite loop to get a valid input System.out.print("Enter a (must be coprime with 26): "); // ask for a a = getIntInput(scanner); // Get the valid integer input if (gcd(a,26)==1){// If a is coprime with 26 break; } else { System.out.println("Error: a must be coprime with 26."); }} return a; // Return the valid value of a }// Function to check if the input string contains only letters public static boolean isLitter(String text){ return text.matches("[a-zA-Z]+"); // Return true if the input consists of only letters }// Function to validate numerical inputs public static int getIntInput(Scanner scanner){ while (!scanner.hasNextInt()){// While the input is not an integer System.out.println("Error: Please enter a valid integer."); scanner.next(); // Ignore the invalid input } return scanner.nextInt(); // Return the valid integer input }// Function to ensure the input text is not empty public static String getnotempty(Scanner scanner){ String input; // Variable to store the input do { System.out.print("Enter the text: "); // ask for text input input = scanner.nextLine().trim(); // Get the input and remove e

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!