凯撒加密。索引中的数组越界 73(共 73)

Caesar encryption. Array out of bounds in index 73 out of 73

我正在为数学演示编写凯撒加密代码。 今天,我一直在为我的演示文稿编写代码。早上这段代码一直在工作。但现在在学校,我得到一个例外,我认为 for 计算了我的符号 table.

的长度

异常:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Index 73 out of bounds for length 73 at caesarForm.decrypt(caesarForm.java:81)

第 81 行是 decrypt()

中的第 3 for

如果有人能查看我的代码,我会很高兴,因为以我 3 个月的编码经验,我无法理解发生了什么。

//Alphanumeric table
    final static char[] symbs = new char[] {'.',',','1','2','3','4','5','6','7','8','9','0','/','z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a',' '
                    ,'ß','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','ä','ö','ü','Ü','Ö','Ä'};
    final static int charlength = 73;

    //var global key
    int key = 0;

    //en- and decryption vars
    char[] letters = new char[255];
    char[] storg = new char[255];
    char[] sterg = new char[255];
    StringBuilder str = new StringBuilder();
    int cnt = 0;
    int cnt2 = 0;
    int c = 0;
    String bakToStr;

textField1.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                super.keyTyped(e);
                char iNumber = e.getKeyChar();
                if (!(Character.isDigit(iNumber))
                        || iNumber == KeyEvent.VK_BACK_SPACE
                        || iNumber == KeyEvent.VK_DELETE) {
                    e.consume();
                    JOptionPane.showMessageDialog(contentPane, "Nur Zahlen werden als schlüssel akzeptiert", "Achtung!", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });

public String encrypt(String Text) {
        letters = new char[255];
        storg = new char[255];
        sterg = new char[255];
        str = new StringBuilder();
        cnt = 0;
        cnt2 = 1;
        c = 0;
        bakToStr = "";
        for (int i = 0; i <Text.length(); i++) {
            letters[i] = Text.charAt(i);
        }
        for (; cnt < Text.length(); cnt++) {
            for (;letters[cnt] != symbs[cnt2];cnt2++) {
            }
            c = (cnt2 + key) %charlength;
            storg[cnt] = symbs[c];
            cnt2 = 0;
        }
        str.append(storg);
        bakToStr = String.valueOf(str);
        return bakToStr;
    }

    public String decrypt(String Text) {
        letters = new char[255];
        storg = new char[255];
        sterg = new char[255];
        str = new StringBuilder();
        cnt = 0;
        cnt2 = 1;
        c = 0;
        bakToStr = "";
        for (int i = 0; i <Text.length(); i++) {
            letters[i] = Text.charAt(i);
        }
        for (; cnt < Text.length(); cnt++) {
            for (;letters[cnt] != symbs[cnt2];cnt2++) {
            }
            c = (cnt2 - key) %charlength;
            while (c <= 0) {
                c += charlength;
            }
            storg[cnt] = symbs[c];
            cnt2 = 0;
        }
        str.append(storg);
        bakToStr = String.valueOf(str);
        return bakToStr;
    }

请考虑这一行:

while (c <= 0) {
    c += charlength;
}

如果c==0会怎样?我认为它会跳转到 73charlength。所以,这里更好的方法是使条件 while (c < 0) 而不是 while (c <= 0).

但是,我认为主要问题在于第三个 for 循环:

for (;letters[cnt] != symbs[cnt2];cnt2++)

因为 cnt2 是用值 1(而不是 0)初始化的,并且会递增。如果不满足条件,则 cnt2 将始终递增,因此它变为 73 并抛出 indexOutOfBound 异常。

希望对您有所帮助:)

我发现了问题:

StringBuilder str = new StringBuilder();

我不知道为什么,但是没有 Stringbuilder 我就不会再遇到任何问题了。

感谢 Mohammed 和@MadProgrammer 的帮助:)

哈萨克斯坦,你好。