删除用于简单字母表的 ascii

Removing ascii using for simple alphabet

我需要在不使用 ASCII table(它是 vigenere 密码的编码器)的情况下修改此方法

private static final int ALPHABET = 26;
public static final String KEY = "AB";

public String encode(String input) {
    StringBuilder letters = new StringBuilder();
    input = input.toUpperCase();
    for (int i = 0, j = 0; i < input.length(); i++) {
        char symbol = input.charAt(i);
        letters.append((char) (((symbol - 65) + (KEY.charAt(j) - 65)) % ALPHABET + 65));
        j = ++j % KEY.length();
    }
    return letters.toString().toLowerCase();
}

就像@Kayaman 说的,我们可以手写我们的字母并作为参考。这样,如果我们的字母表发生变化(例如,更改为另一种语言),我们就不必更改代码。新代码如下所示:

private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final String KEY = "AB";

public String encode(String input) {
    StringBuilder letters = new StringBuilder();
    input = input.toUpperCase();
    for (int i = 0, j = 0; i < input.length(); i++) {
        char symbol = input.charAt(i);
        char keySymbol = KEY.charAt(j);
        int newIndex =
            (ALPHABET.indexOf(symbol) + ALPHABET.indexOf(keySymbol))
                % ALPHABET.length();
        char newSymbol = ALPHABET.charAt(newIndex);
        letters.append(newSymbol);
        j = ++j % KEY.length();
    }
    return letters.toString().toLowerCase();
}