生成密码字母表

Generating Cipher Alphabet

我要根据用户输入的 shift 键生成一个 密码字母表 (基于凯撒密码),我的代码似乎是错误的。

我不明白我的 cipherAlphabet 方法有什么问题,因为每当我尝试测试结果时,它最终都会给我一个错误“java.lang.ArrayIndexOutOfBoundsException: 26."

所以,这可能与我设置的数组有关,但我看不到。我知道这可能非常简单,但有人可以帮我吗?

P.S. - 我尝试在 SO 和其他网站上寻找现有提示以寻求帮助,但无济于事。

我会用一种方法完成所有的转换和加密,但不幸的是我不能那样做(显然,我必须这样做)。这是我拥有的:

private String message;
private static int shiftKey;
public static final String[] ALPHABET = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "N",
                                         "M", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
public static String[] cipherAlphabet = new String[ALPHABET.length];

/**
 * Constructor for objects of class CaesarShiftEncryption
 */
public CaesarShiftEncryption(String m, int shift)
{
    message = m;
    shiftKey = shift;
}

public static String[] cipherAlphabet()
{
    for(int i = 0; i < ALPHABET.length; i++)
    {
        if(i >= 23)
        {
            cipherAlphabet[i] += ALPHABET[26 - shiftKey];
        }
        else
        {
            cipherAlphabet[i] += ALPHABET[i + shiftKey];
        }
    }
    return cipherAlphabet;
}

你需要重新开始!尝试这样的事情,使用 mod 操作,你会从字母表的开头重新开始,如果你碰巧碰到结尾

cipherAlphabet[i] += ALPHABET[(i + shiftKey) % ALPHABET.length]