0
我正在用Java编写一次性键盘加密程序。即使使用新的随机密钥,程序也输出相同的“解密”文本。
这里是加密代码:一次性键盘每次都给出相同的答案
public static String oneTimePad(String plaintext, int[] key) {
int a = 0;
char[] ciphertext = plaintext.toUpperCase().toCharArray();
for (int i = 0; i < plaintext.length(); i++) {
if (ciphertext[i] < 'A' || ciphertext[i] > 'Z') {
} else {
ciphertext[i] = (char) ((((plaintext.charAt(i) - 65) + key[a]) % 26) + 65);
a++;
}
}
return new String(ciphertext);
}
这里是解密代码:
public static String padDecrypt(String ciphertext, int[] key) {
char[] plaintext = ciphertext.toUpperCase().toCharArray();
for (int i = 0, a = 0; i < ciphertext.length(); i++) {
if (plaintext[i] < 'A' || plaintext[i] > 'Z')
continue;
int x = (ciphertext.charAt(i) - 65) - key[a];
if (x < 0)
x += 26;
plaintext[i] = (char) ((x % 26) + 90);
a++;
}
return new String(plaintext).toLowerCase();
}
结果字符串 “这是有史以来最聪明的节目”
加密文本:
IYDS PJ UNN SIMZGQHZ UZRMODJ SWAM WOFM
关键示例:
9, 11, 15, 20, 1, 11, 21, 0, 3, 20, 16, 6, 2, 7, 6, 9, 0, 25, 2, 23, 0, 17, 23, 17, 8, 21, 16, 15, 4, 8, 22, 2, 17, 16, 23, 21, 4, 9
这里是“解密文本甚至从来没有使用不同的密钥改变:
sghr hr sgd rl`qsdrs oqnfq`l d[dq l`cd
如果有人能够给我一些建议,这将是伟大的!
我一次只修复这些东西中的一个!当我解决它的工作。感谢您的帮助! – user3590907