我想要生成一个256位的密码来进行AES加密。当我在加密后检查这个密码时,它与我的初始密码不同。我做错了什么?还是有一些我不知道的安全机制?
我的代码:
password=Generate_key();
var encrypted = CryptoJS.AES.encrypt("Message", password);
//Those two should be the same
document.write(password+"<br>");
document.write(encrypted.key);
function Generate_key() {
var key = "";
var hex = "0123456789abcdef";
for (i = 0; i < 64; i++) {
key += hex.charAt(Math.floor(Math.random() * 16));
//Initially this was charAt(chance.integer({min: 0, max: 15}));
}
return key;
}
输出结果如下:
0b05308c9a00f07044416bad7a51bacd282fc5c0c999551a4ff15c302b268b20 4df875993770411044fb35953166ee7833c32ca0741e9fec091dfa10138039e8
这个结果是正常的,你没有做错什么。感谢您的提问!
Math.floor(Math.random() * 16)
不等同于chance.integer({min: 0, max: 15})
。你应该尝试使用Math.round(Math.random() * 15)
。 - Biduleohm