var crypto = require('crypto');

//md5加密

exports.md5 = function (str) {

var md5sum = crypto.createHash('md5');
md5sum.update(str);
str = md5sum.digest('hex');
return str;
}

//aes192加密

exports.encrypt = function (str, secret) {
var cipher = crypto.createCipher('aes192', secret);
var enc = cipher.update(str, 'utf8', 'hex');
enc += cipher.final('hex');
return enc;
}

//aes192解密

exports.decrypt = function (str, secret) {
var decipher = crypto.createDecipher('aes192', secret);
var dec = decipher.update(str, 'hex', 'utf8');
dec += decipher.final('utf8');
return dec;
}

//aes-128-cbc加密和解密

let str = 'aaaaaaaa';

const crypto = require('crypto');
let key = "08281690909";
let iv = "0828164343434";
let encipher = crypto.createCipheriv('aes-128-cbc', key, iv);
let encoded = encipher.update(str,'utf8','hex');
encoded += encipher.final('hex')
console.log('加密'+encoded);//da4941c6e797895527305b6b6ca28b37

let decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
let decoded = decipher.update(encoded,'hex','utf8')
decoded += decipher.final('utf8')
console.log('解密'+decoded)

//aes-128-cbc
exports.wxPaValidate = function(sessionKey, encryptedData, iv) {
const sessionKeyVal = new Buffer(sessionKey, 'base64')
const encryptedDataVal = new Buffer(encryptedData, 'base64')
const ivVal = new Buffer(iv, 'base64')
try {
// 解密
var decipher = crypto.createDecipheriv('aes-128-cbc', sessionKeyVal, ivVal)
// 设置自动 padding 为 true,删除填充补位
decipher.setAutoPadding(true)
var decoded = decipher.update(encryptedDataVal, 'binary', 'utf8')
decoded += decipher.final('utf8')
decoded = JSON.parse(decoded)
} catch (err) {
throw new Error('Illegal Buffer')
}
if (decoded.watermark.appid !== appId) {
throw new Error('Illegal Buffer')
}
return decoded
}

node加密的更多相关文章

  1. node 加密音频文件 和 解密音频文件

    fs.readFile('./downsuccess/'+name+'', {flag: 'r+', encoding: ''}, function (err, data) {           c ...

  2. node加密解密 crytpo

    var crypto = require('crypto'); exports.encrypt = function (str, secret) { var cipher = crypto.creat ...

  3. node-rsa加密,java解密调试

    用NODE RSA JS 加密解密正常,用JAVA RSAUtils工具类加密解密正常.但是用node加密玩的java解密不了.原因:node默认的是 DEFAULT_ENCRYPTION_SCHEM ...

  4. nodejs常用npm包

    express常用npm包整理如下 art-template 一款js模板引擎,性能不错 jayson     一款纯node的rpc应用包,可实现rpc服务.tcp.http等服务 multer   ...

  5. babeljs源码

    babel.min.js!function(e,t){"object"==typeof exports&&"object"==typeof mo ...

  6. node.js下使用RSA加密事例(windows)

    1.安装openss 直接下载window下的安装包 http://houjixin.blog.163.com/blog/static/3562841020144143494875/ 以我发博文现在的 ...

  7. [Node.js] 对称加密、公钥加密和RSA

    原文地址:http://www.moye.me/2015/06/14/cryptography_rsa/ 引子 对于加解密,我一直处于一种知其然不知其所以然的状态,项目核心部分并不倚重加解密算法时,可 ...

  8. 夺命雷公狗---node.js---15之加密

    node其实也给我们留下了密码的加密发送,不过一般都是用cmd5加密其实也是够了,但是sha1加密也要提下: /** * Created by leigood on 2016/8/31. */ var ...

  9. Node 实现 AES 加密,结果输出为“byte”。

    Node 实现 AES 加密,结果输出为"byte". 最近做个需求,对接一个平台的接口,该平台采用 AES (Advanced Encryption Standard)加密算法, ...

随机推荐

  1. vue指令问题

    挂载点:最外层标签就是vue实例的挂载点,即id或者类对应的 dom节点 模板:指挂载点内部的内容,在实例里使用template标签来构 建 h1标签放在body里面不使用 “template”是一样 ...

  2. 步步为营103-ZTree 二级联动

    1:添加引用 <%--流程类别多选--引用js和css文件--开始--%> <link rel="stylesheet" href="../css/zT ...

  3. 插件使用一进度条---nprogress

    nprogress 是像youtube一样在顶部出现进度条,用在一些加载比较缓慢的场景中. 官方网站是 http://ricostacruz.com/nprogress/ 源码在 https://gi ...

  4. 51Nod 1298 圆与三角形(计算几何)

    1298 圆与三角形  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出圆的圆心和半径,以及三角形的三个顶点,问圆同三角形是否相交.相交输出"Yes&quo ...

  5. 静态属性property

    静态属性property(是通过对象去使用) property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值 1 . 通过@property修饰过的函数属性,调用的时候无需在加() cla ...

  6. #20165323 Java实验四 Android程序设计

    一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:杨金川 学号:20165323 指导教师:娄嘉鹏 实验日期:2018年5月14日 实验时间:13:45 - 15:25 实验序号:实验 ...

  7. zabbix_agent YUM源配置

    wget http://repo.zabbix.com/zabbix/3.0/rhel/5/x86_64/zabbix-release-3.0-1.el5.noarch.rpm rpm -ivh za ...

  8. css3实现旋转表

    如图所示: css部分: <style> #clock{width:100px; height:100px; border-radius:50%; border:4px solid bla ...

  9. Codeforces 980D Perfect Groups 计数

    原文链接https://www.cnblogs.com/zhouzhendong/p/9074164.html 题目传送门 - Codeforces 980D 题意 $\rm Codeforces$ ...

  10. 最终一致性2PC复杂场景,事务报数据库操作超时。

    稀里糊涂的,忘了开启SqlServer的is read committed snapshot on