Java 维吉尼亚密码

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

最佳答案

您明确忽略了空格。您只需添加这一行:

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

确保将其放在此行之前:

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

关于Java 维吉尼亚密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53542147/

相关文章:

java - 为什么我的 if-else 分支的其余部分没有执行?

python - DES.MODE_OFB 无法恢复明文

c - 文件描述符的代码问题。 C (Linux)

c - KERN_INVALID_ADDRESS 让我难住了

java - 如何包含 webstart 应用程序的外部配置

java - 无法让 String.split() 在 android 上按预期工作

c - Vigenere Cipher 只能在处理 C 中的空格 ("") 之前有效 - 为什么?

c - 如何重复数组中的字符

java - 使用最新版本的 jdbc 连接器连接到 1.8 HyperSQL 数据库

c# - 数字签名 : Encrypting the Hash vs Signing the Hash?