var Cryptojs = require ("Crypto-js");//var key = CryptoJS.enc.Utf8.parse ("8NONwyJtHesysWpM"); var crypto = require (' Cry PTO '), var key = new Buffer (' 7y05r9qwkaikgihh4vaw19x1zuknr21y ', ' binary '), var IV = "", var Cryto_aes = Module.exports = {}; Encrypt the Cryto_aes. Encrypt = function (data) {IV = IV | | ""; var clearencoding = ' UTF8 '; var cipherencoding = ' base64 '; var cipherchunks = []; var cipher = Crypto.createcipheriv (' AES-256-ECB ', key, iv); Cipher.setautopadding (TRUE); Cipherchunks.push (cipher.update (data, clearencoding, cipherencoding)); Cipherchunks.push (Cipher.final (cipherencoding)); Return Cipherchunks.join (");}
Decrypt the Cryto_aes. Decrypt = function (data) {if (!data) {return ""; } IV = IV | | ""; var clearencoding = ' UTF8 '; var cipherencoding = ' base64 '; var cipherchunks = []; var decipher = crypto.createdecipheriv (' AES-256-ECB ', key, iv); Decipher.setautopadding (TRUE); Cipherchunks.push (decipher.update (data, cipherencoding, clearencoding)); Cipherchunks.push (Decipher.final (clearencoding)); Return Cipherchunks.join ("); } reference: Http://www.cnblogs.com/vipstone/p/5514886.html when referring to the above method, note that the settings of IV and key may be reported invalid IV length The wrong solution is to set the formatting to the code I pasted.
AES encryption in Nodejs and JavaScript