我正在查看http://rosettacode.org/wiki/Vigen%C3%A8re_cipher#Java上提供的Vigene Ciphere源代碼。我試着自己測試了這個程序,並沒有輸出我期望基於vigene的值。例如'dog'這個詞,'bob'是我希望這個被加密爲'ech'的關鍵字,但是'qot'代替。Vigenere密碼輸出
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密碼採取了不同的方法。
「但是,輸出是不同的」它是什麼呢? –
@AndyTurner QOT – Rishav
@RishavKundu假設這不僅僅是我沒聽說過的縮寫詞,最好在問題中加入它 - 補充。 –