http://www.blueidea.com/bbs/NewsDetail.asp?id=454714
八、Ecode解码器 [原创]

解决方案 »

  1.   

    里面的base64解码方法有点不对~
    例如
    ?GB2312?B?vfHI1dKqzsUgICC12jE5Msba?=
    这是常见的邮件里的编码
    正常解码为“今日要闻 第192期”
    用以下办法却不行(谁能解决,请回帖~:)谢谢!)
    <HTML>
    <HEAD>
    <TITLE>Base64</TITLE>
    <script language=javascript>
    var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var base64DecodeChars = new Array(
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
        -1,  0,  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, -1, -1, -1, -1, -1,
        -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);function base64encode(str) {
        var out, i, len;
        var c1, c2, c3;    len = str.length;
        i = 0;
        out = "";
        while(i < len) {
    c1 = str.charCodeAt(i++) & 0xff;
    if(i == len)
    {
        out += base64EncodeChars.charAt(c1 >> 2);
        out += base64EncodeChars.charAt((c1 & 0x3) << 4);
        out += "==";
        break;
    }
    c2 = str.charCodeAt(i++);
    if(i == len)
    {
        out += base64EncodeChars.charAt(c1 >> 2);
        out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
        out += base64EncodeChars.charAt((c2 & 0xF) << 2);
        out += "=";
        break;
    }
    c3 = str.charCodeAt(i++);
    out += base64EncodeChars.charAt(c1 >> 2);
    out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
    out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
    out += base64EncodeChars.charAt(c3 & 0x3F);
        }
        return out;
    }function base64decode(str) {
        var c1, c2, c3, c4;
        var i, len, out;    len = str.length;
        i = 0;
        out = "";
        while(i < len) {
    /* c1 */
    do {
        c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
    } while(i < len && c1 == -1);
    if(c1 == -1)
        break; /* c2 */
    do {
        c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
    } while(i < len && c2 == -1);
    if(c2 == -1)
        break; out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4)); /* c3 */
    do {
        c3 = str.charCodeAt(i++) & 0xff;
        if(c3 == 61)
    return out;
        c3 = base64DecodeChars[c3];
    } while(i < len && c3 == -1);
    if(c3 == -1)
        break; out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2)); /* c4 */
    do {
        c4 = str.charCodeAt(i++) & 0xff;
        if(c4 == 61)
    return out;
        c4 = base64DecodeChars[c4];
    } while(i < len && c4 == -1);
    if(c4 == -1)
        break;
    out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
        }
        return out;
    }function utf16to8(str) {
        var out, i, len, c;    out = "";
        len = str.length;
        for(i = 0; i < len; i++) {
    c = str.charCodeAt(i);
    if ((c >= 0x0001) && (c <= 0x007F)) {
        out += str.charAt(i);
    } else if (c > 0x07FF) {
        out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
        out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));
        out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
    } else {
        out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));
        out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
    }
        }
        return out;
    }function utf8to16(str) {
        var out, i, len, c;
        var char2, char3;    out = "";
        len = str.length;
        i = 0;
        while(i < len) {
    c = str.charCodeAt(i++);
    switch(c >> 4)

      case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
        // 0xxxxxxx
        out += str.charAt(i-1);
        break;
      case 12: case 13:
        // 110x xxxx   10xx xxxx
        char2 = str.charCodeAt(i++);
        out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
        break;
      case 14:
        // 1110 xxxx  10xx xxxx  10xx xxxx
        char2 = str.charCodeAt(i++);
        char3 = str.charCodeAt(i++);
        out += String.fromCharCode(((c & 0x0F) << 12) |
       ((char2 & 0x3F) << 6) |
       ((char3 & 0x3F) << 0));
        break;
    }
        }    return out;
    }
    function doit() {
        var f = document.f
        f.output.value = base64encode(utf16to8(f.source.value))
        f.decode.value = utf8to16(base64decode(f.output.value))
    }
    </script>
    </HEAD>
    <BODY>
    <H1>Base64</H1>
    <FORM NAME="f">
    原码<BR>
    <TEXTAREA NAME="source" ROWS=4 COLS=60 WRAP="soft"></TEXTAREA><BR><BR>
    Base64 encode<BR>
    <TEXTAREA NAME="output" ROWS=4 COLS=60 WRAP="soft"></TEXTAREA><BR><BR>
    Base64 decode<BR>
    <TEXTAREA NAME="decode" ROWS=4 COLS=60 WRAP="soft"></TEXTAREA><BR><BR>
    <INPUT TYPE=BUTTON VALUE="转换" ONCLICK="doit()">
    </FORM>
    </BODY>
      

  2.   

    <html>
       <head>
          <title>base64 Encoding/Decoding</title>
       </head>   <script type="text/javascript"><!--   var keyStr = "ABCDEFGHIJKLMNOP" +
                    "QRSTUVWXYZabcdef" +
                    "ghijklmnopqrstuv" +
                    "wxyz0123456789+/" +
                    "=";   function encode64(input) {
          var output = "";
          var chr1, chr2, chr3 = "";
          var enc1, enc2, enc3, enc4 = "";
          var i = 0;      do {
             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 = output + 
                keyStr.charAt(enc1) + 
                keyStr.charAt(enc2) + 
                keyStr.charAt(enc3) + 
                keyStr.charAt(enc4);
             chr1 = chr2 = chr3 = "";
             enc1 = enc2 = enc3 = enc4 = "";
          } while (i < input.length);      return output;
       }   function decode64(input) {
          var output = "";
          var chr1, chr2, chr3 = "";
          var enc1, enc2, enc3, enc4 = "";
          var i = 0;      // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
          var base64test = /[^A-Za-z0-9\+\/\=]/g;
          if (base64test.exec(input)) {
             alert("There were invalid base64 characters in the input text.\n" +
                   "Valid base64 characters are A-Z, a-z, 0-9, '+', '/', and '='\n" +
                   "Expect errors in decoding.");
          }
          input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");      do {
             enc1 = keyStr.indexOf(input.charAt(i++));
             enc2 = keyStr.indexOf(input.charAt(i++));
             enc3 = keyStr.indexOf(input.charAt(i++));
             enc4 = keyStr.indexOf(input.charAt(i++));         chr1 = (enc1 << 2) | (enc2 >> 4);
             chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
             chr3 = ((enc3 & 3) << 6) | enc4;         output = output + String.fromCharCode(chr1);         if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
             }
             if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
             }         chr1 = chr2 = chr3 = "";
             enc1 = enc2 = enc3 = enc4 = "";      } while (i < input.length);      return output;
       }   //--></script>   <body>      <form name="base64Form">         Type in the message you want to encode in base64, or paste<br>
             base64 encoded text into the text field, select Encode or Decode, <br>
             and click the button!<br>         <textarea name="theText" cols="40" rows="6"></textarea><br>         <input type="button" name="encode" value="Encode to base64"
                onClick="document.base64Form.theText.value=encode64(document.base64Form.theText.value);">
             <input type="button" name="decode" value="Decode from base64" 
                onClick="document.base64Form.theText.value=decode64(document.base64Form.theText.value);">      </form>   </body>
    </html>
      

  3.   

    刚测试了一下,有BUG,对中文不行.
    不过解决了.
    先用escape()对中文进行编码.然后再进行base64编码.
    解码时,再加入unescape()对中文进行解码.<html>
       <head>
          <title>base64 Encoding/Decoding</title>
       </head>   <script type="text/javascript"><!--   var keyStr = "ABCDEFGHIJKLMNOP" +
                    "QRSTUVWXYZabcdef" +
                    "ghijklmnopqrstuv" +
                    "wxyz0123456789+/" +
                    "=";   function encode64(input) {
          input = escape(input);
          var output = "";
          var chr1, chr2, chr3 = "";
          var enc1, enc2, enc3, enc4 = "";
          var i = 0;      do {
             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 = output + 
                keyStr.charAt(enc1) + 
                keyStr.charAt(enc2) + 
                keyStr.charAt(enc3) + 
                keyStr.charAt(enc4);
             chr1 = chr2 = chr3 = "";
             enc1 = enc2 = enc3 = enc4 = "";
          } while (i < input.length);      return output;
       }   function decode64(input) {
          var output = "";
          var chr1, chr2, chr3 = "";
          var enc1, enc2, enc3, enc4 = "";
          var i = 0;      // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
          var base64test = /[^A-Za-z0-9\+\/\=]/g;
          if (base64test.exec(input)) {
             alert("There were invalid base64 characters in the input text.\n" +
                   "Valid base64 characters are A-Z, a-z, 0-9, '+', '/', and '='\n" +
                   "Expect errors in decoding.");
          }
          input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");      do {
             enc1 = keyStr.indexOf(input.charAt(i++));
             enc2 = keyStr.indexOf(input.charAt(i++));
             enc3 = keyStr.indexOf(input.charAt(i++));
             enc4 = keyStr.indexOf(input.charAt(i++));         chr1 = (enc1 << 2) | (enc2 >> 4);
             chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
             chr3 = ((enc3 & 3) << 6) | enc4;         output = output + String.fromCharCode(chr1);         if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
             }
             if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
             }         chr1 = chr2 = chr3 = "";
             enc1 = enc2 = enc3 = enc4 = "";      } while (i < input.length);      return unescape(output);
       }   //--></script>   <body>      <form name="base64Form">         Type in the message you want to encode in base64, or paste<br>
             base64 encoded text into the text field, select Encode or Decode, <br>
             and click the button!<br>         <textarea name="theText" cols="40" rows="6"></textarea><br>         <input type="button" name="encode" value="Encode to base64"
                onClick="document.base64Form.theText.value=encode64(document.base64Form.theText.value);">
             <input type="button" name="decode" value="Decode from base64" 
                onClick="document.base64Form.theText.value=decode64(document.base64Form.theText.value);">      </form>   </body>
    </html>
      

  4.   

    不用escape()对中文也有效~!我载想为什么同是BASE64编码为什么和邮件的编码结果不一样~!奇怪了`
      

  5.   

    vfHI1dKqzsUgICC12jE5Msba
    等于base64("今日要闻 第192期"):(
      

  6.   

    Base64是一种通用的方法:把3个Byte的数据用4个Byte表示,这样,这4个Byte中,实际用到的都只有前面6个bit,这样就不存在只能传输7个bit的字符问题了;QP方式:把一个8bit的字符用两个16进制数值表示,然后在前面加“Κ”。所以我们有时在收到的邮件中会看到经过QP编码后的文件内容为:ΚB3ΚC2ΚBFΚA1ΚC7ΚE5ΚA3ΚACΚC4ΚFAΚBAΚC3ΚA3ΚA1等。
      最早处理二进制数据的编码为Uuen code,是Unix系统的UU编码方式,其解码为Undecode,它们作为Unix系统主机之间的拷贝协议UUCP的一部分开发出来。
      

  7.   

    不一样是因为一个是gb一个是unicode。
    所以js不能作出真正的base64解码,因为js天生只有unicode。
      

  8.   

    <script language=vbscript>
    lastcode=0
    function uchr(v)
    if v>160 then
    if lastcode <> 0 then
    uchr = chr(lastcode*256+v)
    lastcode=0
    exit function
    else
    lastcode=v
    uchr=""
    end if
     exit function
    end if
    uchr = chr(v)
    end function
    </script>
    <script language="javascript">
    function base64_decode(str){
    var pos=0;
    var v="";
    var reservedVal=0;
    while(pos<str.length){
    var c1,c2,c3,c4;
    var s1,s2,s3,s4;
    var ps1,ps2,ps3,ps4;
    s1=s2=s3=s4=0;
    c1=c2=c3=c4="";
    c1 = str.charAt(pos);
    ps = 0
    if(c1=="\n"){
    pos++;
    continue;
    }
    if(c1!=""){
    ps1 = getPos(c1); //得到字符在base64编码表中的索引
    if(ps1==-1){
    pos++;
    return;
    }
    s1 = ps1 << 18;
    }
    c2 = str.charAt(pos+1);
    if(c2!=""){
    ps2 = getPos(c2); //得到字符在base64编码表中的索引
    if(ps2==-1)ps2=0;
    s2 = ps2 << 12;
    }
    c3 = str.charAt(pos+2);
    if(c3!=""){
    ps3 = getPos(c3); //得到字符在base64编码表中的索引
    if(ps3==-1)ps3=0;
    s3 = ps3 << 6;
    }
    c4 = str.charAt(pos+3);
    if(c4!=""){
    ps4 = getPos(c4); //得到字符在base64编码表中的索引
    if(ps4==-1)ps4=0;
    s4 = ps4;
    }
    //i++;
    newv = s1 | s2 | s3 | s4;
    var ds1 = newv >> 16;
    v+=uchr(ds1);
    var ds2 = newv >> 8 & 0x0000FF;
    v+=uchr(ds2);
    var ds3 = newv & 0x0000FF;
    v+=uchr(ds3);
    pos+=4;
    }
    return v;
    }function getPos(c){
    var base64_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    for(var i=0;i<base64_tbl.length;i++)
    if(base64_tbl.substr(i,1)==c)return i;
    return -1;
    }alert(base64_decode("vfHI1dKqzsUgICC12jE5Msba"))
    </script>
      

  9.   

    <script language=vbscript>
    lastcode=0
    function uchr(v)
    if v>160 then
    if lastcode <> 0 then
    uchr = chr(lastcode*256+v)
    lastcode=0
    exit function
    else
    lastcode=v
    uchr=""
    end if
     exit function
    end if
    uchr = chr(v)
    end function
    </script>
    <script language="javascript">
    function base64_decode(str){
    var pos=0;
    var v="";
    var reservedVal=0;
    while(pos<str.length){
    var c1,c2,c3,c4;
    var s1,s2,s3,s4;
    var ps1,ps2,ps3,ps4;
    s1=s2=s3=s4=0;
    c1=c2=c3=c4="";
    c1 = str.charAt(pos);
    ps = 0
    if(c1=="\n"){
    pos++;
    continue;
    }
    if(c1!=""){
    ps1 = getPos(c1); //得到字符在base64编码表中的索引
    if(ps1==-1){
    pos++;
    return;
    }
    s1 = ps1 << 18;
    }
    c2 = str.charAt(pos+1);
    if(c2!=""){
    ps2 = getPos(c2); //得到字符在base64编码表中的索引
    if(ps2==-1)ps2=0;
    s2 = ps2 << 12;
    }
    c3 = str.charAt(pos+2);
    if(c3!=""){
    ps3 = getPos(c3); //得到字符在base64编码表中的索引
    if(ps3==-1)ps3=0;
    s3 = ps3 << 6;
    }
    c4 = str.charAt(pos+3);
    if(c4!=""){
    ps4 = getPos(c4); //得到字符在base64编码表中的索引
    if(ps4==-1)ps4=0;
    s4 = ps4;
    }
    //i++;
    newv = s1 | s2 | s3 | s4;
    var ds1 = newv >> 16;
    v+=uchr(ds1);
    var ds2 = newv >> 8 & 0x0000FF;
    v+=uchr(ds2);
    var ds3 = newv & 0x0000FF;
    v+=uchr(ds3);
    pos+=4;
    }
    return v;
    }function getPos(c){
    var base64_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    for(var i=0;i<base64_tbl.length;i++)
    if(base64_tbl.substr(i,1)==c)return i;
    return -1;
    }alert(base64_decode("vfHI1dKqzsUgICC12jE5Msba"))
    </script>
      

  10.   

    这是我以前写的代码, 那时我学JS没有太深, 只有这种代码精度了
    还用了vbs, 不过应该可以凑活用了
      

  11.   

    谢谢~~军仔、海曦、灰豆宝宝。net、我佛山人。
     揭贴了~
    如果是用Q方式编码的邮件如何解决~~?