NodeJs 中的Crypto 加密模块
加密技术通常分为两大类:“对称式”和“非对称式”。
对称式加密:
就是加密和解密使用同一个密钥,通常称之为“Session Key ”这种加密技术在当今被广泛采用,如美国政府所采用的DES加密标准就是一种典型的“对称式”加密法,它的Session Key长度为56bits。
非对称式加密:
就是加密和解密所使用的不是同一个密钥,通常有两个密钥,称为“公钥”和“私钥”,它们两个必需配对使用,否则不能打开加密文件。
加密为系统中经常使用的功能,node自带强大的加密功能Crypto,下面通过简单的例子进行练习。
1、加密模块的引用:

var crypto=require('crypto');
var $=require('underscore');
var DEFAULTS = {
encoding: {
input: 'utf8',//输入数据格式为utf8
output: 'hex' //输出数据格式为hex(二进制)
},
algorithms: ['bf', 'blowfish', 'aes-128-cbc'] //使用的加密算法
};

默认加密算法配置项:
输入数据格式为utf8,输出格式为hex,
算法使用bf,blowfish,aes-128-abc三种加密算法;
2、配置项初始化:

function MixCrypto(options) {
if (typeof options == 'string')
options = { key: options };
options = $.extend({}, DEFAULTS, options);
this.key = options.key;
this.inputEncoding = options.encoding.input;
this.outputEncoding = options.encoding.output;
this.algorithms = options.algorithms;
}

加密算法可以进行配置,通过配置option进行不同加密算法及编码的使用。
3、加密方法代码如下:

MixCrypto.prototype.encrypt = function (plaintext) {
return $.reduce(this.algorithms, function (memo, a) {
var cipher = crypto.createCipher(a, this.key);
return cipher.update(memo, this.inputEncoding, this.outputEncoding)
+ cipher.final(this.outputEncoding)
}, plaintext, this);
};

使用crypto进行数据的加密处理。
4、解密方法代码如下:

MixCrypto.prototype.decrypt = function (crypted) {
try {
return $.reduceRight(this.algorithms, function (memo, a) {
var decipher = crypto.createDecipher(a, this.key);
return decipher.update(memo, this.outputEncoding, this.inputEncoding)
+ decipher.final(this.inputEncoding);
}, crypted, this);
} catch (e) {
return;
}
};

使用crypto进行数据的解密处理。
通过underscore中的reduce、reduceRight方法,进行加密和解密的算法执行。
简单的加密解密实例:
var crypto = require('crypto');
//加密
function encrypt(str, secret) {
var cipher = crypto.createCipher('aes192', secret);
var enc = cipher.update(str, 'utf8', 'hex');
enc += cipher.final('hex');
return enc;
}
//解密
function decrypt(str, secret) {
var decipher = crypto.createDecipher('aes192', secret);
var dec = decipher.update(str, 'hex', 'utf8');
dec += decipher.final('utf8');
return dec;
}
NodeJs 中的Crypto 加密模块的更多相关文章
- 浅析nodeJS中的Crypto模块,包括hash算法,HMAC算法,加密算法知识,SSL协议
node.js的crypto在0.8版本,这个模块的主要功能是加密解密. node利用 OpenSSL库(https://www.openssl.org/source/)来实现它的加密技术, 这是因为 ...
- 60.浅谈nodejs中的Crypto模块
转自:https://www.cnblogs.com/c-and-unity/articles/4552059.html node.js的crypto在0.8版本并没有改版多少,这个模块的主要功能是加 ...
- nodejs中的Crypto模块
我是属于实用型的选手,千万别问我过多原理性的东西,我只知道,这个是最好的,我就用它. http://cnodejs.org/topic/504061d7fef591855112bab5
- Nodejs进阶:crypto模块中你需要掌握的安全基础
本文摘录自<Nodejs学习笔记>,更多章节及更新,请访问 github主页地址. 一. 文章概述 互联网时代,网络上的数据量每天都在以惊人的速度增长.同时,各类网络安全问题层出不穷.在信 ...
- nodejs中常用加密算法
在常用的nodejs+express工程中,为了安全在登录及表单传输时,应该都需进行加密传输,目前个人常用到的加密方式有下列几种: 1.Hash算法加密: 创建一个nodejs文件hash.js,输入 ...
- nodejs中aes-128-cbc加密和解密
和java程序进行交互的时候,java那边使用AES 128位填充模式:AES/CBC/PKCS5Padding加密方法,在nodejs中采用对应的aes-128-cbc加密方法就能对应上,因为有使用 ...
- NodeJS学习笔记 进阶 (12)Nodejs进阶:crypto模块之理论篇
个人总结:读完这篇文章需要30分钟,这篇文章讲解了使用Node处理加密算法的基础. 摘选自网络 Nodejs进阶:crypto模块之理论篇 一. 文章概述 互联网时代,网络上的数据量每天都在以惊人的速 ...
- 不要在nodejs中阻塞event loop
目录 简介 event loop和worker pool event loop和worker pool中的queue 阻塞event loop event loop的时间复杂度 Event Loop中 ...
- nodejs中获取时间戳、时间差
Nodejs中获取时间戳的方法有很多种,例如: new Date().getTime() Date.now() process.uptime() process.hrtime() 平时想获取一个时间戳 ...
随机推荐
- What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
http://stackoverflow.com/questions/998979/difference-between-and-in-rails/25617607#25617607 http://s ...
- android studio 安装报错 unable to run mksdcard sdk tool
搜了一下原来缺少这个 sudo apt-get install lib32z1 lib32ncurses5 lib32stdc++6
- Jmeter+jenkins接口性能测试平台实践整理(一)
最近两周在研究jmeter+Jenkin的性能测试平台测试dubbo接口,分别尝试使用maven,ant和Shell进行构建,jmeter相关设置略. 一.Jmeter+jenkins+Shell+t ...
- grep和sed替换文件中的字符串
sed -i s/"str1"/"str2"/g `grep "str1" -rl --include="*.[ch]" ...
- expdp impdp终极教学
源地址:http://blog.csdn.net/giianhui/article/details/7788550
- [ActionScript 3.0] AS3 获取函数参数个数
function createFunction(param1:String,param2:String,param3:int=0):void { trace(arguments.length);//a ...
- php mssql 中文各种乱码
1 查询输出时乱码 (SELECT ) 因为MSSQL 数据库一般都是 GBK 编码,所以在php页面中加入 header('Content-Type:text/html; charset=GBK' ...
- MySql 日期函数
在 MySql 中经常会用到日期,关于常用的日期函数,做了以下的总结: 1 . now() 作用; 获取当前的日期 除此之外,获取当前日期的函数还有: current_timestamp(); cur ...
- Spring Boot 内嵌Tomcat的端口号的修改
操作非常的简单,不过如果从来没有操作过,也是需要查找一下资料的,所以,在此我简单的记录一下自己的操作步骤以备后用! 1:我的Eclipse版本,不同的开发工具可能有所差异,不过大同小异 2:如何进入对 ...
- devexpress中应用于girdviw中HtmlDataCellPrepared事件与CellEditorInitialize事件的区别
HtmlDataCellPrepared 事件为页面展示的时候对页面做的初始化(将id变为name) CellEditorInitialize 事件为页面在编辑时(新增.修改)时做的初始化,如将值填 ...