如下所示:
'use strict';
const crypto = require('crypto');
var AES_conf = {
key: getSecretKey(), //密钥
iv: '1012132405963708', //偏移向量
padding: 'PKCS7Padding' //补全值
}
function getSecretKey(){
return "abcdabcdabcdabcd";
}
function encryption(data) {
let key = AES_conf.key;
let iv = AES_conf.iv;
// let padding = AES_conf.padding;
var cipherChunks = [];
var cipher = crypto.createCipheriv('aes-128-cbc', key, iv);
cipher.setAutoPadding(true);
cipherChunks.push(cipher.update(data, 'utf8', 'base64'));
cipherChunks.push(cipher.final('base64'));
return cipherChunks.join('');
}
function decryption(data){
let key = AES_conf.key;
let iv = AES_conf.iv;
// let padding = AES_conf.padding;
var cipherChunks = [];
var decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
decipher.setAutoPadding(true);
cipherChunks.push(decipher.update(data, 'base64', 'utf8'));
cipherChunks.push(decipher.final('utf8'));
return cipherChunks.join('');
}
console.log(encryption('aaaaa4'));
console.log(decryption('VuoXtyUolFyPrK50JnNUdw=='));
以上这篇nodejs aes 加解密实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。



