node通过crypto加密解密

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jbguo/article/details/90169546

MD5摘要算法

var crypto = require('crypto');
// 添加「utf-8」避免出现中文加密不致的情况 注:一个crypto实例只能调用一次
var md5Sign = function (data) {
    var md5 = crypto.createHash('md5').update(data, 'utf-8').digest('hex');
    return md5;
}

AES对称加密解密

const crypto = require('crypto');
//AES对称加密
var secretkey = 'password'; //唯一(公共)秘钥
function encrypt(a) {
  var content = a;
  var cipher = crypto.createCipher('aes192', secretkey); //使用aes192加密
  var enc = cipher.update(content, 'utf8', 'hex'); //编码方式从utf-8转为hex;
  return (enc += cipher.final('hex')); //编码方式转为hex;
}
// encrypt('花样百出');
function decrypt() {
  let enc = encrypt('花样百出');
  console.log(enc);
  //AES对称解密
  var decipher = crypto.createDecipher('aes192', secretkey);
  var dec = decipher.update(enc, 'hex', 'utf8');
  dec += decipher.final('utf8');
  console.log('AES对称解密结果:' + dec);
}
decrypt();

参考:《NodeJS开发教程14-Crypto加密与解密》

猜你喜欢

转载自blog.csdn.net/jbguo/article/details/90169546
今日推荐