Java Vigenere Cipher



我正在尝试破解Vigener_Cipher当我输入BEXR TKGKTRQFARI时,输出是JAVAPROGRAMMING但我想要以放置类似CCD_ 3的空间。

我的代码

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;
}

您显式忽略了空格。您只需添加以下行:

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

一定要把它放在这行前面:

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

您忽略了空格。在检查字符范围"A"到"Z"时检查空格,并仅在不希望空格被视为另一个字符时将其作为空格添加到解密文本中。

最新更新