IceWarp 이메일 서버 자세히보기

고객지원

asp에서 base64 인코딩 및 디코딩 소스

알 수 없는 사용자 2007. 1. 22. 13:54

ASP에서 메일을 주고 받을 때 메일 인코딩 방식인 base64를 인코딩하거나 디코딩하여야 하는데 기본적으로 제공되는 함수가 없어 간혹 곤란을 겪으셨을 것입니다.

아래 소스는 asp 페이지에서 자바스크립트를 사용하여 base64 인코딩, 디코딩을 하기 위한 함수입니다. 또한 아웃룩이나 아웃룩 익스프레스에서 사용하는 편지함 폴더를 인코딩하기 위한 IMAP_UTF7 함수를 포함하였습니다.
 

<script language="JavaScript">

var enc64List, dec64List;

function initBase64() {
  enc64List = new Array();
  dec64List = new Array();
  var i;
  for (i = 0; i < 26; i++) {
       enc64List[enc64List.length] = String.fromCharCode(65 + i);
  }
  for (i = 0; i < 26; i++) {
       enc64List[enc64List.length] = String.fromCharCode(97 + i);
  }
  for (i = 0; i < 10; i++) {
       enc64List[enc64List.length] = String.fromCharCode(48 + i);
  }
  enc64List[enc64List.length] = "+";
  enc64List[enc64List.length] = "/";
  for (i = 0; i < 128; i++) {
       dec64List[dec64List.length] = -1;
  }
  for (i = 0; i < 64; i++) {
       dec64List[enc64List[i].charCodeAt(0)] = i;
  }
}

function base64Encode(str) {
  var c, d, e, end = 0;
  var u, v, w, x;
  var ptr = -1;
  var input = str.split("");
  var output = "";
  while(end == 0) {
       c = (typeof input[++ptr] != "undefined") ? input[ptr].charCodeAt(0) :
           ((end = 1) ? 0 : 0);
       d = (typeof input[++ptr] != "undefined") ? input[ptr].charCodeAt(0) :
           ((end += 1) ? 0 : 0);
       e = (typeof input[++ptr] != "undefined") ? input[ptr].charCodeAt(0) :
           ((end += 1) ? 0 : 0);
       u = enc64List[c >> 2];
       v = enc64List[(0x00000003 & c) << 4 | d >> 4];
       w = enc64List[(0x0000000F & d) << 2 | e >> 6];
       x = enc64List[e & 0x0000003F];
       if (end >= 1) {x = "=";}
       if (end == 2) {w = "=";}
       if (end < 3) {output += u + v + w + x;}
  }
  var formattedOutput = "";
  var lineLength = 76;
  while (output.length > lineLength) {
    formattedOutput += output.substring(0, lineLength) + "\n";
    output = output.substring(lineLength);
  }
  formattedOutput += output;
  return formattedOutput;
}

function base64Decode(str) {
  var c=0, d=0, e=0, f=0, i=0, n=0;
  var input = str.split("");
  var output = "";
  var ptr = 0;
  do {
       f = input[ptr++].charCodeAt(0);
       i = dec64List[f];
       if ( f >= 0 && f < 128 && i != -1 ) {
           if ( n % 4 == 0 ) {
               c = i << 2;
           } else if ( n % 4 == 1 ) {
               c = c | ( i >> 4 );
               d = ( i & 0x0000000F ) << 4;
           } else if ( n % 4 == 2 ) {
               d = d | ( i >> 2 );
               e = ( i & 0x00000003 ) << 6;
           } else {
               e = e | i;
           }
           n++;
           if ( n % 4 == 0 ) {
               output += String.fromCharCode(c) +
                         String.fromCharCode(d) +
                         String.fromCharCode(e);
           }
       }
  }
  while (typeof input[ptr] != "undefined");
  output += (n % 4 == 3) ? String.fromCharCode(c) + String.fromCharCode(d) :
             ((n % 4 == 2) ? String.fromCharCode(c) : "");
  return output;
}


function IMAP_UTF7( s ){
var c, d = "";
var tag = 0;

initBase64();

for (var i=0; i < s.length; i++){
 c = s.charCodeAt(i);
 if ( c = 0x26 )
 {
  d += "&-";
 /* tag = 0; */
 }
 else if ( 0x 20 <= c && c <= 0x7e )
 {
  if (tag == 1)
  {
   d += "&-";
  }
  d += s.charAt(i);
  tag = 0;
 }
 else if (0x7f <= c)
 {
  if (tag == 0)
  {
   d += "&";
  }
  d += base64Encode(s.charAt(i));
  tag = 1;
 }


         
}

alert(d);

}
</script>

<title>aa</title>

</head>

<body>
<a href="javascript:IMAP_UTF7('abcd대한민국')">Press</a>