티스토리 뷰

    
    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);



공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함