我正在用Java做一个vigenere密码.加密运行得很好,但我无法解密.以下是我的代码

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Do you want to encrypt or decrypt? (E/D): ");
        String mode = scanner.nextLine().toUpperCase();
        System.out.print("Input file name: ");
        String inputFileName = scanner.nextLine();
        System.out.print("Output file name: ");
        String outputFileName = scanner.nextLine();
        System.out.print("Keyword (all capitals): ");
        String keyword = scanner.nextLine().toUpperCase();
        scanner.close();
        
        try {
            BufferedReader reader = new BufferedReader(new FileReader(inputFileName));
            BufferedWriter writer = new BufferedWriter(new FileWriter(outputFileName));
            
            int keywordIndex = 0;
            String line = reader.readLine();
            while (line != null) {
                for (int i = 0; i < line.length(); i++) {
                    char c = line.charAt(i);
                    if (Character.isLetter(c)) {
                        int shift = keyword.charAt(keywordIndex) - 'A';
                        if (mode.equals("D")) {
                            shift = 26 - shift;
                        }
                        else {
                        c = (char)(((c + shift - 'A') % 26) + 'A');
                        keywordIndex = (keywordIndex + 1) % keyword.length();
                        c = Character.toUpperCase(c);
                        }
                    }
                    writer.write(c);
                }
                writer.newLine();
                line = reader.readLine();
            }
            reader.close();
            writer.close();
            
            System.out.println("Done.");
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

除了解密部分,代码的所有部分都运行得很好.出于某种原因,它无法解密.它也创建了我的输出文件,但加密是1相同的,或者2再次加密它.

推荐答案

最里面的else块的内容应该是更高一级.您格式化代码的方式肯定是您的意图,但您的IDE只是添加了不必要的else{}:-) 只需注释掉两行,它应该可以工作:

                    if (Character.isLetter(c)) {
                        int shift = keyword.charAt(keywordIndex) - 'A';
                        if (mode.equals("D")) {
                            shift = 26 - shift;
                        }
                        // else {
                        c = (char)(((c + shift - 'A') % 26) + 'A');
                        keywordIndex = (keywordIndex + 1) % keyword.length();
                        c = Character.toUpperCase(c);
                        // }
                    }

Java相关问答推荐

是否可以从@ TrustMapping中删除特定方法的基路径?

将具有多个未知字段的SON映射到Java POJO

更新我们的一个文物后出现了严重的符号引用错误

我的scala文件失败了Scala.g4 ANTLR语法

Springdoc Whitelabel Error Page with Spring V3

Java Stream,需要更新列表对象列表

将关键字与正文中的_Allowed匹配,但带有__Signing可选后缀

SpringBoot+Java 17@Valid未验证POJO

如何在Java记录中设置BigDecimal类型属性的精度?

如何在Application.yaml中连接字符串?

Java Mooc.fi Part 12_01.Hideout -返回和删除方法

如何将Pane的图像快照保存为BMP?

为什么Collectors.toList()不能保证易变性

Java 11 HttpCookie.parse在解析包含JSON的Cookie时引发IlLegalArgumentException

为什么在下面的Java泛型方法中没有类型限制?

Java中的发布/订阅-Long Live和Short Live Publisher,哪种方法是正确的?

JavaFX,GridPane:在GridPane的列中生成元素将保持所有列的宽度

如何使用Rascal Evaluator从编译的JAR访问Rascal函数?

在输入端没有可行的替代方案'; Select *';

java 11上出现DateTimeParseException,但java 8上没有