2018-04-28 非对称加密 - nodejs实现
2018-04-28 本文已影响53人
26d1e4f85623
数字钱包常常有一个公钥和私钥,然后借此生成一个地址,以下就是一个简单的node 代码
var crypto = require('crypto');
// 支持的hash算法
// hash算法不可逆(即只提供了将给定值加密的方法,没给定解密方式),除非用方法暴力碰撞
// console.log(crypto.getHashes())
// 支持的加密解密算法
// 提供能力给使用者: 加密给定的数据,通过方式解密得到给定的数据
// console.log(crypto.getCiphers())
let key = 'a password ';
let content = 'hello world'
//加密
function encrypt(str,secret){
var cipher = crypto.createCipher('aes192',secret);
var enc = cipher.update(str,'utf8','hex');
enc += cipher.final('hex');
return enc;
}
//解密
function decrypt(str,secret){
var decipher = crypto.createDecipher('aes192',secret);
var dec = decipher.update(str,'hex','utf8');
dec += decipher.final('utf8');
return dec;
}
let en = encrypt(content,key)
console.log(en) // 661728d44eacc82388b6de09e06f78dc
let de = decrypt(en,key)
console.log(de) // hello world