Answered step by step
Verified Expert Solution
Question
1 Approved Answer
PLEASE FIX THE JAVA METHOD DECRYPT & IN THE MAIN STRING IN ORDER FOR THE RANDOM GENERATED PASSWORD TO BE DECRYPTED: BELOW IS FULL CODE.
PLEASE FIX THE JAVA METHOD DECRYPT & IN THE MAIN STRING IN ORDER FOR THE RANDOM GENERATED PASSWORD TO BE DECRYPTED: BELOW IS FULL CODE.
import java.util.Random; import java.util.Scanner; public class RandomPasswordGenerator { private static final String LETTERS_AND_DIGITS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; private static final int MIN_PASSWORD_LENGTH = 10; private static final int MIN_DIGITS = 2; public static void main(String[] args) { String password = generateRandomPassword(); System.out.println("This is a test password to confirm the generator work. "); System.out.println("Generated password: " + password); System.out.println("******************** "); System.out.println("Enter a password to encrypt: "); System.out.println("Password to encrypt is: " +generateRandomPassword()); int key = 3; // shift key for Caesar Cipher String encryptedPassword = encrypt(password, key); System.out.println("Encrypted password would be: " + encryptedPassword); System.out.println("******************** "); System.out.println("Enter an encrypted password to decrypt: "); System.out.println("Password to decrypt is: " +generateRandomPassword()); String decryptedPassword = decrypt(encrypted, key); System.out.println("Decrypted password would be: " + decryptedPassword); } public static String generateRandomPassword() { StringBuilder password = new StringBuilder(); Random random = new Random(); int numDigits = 0; while (password.length() < MIN_PASSWORD_LENGTH || numDigits < MIN_DIGITS) { char c = LETTERS_AND_DIGITS.charAt(random.nextInt(LETTERS_AND_DIGITS.length())); if (Character.isDigit(c)) { numDigits++; } password.append(c); } return password.toString(); } public static String encrypt(String password, int key) { String encrypted = ""; for (int i = 0; i < password.length(); i++) { char c = password.charAt(i); if (Character.isLetter(c)) { if (Character.isUpperCase(c)) { encrypted += (char) ((c + key - 65) % 26 + 65); } else { encrypted += (char) ((c + key - 97) % 26 + 97); } } else { encrypted += c; } } return encrypted; } public static String decrypt(String encrypted, int key) { String decrypted = ""; for (int i = 0; i < encrypted.length(); i++) { char c = encrypted.charAt(i); if (Character.isLetter(c)) { if (Character.isUpperCase(c)) { decrypted += (char) ((c - key - 65 + 26) % 26 + 65); } else { decrypted += (char) ((c - key - 97 + 26) % 26 + 97); } } else { decrypted += c; } } return decrypted; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started