码迷,mamicode.com
首页 > 编程语言 > 详细

nodejs与javascript中的aes加密

时间:2017-08-08 17:48:33      阅读:398      评论:0      收藏:0      [点我收藏+]

标签:vip   base64   ejs   nod   方法   aes加密   []   解决方法   UI   

 
// var CryptoJS = require("crypto-js");
// var key = CryptoJS.enc.Utf8.parse("8NONwyJtHesysWpM");
var crypto = require(‘crypto‘);
var key = new Buffer(‘7y05R9qwKaIKgIHh4vAw19X1zuknR21Y‘, ‘binary‘);
var iv = "";
var cryto_aes = module.exports = {};
 
//加密
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(‘‘);
}

 

//解密
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(‘‘);
 
}
 
 
参考:http://www.cnblogs.com/vipstone/p/5514886.html
参照上述方法时注意 iv 和 key的设置  可能会报Invalid IV length 错  解决方法 就是 设置成我粘贴的代码中的格式。
 

 

nodejs与javascript中的aes加密

标签:vip   base64   ejs   nod   方法   aes加密   []   解决方法   UI   

原文地址:http://www.cnblogs.com/blhw/p/7307890.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!