Java 我的程序能够对一条消息进行加密,但我不知道如何准确地解密,因为它无法反向工作

Java 我的程序能够对一条消息进行加密,但我不知道如何准确地解密,因为它无法反向工作,java,Java,解密是我遇到的问题 public static void encrypt(Scanner scan, PrintWriter out, String key) throws IOException { while(scan.hasNextLine()){ String lineRead = scan.nextLine(); for(int i=0; i < lineRead.length(); i++){

解密是我遇到的问题

public static void encrypt(Scanner scan, PrintWriter out, String key) throws IOException {
        while(scan.hasNextLine()){
            String lineRead = scan.nextLine();

            for(int i=0; i < lineRead.length(); i++){
                char c = lineRead.charAt(i);
                out.print(shiftUpByK(c, key.charAt(i%key.length()) - 'a' ));
            }
        out.println();

    }
}

public static void decrypt(Scanner scan, PrintWriter out, String key) {
    while(scan.hasNextLine()){
        String lineRead = scan.nextLine();

        for(int i = 0; i < lineRead.length(); i++){
            char c = lineRead.charAt(i);
            out.print(shiftDownByK(c,key.charAt(i%key.length())));
        }
        out.println();
    }

}

public static final int NUM_LETTERS = 26;

public static char shiftUpByK(char c, int k) {
    if ('a' <= c && c <= 'z')
        return (char) ('a' + (c - 'a' + k) % NUM_LETTERS);
    if ('A' <= c && c <= 'Z')
        return (char) ('A' + (c - 'A' + k) % NUM_LETTERS);
    return c; // don't encrypt if not an alphabetic character
}

public static char shiftDownByK(char c, int k) {
    return shiftUpByK(c, NUM_LETTERS - k);
}

解密时你需要加上“a”:你忘记了那部分。我试过了,但似乎没有解密it@ChrisV相应地更新您的答案。包括带有输入/输出的主设备。