【发布时间】:2019-02-24 17:06:30
【问题描述】:
我用 JavaScript 制作了 Vigenère 密码。
如果我在 Firefox 中运行我的代码,我会得到以下输出:
�QZ4Sm0]m
在谷歌浏览器中看起来像这样
QZ4Sm0]m
如何避免这些符号或如何使它们可见? 我做错了什么?
function vigenere(key, str, mode) {
var output = [str.length];
var result = 0;
var output_str;
for (var i = 0; i < str.length; i++) {
if (mode == 1) {
result = ((str.charCodeAt(i) + key.charCodeAt(i % key.length)) % 128);
output[i] = String.fromCharCode(result);
} else if (mode == 0) {
if (str.charCodeAt(i) - key.charCodeAt(i % key.length) < 0) {
result = (str.charCodeAt(i) - key.charCodeAt(i % key.length)) + 128;
} else {
result = (str.charCodeAt(i) - key.charCodeAt(i % key.length)) % 128;
}
output[i] = String.fromCharCode(result);
}
}
output_str = output.join('');
return output_str;
}
console.log(vigenere("Key", "Plaintext", 1))
【问题讨论】:
-
我给你做了一个 sn-p 并删除了你多余的)
-
您在 Chrome 中也是如此。它只是在控制台中不可见
-
如果您更改为
output[i] = String.fromCharCode(result); console.log(JSON.stringify(output[i])),您将看到u\001b作为第一个字符
标签: javascript encryption cryptography vigenere