如何加密解密Node.js加密aes256密码

时间:2014-06-14 07:48:55

标签: javascript node.js encryption

我必须加密javascript前端代码中的密码字符串并使用$ .ajax我曾经在node.js中发送加密密码,但我不知道如何解密node.js中的密码

我有一个node.js的例子,但我如何使用javascript(前端)加密代码并执行下面给出的相同解密。

var crypto = require('crypto');

var algorithm = 'aes256'; // or any other algorithm supported by OpenSSL
var key = 'password';
var text = 'I love kittens';

var cipher = crypto.createCipher(algorithm, key);  
var encrypted = cipher.update(text, 'utf8', 'hex') + cipher.final('hex');
var decipher = crypto.createDecipher(algorithm, key);
var decrypted = decipher.update(encrypted, 'hex', 'utf8') + decipher.final('utf8');

console.log(decrypted);

1 个答案:

答案 0 :(得分:0)

好吧我得到了解决方案。

客户端(JavaScript)

var encrypted = CryptoJS.AES.encrypt("password", "Secret Passphrase");
$.ajax({
 url: "/enc_md5",
 type: "POST",
 data: {username:"uname",password:encrypted.toString()},
 success:function (data) {
   alert(data);
 }
});

服务器端(Node.js的)

var CryptoJS = require("crypto-js");
app.post('/enc_md5', function (req,res) {
    var encrypted = req.body.password;
    var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase");
    console.log(decrypted.toString(CryptoJS.enc.Utf8));
});