0

これは、プログラミングのインストラクターからもらった課題についてです。すべての印刷可能な ASCII コードに対して vigenere 暗号を実装し、それを使用してテストを実行します。

vigenere 暗号は、シフトが 1 の複数の caesar 暗号を使用する多アルファベット暗号です。ウィキペディアも参照

以下のように vigenere を実装しましたが、課題のテストでは、実装に必要な出力が生成されません。

検索しましたが、これの ASCII 実装は非常にまばらなようです。表示されていないコードに明らかなエラーがありますか?

public String encrypt(String plaintext) {

    String cipherText = "";
    for (int i = 0; i < plaintext.length();  i++) {
        char c = plaintext.charAt(i);

        // vigenere for the letters A-Z is defined as vigenere(m) = m[i] + k[i] % 26
        // where m[i] is the message and k[i] is the key.
        //
        // For ASCII support, as printable space starts at 32, 
        // subtract 2*32 from the sum of keyChar and messageChar to get zero based.
        // Then calculate the modulo of this sum to be sure to stay in bounds.
        // Finally add 32 to the result of the modulo operation to place it in the 32 - 126 range.
        //
        // The key wrapping is implemented as i % _key.length() to restart
        // from the beginning if the end of the key is reached.
        cipherText += (char) ((c + _key.charAt(i % _key.length()) - 64) % 94 + 32);
    }

    return cipherText;
}
4

1 に答える 1