nodejs aes 加密

加密
function aesEncrypt(data, key) {
var algorithm = ‘aes-128-ecb’;
var clearEncoding = ‘utf8’;
var iv = “”;
var cipherEncoding = ‘hex’;
var key = key || ‘22222222222222222’;
//key = new Buffer(keyHex, ‘hex’)
try {
var cipher = crypto.createCipheriv(algorithm, key, iv);
var cipherChunks = [];
cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding));
cipherChunks.push(cipher.final(cipherEncoding));
return cipherChunks.join(’’);
} catch (e) {
return ‘’;
}
}解密:
function aesDecrypt(data, key) {
var algorithm = ‘aes-128-ecb’;
var clearEncoding = ‘utf8’;
var iv = “”;
var cipherEncoding = ‘hex’;
var key = key || ‘1111111111111111’;
//key = new Buffer(keyHex, ‘hex’)
try {
var decipher = crypto.createDecipheriv(algorithm, key, iv);
var plainChunks = [], cipherChunks = ;
for (var i = 0; i < cipherChunks.length; i++) {
plainChunks.push(decipher.update(cipherChunks, cipherEncoding, clearEncoding));
}
plainChunks.push(decipher.final(clearEncoding));
return plainChunks.join(’’);
} catch (e) {
return ‘’;
}
};