我正在尝试破译 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;
}
最佳答案
您明确忽略了空格。您只需添加这一行:
if (c == ' ') {
decipheredText += ' ';
}
确保将其放在此行之前:
if (c < 'A' || c > 'Z') continue;
关于Java 维吉尼亚密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53542147/