const crypto = require('crypto');
const algorithm = 'aes-128-cbc';
const secretKey = 'abcdefghijklmnop';
const iv = '00000000000000000000000000000000';
const encrypt = (message) => {
const cipher = crypto.createCipheriv(algorithm, Buffer.from(secretKey), Buffer.from(iv, 'hex'));
const encrypted = cipher.update(message);
return Buffer.concat([encrypted, cipher.final()]).toString('hex');
};
const decrypt = (message) => {
const decipher = crypto.createDecipheriv(algorithm, Buffer.from(secretKey), Buffer.from(iv, 'hex'));
const decrypted = decipher.update(Buffer.from(message, 'hex'));
return Buffer.concat([decrypted, decipher.final()]).toString();
};
const encrypted = encrypt('Hello World!');
console.log(`encrypted: ${encrypted}`);
const decrypted = decrypt(encrypted);
console.log(`decrypted: ${decrypted}`);
'Javascript > Node.js' 카테고리의 다른 글
Node.js RSA Encryption, Decryption (0) | 2021.04.27 |
---|---|
query 결과 csv로 저장하기 (0) | 2018.07.25 |
Node.js로 카카오 플러스친구 스마트 채팅 개발하기 (2) (1) | 2018.06.05 |
Node.js로 카카오 플러스친구 스마트 채팅 개발하기 (1) (0) | 2018.05.19 |