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 conditionYour system should encryptdecrypt 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 gcdint a int m while m 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 encryptString text, int a int b String result new String; Create an empty string to hold the encrypted text for int i ; i text.length; i Loop through each character in the text char ch text.charAti; Get the current character if CharacterisLetterch Check if the character is a letter char base; Variable to determine the base character A or a if CharacterisUpperCasech 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 chara x b 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 decryptString cipher, int a int b String result new String; Create an empty string to hold the decrypted text int ainv ; Variable to hold the multiplicative inverse of a for int i ; i ; i Find the multiplicative inverse of a if a i If the result is we found the inverse ainv i; Set the inverse value break; Exit the loop for int i ; i cipher.length; i Loop through each character in the cipher text char ch cipher.charAti; Get the current character if CharacterisLetterch Check if the character is a letter char base; Variable to determine the base character if CharacterisUpperCasech 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 charainv y b 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 public static int getValidAScanner scanner int a; Variable to store the value of a while true Infinite loop to get a valid input System.out.printEnter a must be coprime with : ; ask for a a getIntInputscanner; Get the valid integer input if gcda If a is coprime with break; else System.out.printlnError: a must be coprime with ; return a; Return the valid value of a Function to check if the input string contains only letters public static boolean isLitterString text return text.matchesazAZ; Return true if the input consists of only letters Function to validate numerical inputs public static int getIntInputScanner scanner while scanner.hasNextInt While the input is not an integer System.out.printlnError: 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 getnotemptyScanner scanner String input; Variable to store the input do System.out.printEnter the text: ; ask for text input input scanner.nextLinetrim; Get the input and remove e
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
