AES encryption in Nodejs and JavaScript

Source: Internet
Author: User
Tags base64 decrypt

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.