nodejs加密Crypto的实例代码_.docx

上传人:PIYPING 文档编号:11619325 上传时间:2021-08-26 格式:DOCX 页数:4 大小:12.52KB
返回 下载 相关 举报
nodejs加密Crypto的实例代码_.docx_第1页
第1页 / 共4页
nodejs加密Crypto的实例代码_.docx_第2页
第2页 / 共4页
nodejs加密Crypto的实例代码_.docx_第3页
第3页 / 共4页
nodejs加密Crypto的实例代码_.docx_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《nodejs加密Crypto的实例代码_.docx》由会员分享,可在线阅读,更多相关《nodejs加密Crypto的实例代码_.docx(4页珍藏版)》请在三一文库上搜索。

1、nodejs加密Crypto的实例代码_ 下面我就为大家带来一篇nodejs加密Crypto的实例代码。我觉得挺不错的, 现在就分享给大家,也给大家做个参考。 加密技术通常分为两大类:“对称式”和“非对称式”。 对称式加密: 就是加密和解密用法同一个密钥,通常称之为“Session Key ”这种加密技术在当今被广泛采纳,如美国政府所采纳的DES加密标准就是一种典型的“对称式”加密法,它的Session Key长度为56bits。 非对称式加密: 就是加密和解密所用法的不是同一个密钥,通常有两个密钥,称为“公钥”和“私钥”,它们两个必需配对用法,否则不能打开加密文件。 加密为系统中常常用法的功

2、能,node自带强大的加密功能Crypto,下面通过简洁的例子进行练习。 1、加密模块的引用: var crypto=require(crypto); var $=require(underscore);var DEFAULTS = encoding: input: utf8, output: hex , algorithms: bf, blowfish, aes-128-cbc ; 默认加密算法配置项: 输入数据格式为utf8,输出格式为hex, 算法用法bf,blowfish,aes-128-abc三种加密算法; 2、配置项初始化: function MixCrypto(options)

3、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、加密方法代码如下: Mi

4、xCrypto.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、解密方法代

5、码如下: 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方法,进行加密和解密的算法执行。 本文依据民少编写的算法进行编写,如有不足之处,敬请谅解。菜鸟在路上,连续前进。 以上这篇nodejs加密Crypto的实例代码就是我分享给大家的全部内容了,盼望能给大家一个参考 .

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1