ASCII-Vigenere密码在java中的实现

ASCII-Vigenere密码在java中的实现,java,vigenere,Java,Vigenere,这是关于我从编程老师那里得到的作业。 我们将为所有可打印的ASCII码实现vigenere密码,并使用它运行测试 vigenere密码是一种使用多个caesar密码(移位为1)的多字母密码。也看到 我实现了我的vigenere,如下所示,但是我任务中的测试并没有产生实现所需的输出 我做了一个搜索,但似乎ASCII实现的这是相当稀疏的。 我的代码中是否有明显的错误我没有看到 public String encrypt(String plaintext) { String cipherTe

这是关于我从编程老师那里得到的作业。 我们将为所有可打印的ASCII码实现vigenere密码,并使用它运行测试

vigenere密码是一种使用多个caesar密码(移位为1)的多字母密码。也看到

我实现了我的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;
}
公共字符串加密(字符串明文){
字符串密文=”;
for(int i=0;i
代码和注释之间的唯一区别在于,当32到126之间包含95个字符时,您使用的是%94

将相应语句更改为使用模95,并将其稍微分解:

int caesar = _key.charAt(i % _key.length()) - 32;
int sum = c - 32 + caesar;
cipherText += (char) (sum % 95 + 32);
然后,解密算法可以使用所有相同的代码,只需将上面的第二条语句替换为:

int sum = c - 32 + (95 - caesar);

请你和蔼可亲地解释一下什么是vigenere cipher好吗?如果你能和蔼可亲地将此添加到你的帖子中,那将是一件了不起的事情。将其添加到帖子中:)