简体   繁体   中英

how to encrypt-decrypt Node.js Crypto aes256 Cipher

i have to encrypt password string inside javascript front-code and using $.ajax i used to send that encrypted password in node.js but i dont know how to decrypt that password inside node.js

i got one example with node.js but how can i use encryption code with javascript(front-end) and do the same decryption given in bellow code.

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

alright i got solution.

client-side (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);
 }
});

Server-Side(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));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM