Java Vigenere密码

时间:2018-11-29 15:16:23

标签: java encryption vigenere

我正在尝试解密Vigenere_Cipher 当我输入BEXR TKGKTRQFARI时,输出为JAVAPROGRAMMING但我想要 放置JAVA PROGRAMMING之类的空间。

我的代码

public static String VigenereDecipher(String text) {
    String keyword = "SECRET";
    String decipheredText = "";
    text = text.toUpperCase();
    for (int i = 0, j = 0; i < text.length(); i++) {
        char c = text.charAt(i);
        if (c < 'A' || c > 'Z') continue;
        decipheredText += (char)((c - keyword.charAt(j) + 26) % 26 + 'A');
        j = ++j % keyword.length();
    }  
    return decipheredText;
}

2 个答案:

答案 0 :(得分:1)

您明确地忽略了空格。您只需要添加以下行:

if (c == ' ') {
   decipheredText += ' ';
}

请确保将其放在此行之前:

if (c < 'A' || c > 'Z') continue;

答案 1 :(得分:0)

您正在忽略空格。在检查字符范围“ A”到“ Z”时检查空间,仅当您不希望将空间视为另一个字符时,才将其作为空格添加到decipheredText中。