Как мне сделать расшифровку?
Я закончил делать шифрование, и это работает, но я не знаю, как сделать расшифровку. Я пробовал делать ut, но результат не тот.
Может ли кто-нибудь исправить мой код?
Ниже приведен код того, что я сделал
Что я уже пробовал:
package oops; import java.util.Arrays; import java.util.Map; import java.util.Scanner; public class Main { private static final String Bforce = null; private static final String BMessage = null; public static void main(String[] args) { System.out.println("----Vigenere Cipher Encryptor----\n"); Scanner in = new Scanner(System.in); // Allow the user to choose if they want to encrypt or decrypt or brute force a message System.out.println(" 1 to encrypt a message "); System.out.println(" 2 to decrypt a message "); System.out.println(" 3 to brute force "); int input = in.nextInt(); // ENCRYPT MESSAGE if (input == 1) { System.out.print("Enter the password: "); String key = in.next(); // key number System.out.print("Enter the key number (1-26) : "); int number = in.nextInt(); if (number >= 1 && number <= 26) { } else { } // message System.out.print("Enter the message : "); String EMessage = in.next(); String encryptMessage = encrypt(EMessage, key, number); System.out.println("The encrypted message is: " + encryptMessage); //for (int x = 0; x <= number; x++) { // replaceAll("[-+.^:,]",""); // encrypt the message // String encryptMessage = encrypt(EMessage, key); // System.out.println("The encrypted message is: " + encryptMessage); } // DECRYPT MESSAGE option 2 else if (input == 2) { System.out.print("Enter the password: "); String key = in.next(); // key number System.out.print("Enter the key number (1-26) : "); // NUM 1- 26 OR ELSE IT GIVES AN ERROR int number = in.nextInt(); if (number >= 1 && number <= 26) { } else { } // prompt for message System.out.print("Enter the message: "); String dMessage = in.next(); String decryptMessage = decrypt(dMessage, key, number); System.out.println("The decrypted message is: " + decryptMessage); //for (int x = 0; x <= number; x++) { //replaceAll("[-+.^:,?_-`*/()]",""); // decrypt the message //// String DecryptMessage = decrypt(DMessage, key); // System.out.println("The decrypted message is: " + decryptMessage); // brute force option 3 } else if (input == 3) { System.out.print("Enter the password: "); String password = in.next(); // key number System.out.print("Enter the key number (1-26) : "); // 1-26 OR ELSE ERROR int number = in.nextInt(); if (number >= 1 && number <= 26) { } else { } // message for user System.out.print("Enter the message: "); String BMessage = in.next(); for (int x = 0; x <= number; x++) { replaceAll("[-+.^:,]",""); // decrypt w brute force String BruteMessage = Bforce(BMessage, password); System.out.println("The decrypted message is: " + BruteMessage); } } else { System.out.println("Wrong Input!"); } in.close(); } private static void replaceAll(String string, String string2) { } // Encryption 1 // Encryption Logic: public static String encrypt(String Message, String Key, int number) { String eMessage = ""; Message = Message.toUpperCase(); Key = Key.toUpperCase(); for (int i = 0, j = 0; i < Message.length(); i++) { // Get the character from the message char letter = Message.charAt(i); // Get the ASCII value of the character int ascii = (int) letter; // Get the value of the character in the password int passVal = (int) (Key.charAt(j)) - 65; // Calculate the amount of the shift int shiftPos = ascii + passVal - number; // Check if the position is > 90 (that is, past the letter 'Z') if (shiftPos > 90) { // Go back to the letter 'A' eMessage += (char) (shiftPos % 90 + 64); } else { // Take the character at the shift position eMessage += (char) (ascii + passVal - number); } // System.out.println(EMessage); j = ++j % Key.length(); } return eMessage; } // Decryption 2 // Decryption Logic: public static String decrypt(String Message, String Key, int number) { String dMessage = ""; Message = Message.toLowerCase(); Key = Key.toLowerCase(); for (int i = 0, j = 0; i < Message.length(); i++) { char letter = Message.charAt(i); int num = (int) letter; int pass = (int) (Key.charAt(j)) - 'a'; int shiftPos = num - pass + number; if (shiftPos > 90) { // Go back to the letter 'A' dMessage += (char) (shiftPos % 90 - 64); } else { dMessage += (char) (num - pass + number); } dMessage += (char)((num - pass - number) % 26 + 65); j = ++j % Key.length(); dMessage = dMessage.replaceAll("[^a-zA-Z0-9]", ""); } return dMessage; } // BRUTE FORCE 3 public static <Bmessage> String Bforce(String Message, String key) { String BMessage = ""; Message = Message.toLowerCase(); for (int i = 0, j = 0; i < Message.length(); i++) { char letter = Message.charAt(i); BMessage += (char)((letter - key.charAt(j) + 26) % 26 + 65); j = ++j % key.length(); } return BMessage.replaceAll("[", ""); } }