가끔 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");
'작은 중고 서점' 카테고리의 다른 글
정규식 웹테스트 사이트~! (0) | 2014.11.19 |
---|---|
[ASP] dictionary 스크립팅 객체 (0) | 2013.07.24 |
윈도우 프로그램추가/삭제에 문제 있을때 해결방법 (0) | 2012.08.12 |
시놀로지 DiskStation NAS 시간동기화 잘 안될때 해결방법 (0) | 2011.12.06 |
익스플로러6 이제 그만!!! IE6 no more!! (2) | 2011.02.17 |
아이폰 탈옥후 Cydia 소스(source) 오류 날때 수정방법 (4) | 2010.10.24 |
아이폰 키보드에 없는 특수문자를 입력하자 ★ ☆ ♥ ♡ → ← ♪ ♬ ※ (13) | 2010.03.09 |
아이팟터치(3.X)~ 음악 아이콘과 비디오 아이콘을 합쳐 보자~~ (17) | 2009.07.07 |
Adboe Flash 문제해결 (0) | 2009.03.19 |
현재 날짜와 시간을 자바스크립트로 만들자 (1) | 2009.03.10 |