nodejs AES対称暗号解読
6355 ワード
'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;