码迷,mamicode.com
首页 > Web开发 > 详细

nodejs加密解密

时间:2015-08-26 13:35:23      阅读:451      评论:0      收藏:0      [点我收藏+]

标签:

nodejs是通集成在内核中的crypto模块来完成加密解密。

常用加密解密模块化代码:

/**
 * Created by linli on 2015/8/25.
 */
var crypto = require(‘crypto‘);

//加密
exports.cipher = function(algorithm, key, buf) {
    var encrypted = "";
    var cip = crypto.createCipher(algorithm, key);
    encrypted += cip.update(buf, ‘binary‘, ‘hex‘);
    encrypted += cip.final(‘hex‘);
    return encrypted
};

//解密
exports.decipher = function(algorithm, key, encrypted) {
    var decrypted = "";
    var decipher = crypto.createDecipher(algorithm, key);
    decrypted += decipher.update(encrypted, ‘hex‘, ‘binary‘);
    decrypted += decipher.final(‘binary‘);
    return decrypted
};

此处,只针对可逆加密。

更详细内容请访问:http://blog.fens.me/nodejs-crypto/

 

nodejs加密解密

标签:

原文地址:http://www.cnblogs.com/kevalin/p/4759898.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!