本文转自https://blog.csdn.net/sinat_35670989/article/details/78224214
'use strict'
//crypto(kri:pto)意为加密
const cons = require('crypto');
//声明为const 表示该变量不可修改
//Hash算法
var hash = cons.createHash('md5')//'sha1', 'md5', 'sha256', 'sha512'等
hash.update("hello world")
console.log(hash.digest('hex'));
//Hmac算法,需要一个密钥
var hmac = cons.createHmac('sha1','secret-key');
hmac.update('hello world');
console.log(hmac.digest('hex'));
//AES是一种常用的对称加密算法,加解密都用同一个密钥。crypto模块提供了AES支持,但是需要自己封装好函数,便于使用:
//加密 cipher意为暗号
function aesCrypto(data,key){
//创建一个加了秘钥的暗号
const cipher = cons.createCipher('aes192',key);
//将暗号转换成十六进制
var aes = cipher.update(data,'utf-8','hex');
aes+=cipher.final('hex');
return aes;
}
//解密
function aesDecrypto(data,key){
const dcipher = cons.createDecipher('aes192',key);
var daes = dcipher.update(data,'hex','utf-8');
daes+=dcipher.final('utf-8');
return daes;
}
var data = '这是一个秘密,需要加密';
var key = 'passworld';
var encrypted = aesCrypto(data, key);
var decrypted = aesDecrypto(encrypted, key);
console.log('加密后: ' + encrypted);
console.log('解密后: ' + decrypted);
//注意到AES有很多不同的算法,如aes192,aes-128-ecb,aes-256-cbc等,AES除了密钥外还可以指定IV(Initial Vector),
//不同的系统只要IV不同,用相同的密钥加密相同的数据得到的加密结果也是不同的。
//加密结果通常有两种表示方法:hex和base64,这些功能Nodejs全部都支持,
//但是在应用中要注意,如果加解密双方一方用Nodejs,另一方用Java、PHP等其它语言,需要仔细测试。
//如果无法正确解密,要确认双方是否遵循同样的AES算法,字符串密钥和IV是否相同,加密后的数据是否统一为hex或base64格式。