1. 程式人生 > >nodejs中的加密模組 crypto 模組

nodejs中的加密模組 crypto 模組

直接上程式碼吧:

#!/usr/bin/env node

var crypto = require('crypto');

// use secret to encrypt string
function encrypt(str, secret) {
    var cipher = crypto.createCipher('aes192', secret);
    var enc = cipher.update(str, 'utf8', 'hex');
    enc += cipher.final('hex');
    console.log("enc is: "+enc);
    return enc;
}

//use secret to decrypt string
function decrypt(str, secret) {
    var decipher = crypto.createDecipher('aes192', secret);
    var dec = decipher.update(str, 'hex', 'utf8');
    dec += decipher.final('utf8');
    console.log("dec is: "+dec);
    return dec;
}


encrypt("leekwen",'987654321');
decrypt("33e706a935b3e61adaf83506897a3f28",'987654321');