nodejs加密解密

nodejs是通集成在內核中的crypto模塊來完成加密解密。node

經常使用加密解密模塊化代碼:算法

/**
 * 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/ui

相關文章
相關標籤/搜索