前言:
上次整了crypto怎么建立加密和解密的,于是从stackoverflow抄了一段代码,关于解密加密的,关于加密算法,百度也没百度出来,外国鸟写的代码跟我的想法还是很有出入的,有了这个就可以轻松实现链接验证的功能了。
var crypto = require('crypto'),
algorithm = 'aes-256-gcm',//算法
password = '3zTvzr3p67VC61jmV54rIYu1545x4TlY',//加密规则
iv = '60iP0h6vJoEa'//偏移向量
function encrypt(text) {
var cipher = crypto.createCipheriv(algorithm, password, iv)
var encrypted = cipher.update(text, 'utf8', 'hex')//更新内容
encrypted += cipher.final('hex');//返回剩余内容结合更新内容
var tag = cipher.getAuthTag();
return {
content: encrypted,
};
}
function decrypt(encrypted) {
var decipher = crypto.createDecipheriv(algorithm, password, iv)
var dec = decipher.update(encrypted.content, 'hex', 'utf8')
return dec;
}
var hw = JSON.stringify(encrypt("hello world"));
console.log(hw);//加密字符
console.log(decrypt(JSON.parse(hw)));//解密字符
后记:
对于加密模块的实现其实还不是很熟悉,先把这个整理到我的练手项目了,另外最近博客更新的少一些,因为一直很忙,但是该记录还是要记录的。
