【发布时间】:2016-01-18 00:08:13
【问题描述】:
我正在查看http://rosettacode.org/wiki/Vigen%C3%A8re_cipher#Java 上提供的 Vigene Ciphere 源代码。我尝试自己测试该程序,但它没有输出我基于 vigene 所期望的值。例如,“dog”是单词,“bob”是密钥,我希望它被加密为“ech”,但改为“qot”。
public static void main(String[] args) {
String key = "bob";
String ori = "dog";
String enc = encrypt(ori, key);
System.out.println(enc);
}
static String encrypt(String text, final String key) {
String res = "";
text = text.toLowerCase();
for (int i = 0, j = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c < 'a' || c > 'z') continue;
res += (char)((c + key.charAt(j) - 2 * 'A') % 26 + 'A');
j = ++j % key.length();
}
return res;
}
但是输出不同。这是因为我对密码的理解不正确,还是对众所周知的 vigenere 密码采取了不同的方法。
【问题讨论】:
-
"但是输出不同" 是什么?
-
@RishavKundu 假设这不仅仅是我没听过的首字母缩写词,最好将其包含在问题中 - 已添加。
-
@AndyTurner 注意输出是大写的;我不知道这是否重要。