鍍金池/ 問答/HTML/ node中怎么進行可逆加密

node中怎么進行可逆加密

md5的加密是不可逆的,無法還原密文。需要一種可逆加密,加密的時候提供一個私鑰key,解密的時候再用這個私鑰key還原密文,node中如何進行這種可逆加密?

回答
編輯回答
柒喵

var crypto = require('crypto');

//加密
export function cipher (buf) {
    var encrypted = "";
    var cip = crypto.createCipher('rc4', 'pkdpkq'); //第一個為加密方式 第二個為密匙
    encrypted += cip.update(buf, 'hex', 'hex');
    encrypted += cip.final('hex');
    return encrypted
};

//解密
export function decipher(encrypted) {
    var decrypted = "";
    var decipher = crypto.createDecipher('rc4', 'pkdpkq');  //第一個為加密方式 第二個為密匙
    decrypted += decipher.update(encrypted, 'hex', 'hex');
    decrypted += decipher.final('hex');
    return decrypted
};
2017年10月30日 12:52