【问题标题】:Java Vigenere CipherJava Vigenere 密码
【发布时间】:2018-11-29 15:16:23
【问题描述】:

我正在尝试破译 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;
}

【问题讨论】:

  • 当我输入 BEXR TKGKTRQFARI 时,我得到 TI ZIXDYOVIUYSVK - 当我输入 JAVA PROGRAMMING 时,我得到 BEXR TKGKTRQFARI - 所以这里的示例有些问题。
  • 对不起,这是加密的,我编辑它。
  • 这种密文会泄露明文的信息。

标签: java encryption vigenere


【解决方案1】:

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

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

确保把它放在这一行之前:

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

【讨论】:

    【解决方案2】:

    您忽略了空格。在检查字符范围 'A' 到 'Z' 时检查空格,并将其添加到 decipheredText 作为空格,因为您不希望空格被视为另一个字符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多