标签:node const encrypted hex 解密 crypto cipher decipher 加密
加密
//cipher加密算法
function cipher(str){
try{
const crypto = require('crypto');
/**
* --创建 Cipher 实例。 不能使用 new 关键字直接地创建 Cipher 对象
* --crypto.createCipher,@param1 算法,@param2 密文,@param3 向量--可为""或省略
*/
const cipher = crypto.createCipher('aes128', '冷月心');
/**
* update方法
* @param1 加密的数据
* @param2 数据编码格式,一般为utf8
* @param3 输出格式,一般为 'base64' 或者 'hex',缺省返回Buffer
*/
let encrypted = cipher.update(str, 'utf8', 'hex');
/**
* final方法,返回加密后结果
* @param 返回值的字符编码 ,一般为 'base64' 或者 'hex',缺省返回Buffer
* -- 一旦 cipher.final() 方法被调用, Cipher 对象就不能再用于加密数据。
* -- 如果试图再次调用 cipher.final(),将会抛出一个错误。
*/
encrypted += cipher.final('hex');
return encrypted;
}catch(e){
console.log('加密失败');
return e.message ;
}
}
const result=cipher('清风明月晓星尘') ;
console.log(result)//fa0bb8ac7f40bfdc925bd24ae2aacc04ac89740c0bd6dfe51213d9e66a8e0360
解密
function decipher(encrypted){
try{
const crypto = require('crypto');
const decipher = crypto.createDecipher('aes128', '冷月心')
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}catch(e){
console.log('解密失败');
return e.message;
}
}
const result=decipher('fa0bb8ac7f40bfdc925bd24ae2aacc04ac89740c0bd6dfe51213d9e66a8e0360');
console.log(result)//清风明月晓星尘
标签:node,const,encrypted,hex,解密,crypto,cipher,decipher,加密 来源: https://blog.csdn.net/qq_42813491/article/details/101421616
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。