cryptojs: Comment générer des AES phrase de passe

Je veux générer un 256bit mot de passe pour mon chiffrement AES. Quand je vérifie le mot de passe après le cryptage, c'est différent de mon mot de passe initial. Ce que je fais mal? Ou est-il un mécanisme de sécurité, je ne suis pas au courant?

Mon code:

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;
}

La sortie est c'est à dire

0b05308c9a00f07044416bad7a51bacd282fc5c0c999551a4ff15c302b268b20
4df875993770411044fb35953166ee7833c32ca0741e9fec091dfa10138039e8

Est-ce normal ou suis-je en train de faire quelque chose de mal ici?
Merci pour l'aide!

Math.floor(Math.random() * 16) n'est pas équivalent à chance.integer({min: 0, max: 15}). Vous devriez essayer Math.round(Math.random() * 15)

OriginalL'auteur Yaron | 2014-04-05