programming/react & mean
[node.js, cipher, js] AES-CBC를 이용한 한글 암호화
LEIBNIZ
2017. 3. 3. 14:08
var plainText = 'HELLO 반갑습니다.';
var check = /[ㄱ-ㅎ|ㅏ-ㅣ|가-힣]/;
/**
* 한글이 있다면 URI 인코딩을 한다.
*/
if(check.test(plainText)){
plainText = encodeURIComponent(plainText);
}
/**
* 16바이트 길이의 랜던 키와, Initialization Vector 값을 생성
*/
var key = forge.random.getBytesSync(16);
var iv = forge.random.getBytesSync(16);
var cipher = forge.cipher.createCipher('AES-CBC', key);
cipher.start({ iv: iv });
cipher.update(forge.util.createBuffer(plainText));
cipher.finish();
console.log('AES-CBC Encrypted(base64) : ' + forge.util.encode64(cipher.output.bytes()));
var decipher = forge.cipher.createDecipher('AES-CBC', key);
decipher.start({ iv: iv });
decipher.update(forge.util.createBuffer(cipher.output));
decipher.finish();
try{
result = decodeURIComponent(decipher.output);
}catch(e){
result = decipher.output;
}
console.log('AES-CBC Decrypted : ' + result);