달력

42024  이전 다음

  • 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
반응형


가끔 form 이나 쿠키로 데이터를 전송할때 base64 같은 인코딩을 해야할 경우가 있어요.


필요할때마다 찾아 쓰려니 넘 귀찮아서 포스팅으로 남겨 봅니다.


필요하신분들 유용하게 사용하세요~ ㅎㅎ


<script type="text/javascript">
    var _KeyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
    function base64_encode(input) {
        var output = '', chr1, chr2, chr3, enc1, enc2, enc3, enc4, i = 0;
        function _keyStrCharAt() {
          var ar=arguments, i, ov='';
          for (i=0; i<ar.length; i++) ov+=_KeyStr.charAt(ar[i]);
          return ov;
        }
        function _utf8_encode(string) {
          string = string.replace(/\r\n/g,'\n');
          var utftext = '', c;
          for (var n = 0; n < string.length; n++) {
            var c = string.charCodeAt(n);
            if (c < 128)
              utftext += String.fromCharCode(c);
            else if((c > 127) && (c < 2048))
              utftext += String.fromCharCode((c >> 6) | 192, (c & 63) | 128);
            else
              utftext += String.fromCharCode((c >> 12) | 224, ((c >> 6) & 63) | 128, (c & 63) | 128);
            }
          return utftext;
        }
        input = _utf8_encode(input);
        while (i < input.length) {
            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);
            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;
            if (isNaN(chr2)) enc3 = enc4 = 64;
            else if (isNaN(chr3)) enc4 = 64;
            output +=_keyStrCharAt(enc1, enc2, enc3, enc4);
        }
        return output;
    }

    function base64_decode(input) {
        function _keyStrindexOfinputcharAt(p){ return _KeyStr.indexOf(input.charAt(p)); }
        function _utf8_decode (utftext){
            var string = '', i = 0, c, c2, c3;
            while ( i < utftext.length ) {
                c = utftext.charCodeAt(i);
                if (c < 128){
                    string += String.fromCharCode(c);
                    i++;
                }else if((c > 191) && (c < 224)){
                    c2 = utftext.charCodeAt(i+1);
                    string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                    i += 2;
                }else{
                    c2 = utftext.charCodeAt(i+1);
                    c3 = utftext.charCodeAt(i+2);
                    string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                    i += 3;
                }
            }
            return string;
        }
        var output = '', chr1, chr2, chr3, enc1, enc2, enc3, enc4, i = 0;
        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
        while (i < input.length){
            enc1 = _keyStrindexOfinputcharAt(i++);
            enc2 = _keyStrindexOfinputcharAt(i++);
            enc3 = _keyStrindexOfinputcharAt(i++);
            enc4 = _keyStrindexOfinputcharAt(i++);
            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;
            output += String.fromCharCode(chr1);
            if (enc3 != 64) output += String.fromCharCode(chr2);
            if (enc4 != 64) output += String.fromCharCode(chr3);
        }
        output = _utf8_decode(output);
        return output;
    }
</script>


인코딩 : base64_encode("문자열");

디코딩 : base64_decode("66y47J6Q7Je0");








반응형
|