我写了一个Java程序,用Vigenere密码进行编码的加密工作正常,但解密不适用于一些特殊情况。Vigenere解密奇怪
例如,如果解密时明文是数 'k',密钥为 'y' 的它正确地产生密文的 'i'((10 + 24 = 34%26 = 8))
然而((8-24)= -16%26 = -16)),即使它是正值,也是Q.当它应该正确地解密回''时, k'这将是10.
任何人都可以帮助我吗?如果需要,我可以发布更多代码。
---链接到维基Viginare加密算法http://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher ---
//decryption
else{
for (int i=0; i < a.length(); i++){
for (int j=0; j < full.length(); j++){
//finding the index of the current cipher text letter
if (a.charAt(i) == full.charAt(j)){
positionP = j;
}
//finding the index of the current key letter
if(key.charAt(i)==full.charAt(j)){
positionK = j;
}
}
//using the formula for vigenere encoding it adds the newly encrypted character to the output
output = output + full.charAt((positionP - positionK)%26);
}
}
太谢谢你了!我真的被困住了,我发现好奇的是没有任何解密Vigenere的算法提到了这个!他们通常会说:(CipherTextIndex - keyTextIndex)%26 = IndexOfPlainText 我可能会在wiki中添加一条关于此的注释。 非常感谢你! – 2013-02-24 18:51:45
@Luke它依赖于你如何定义负数的其余部分。见 http://stackoverflow.com/questions/4403542/how-does-java-do-modulus-calculations-with-negative-numbers – Javier 2013-02-24 18:58:09