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. VMware 虚拟机 Ubuntu 系统执行 ifconfig 命令 eth0没有IP地址(intet addr、Bcast、Mask) 解决:UP BROADCAST MULTICAST 问题

    VMware 虚拟机 ifconfig没有net_addr地址.Bcast.Mask的解决方法 使用时间长的虚拟机,会莫名其妙的连接不上网 在终端中,使用ifconfig命令查看Ubuntu系统的IP ...

  3. python多线程爬取-今日头条的街拍数据(附源码加思路注释)

    这里用的是json+re+requests+beautifulsoup+多线程 1 import json import re from multiprocessing.pool import Poo ...

  4. linux dig 命令使用方法

    ref:https://www.imooc.com/article/26971?block_id=tuijian_wz dig 命令主要用来从 DNS 域名服务器查询主机地址信息. 查询单个域名的 D ...

  5. hadoop集群运行jps命令以后Datanode节点未启动的解决办法

    出现该问题的原因:在第一次格式化dfs后,启动并使用了hadoop,后来又重新执行了格式化命令(hdfs namenode -format),这时namenode的clusterID会重新生成,而da ...

  6. mysql binary

    mysql在比较字符串的时候是忽略大些写的 比如有用户叫ABC和abc select * from `sys_user` where username = 'abc' 会出来两条记录 select * ...

  7. 【loj6142】「2017 山东三轮集训 Day6」A 结论题+Lucas定理

    题解: 当奇数 发现答案就是C(n,1)^2+C(n,3)^2+...C(n,n)^2 倒序相加,发现就是C(2n,n) 所以答案就是C(2n,n)/2 当偶数 好像并不会证 打表出来可以得到 2.当 ...

  8. C# 之 6.0 新特性

    VS2015内置的C#版本为6.0,学习了一下C#6.0的新特性. 特性1:自动属性初始化 (Initializers for auto-properties) 以前我们是这么写的 public st ...

  9. MySQL 存储过程中分页

    MySQL数据库中,自定义存储过程查询表中的数据,带有分页功能.具体实例如下代码: 1 DROP PROCEDURE IF EXISTS `sampledb`.`proc_GetPagedDataSe ...

  10. content字符生成配合CSS3 animation的点点点loading

    CSS代码: dot { display: inline-block; height: 1em; line-height: 1; vertical-align: -.25em; overflow: h ...