'use strict';
const crypto = require('crypto');
/**
* @util 加密、解密工具类
*/
class CryptoUtil {
/**
* 解密
* @param dataStr {string}
* @param key {string}
* @param iv {string}
* @return {string}
*/
static Decrypt(dataStr, key, iv) {
let cipherChunks = [];
let decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
decipher.setAutoPadding(true);
cipherChunks.push(decipher.update(dataStr, 'base64', 'utf8'));
cipherChunks.push(decipher.final('utf8'));
return cipherChunks.join('');
}
/**
* 加密
* @param dataStr {string}
* @param key {string}
* @param iv {string}
* @return {string}
*/
static Encrypt(dataStr, key, iv) {
let cipherChunks = [];
let cipher = crypto.createCipheriv('aes-128-cbc', key, iv);
cipher.setAutoPadding(true);
cipherChunks.push(cipher.update(dataStr, 'utf8', 'base64'));
cipherChunks.push(cipher.final('base64'));
return cipherChunks.join('');
}
}
module.exports = CryptoUtil;
最后
以上就是帅气哈密瓜最近收集整理的关于nodejs AES 对称加密解密的全部内容,更多相关nodejs内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复