[node.js/노드] 암호화 예제

Jongmin Shin·2021년 4월 27일
0
post-thumbnail

테스트입니다

//crypto 모듈 사용
const crypto = require('crypto'); 
const algorithm = 'aes-256-cbc';
const key = 'abcdefghizklmnopqrstlmn'; //임의의 key값 
const iv = '1234567890123456'; //16자리
const encrypt = (value) => {
    const fn = 'encrypt';
    try {
        const cipher = crypto.createCipheriv(algorithm, key, iv);
        let result = cipher.update(value, 'utf8', 'base64');
        result += cipher.final('base64');
        console.log(`encrypt result: ${result}`);
        return result;
    } catch (error) {
        throw error;
    }
};
const decrypt = (value) => {
    const fn = 'decrypt';
    try {
        const decipher = crypto.createDecipheriv(algorithm, key, iv);
        let result = decipher.update(value, 'base64', 'utf8');
        result += decipher.final('utf-8');
        console.log(`decrypt result: ${result}`);
        return result;
    } catch (error) {
        throw error;
    }
};

0개의 댓글