jascript base64编解码,好东西

来源:互联网 发布:数控车床编程语言 编辑:程序博客网 时间:2024/05/16 09:21
  1. var Base64 = {  
  2.     // 转码表 
  3.     table : [  
  4.             'A''B''C''D''E''F''G''H',  
  5.             'I''J''K''L''M''N''O' ,'P',  
  6.             'Q''R''S''T''U''V''W''X',  
  7.             'Y''Z''a''b''c''d''e''f',  
  8.             'g''h''i''j''k''l''m''n',  
  9.             'o''p''q''r''s''t''u''v',  
  10.             'w''x''y''z''0''1''2''3',  
  11.             '4''5''6''7''8''9''+''/' 
  12.     ],  
  13.     UTF16ToUTF8 : function(str) {  
  14.         var res = [], len = str.length;  
  15.         for (var i = 0; i < len; i++) {  
  16.             var code = str.charCodeAt(i);  
  17.             if (code > 0x0000 && code <= 0x007F) {  
  18.                 // 单字节,这里并不考虑0x0000,因为它是空字节 
  19.                 // U+00000000 – U+0000007F  0xxxxxxx 
  20.                 res.push(str.charAt(i));  
  21.             } else if (code >= 0x0080 && code <= 0x07FF) {  
  22.                 // 双字节 
  23.                 // U+00000080 – U+000007FF  110xxxxx 10xxxxxx 
  24.                 // 110xxxxx 
  25.                 var byte1 = 0xC0 | ((code >> 6) & 0x1F);  
  26.                 // 10xxxxxx 
  27.                 var byte2 = 0x80 | (code & 0x3F);  
  28.                 res.push(  
  29.                     String.fromCharCode(byte1),   
  30.                     String.fromCharCode(byte2)  
  31.                 );  
  32.             } else if (code >= 0x0800 && code <= 0xFFFF) {  
  33.                 // 三字节 
  34.                 // U+00000800 – U+0000FFFF  1110xxxx 10xxxxxx 10xxxxxx 
  35.                 // 1110xxxx 
  36.                 var byte1 = 0xE0 | ((code >> 12) & 0x0F);  
  37.                 // 10xxxxxx 
  38.                 var byte2 = 0x80 | ((code >> 6) & 0x3F);  
  39.                 // 10xxxxxx 
  40.                 var byte3 = 0x80 | (code & 0x3F);  
  41.                 res.push(  
  42.                     String.fromCharCode(byte1),   
  43.                     String.fromCharCode(byte2),   
  44.                     String.fromCharCode(byte3)  
  45.                 );  
  46.             } else if (code >= 0x00010000 && code <= 0x001FFFFF) {  
  47.                 // 四字节 
  48.                 // U+00010000 – U+001FFFFF  11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 
  49.             } else if (code >= 0x00200000 && code <= 0x03FFFFFF) {  
  50.                 // 五字节 
  51.                 // U+00200000 – U+03FFFFFF  111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 
  52.             } else /** if (code >= 0x04000000 && code <= 0x7FFFFFFF)*/ {  
  53.                 // 六字节 
  54.                 // U+04000000 – U+7FFFFFFF  1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 
  55.             }  
  56.         }  
  57.  
  58.         return res.join('');  
  59.     },  
  60.     UTF8ToUTF16 : function(str) {  
  61.         var res = [], len = str.length;  
  62.         var i = 0;  
  63.         for (var i = 0; i < len; i++) {  
  64.             var code = str.charCodeAt(i);  
  65.             // 对第一个字节进行判断 
  66.             if (((code >> 7) & 0xFF) == 0x0) {  
  67.                 // 单字节 
  68.                 // 0xxxxxxx 
  69.                 res.push(str.charAt(i));  
  70.             } else if (((code >> 5) & 0xFF) == 0x6) {  
  71.                 // 双字节 
  72.                 // 110xxxxx 10xxxxxx 
  73.                 var code2 = str.charCodeAt(++i);  
  74.                 var byte1 = (code & 0x1F) << 6;  
  75.                 var byte2 = code2 & 0x3F;  
  76.                 var utf16 = byte1 | byte2;  
  77.                 res.push(Sting.fromCharCode(utf16));  
  78.             } else if (((code >> 4) & 0xFF) == 0xE) {  
  79.                 // 三字节 
  80.                 // 1110xxxx 10xxxxxx 10xxxxxx 
  81.                 var code2 = str.charCodeAt(++i);  
  82.                 var code3 = str.charCodeAt(++i);  
  83.                 var byte1 = (code << 4) | ((code2 >> 2) & 0x0F);  
  84.                 var byte2 = ((code2 & 0x03) << 6) | (code3 & 0x3F);  
  85.                 utf16 = ((byte1 & 0x00FF) << 8) | byte2  
  86.                 res.push(String.fromCharCode(utf16));  
  87.             } else if (((code >> 3) & 0xFF) == 0x1E) {  
  88.                 // 四字节 
  89.                 // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 
  90.             } else if (((code >> 2) & 0xFF) == 0x3E) {  
  91.                 // 五字节 
  92.                 // 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 
  93.             } else /** if (((code >> 1) & 0xFF) == 0x7E)*/ {  
  94.                 // 六字节 
  95.                 // 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 
  96.             }  
  97.         }  
  98.  
  99.         return res.join('');  
  100.     },  
  101.     encode : function(str) {  
  102.         if (!str) {  
  103.             return '';  
  104.         }  
  105.         var utf8    = this.UTF16ToUTF8(str); // 转成UTF8 
  106.         var i = 0; // 遍历索引 
  107.         var len = utf8.length;  
  108.         var res = [];  
  109.         while (i < len) {  
  110.             var c1 = utf8.charCodeAt(i++) & 0xFF;  
  111.             res.push(this.table[c1 >> 2]);  
  112.             // 需要补2个= 
  113.             if (i == len) {  
  114.                 res.push(this.table[(c1 & 0x3) << 4]);  
  115.                 res.push('==');  
  116.                 break;  
  117.             }  
  118.             var c2 = utf8.charCodeAt(i++);  
  119.             // 需要补1个= 
  120.             if (i == len) {  
  121.                 res.push(this.table[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]);  
  122.                 res.push(this.table[(c2 & 0x0F) << 2]);  
  123.                 res.push('=');  
  124.                 break;  
  125.             }  
  126.             var c3 = utf8.charCodeAt(i++);  
  127.             res.push(this.table[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]);  
  128.             res.push(this.table[((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6)]);  
  129.             res.push(this.table[c3 & 0x3F]);  
  130.         }  
  131.  
  132.         return res.join('');  
  133.     },  
  134.     decode : function(str) {  
  135.         if (!str) {  
  136.             return '';  
  137.         }  
  138.  
  139.         var len = str.length;  
  140.         var i   = 0;  
  141.         var res = [];  
  142.  
  143.         while (i < len) {  
  144.             code1 = this.table.indexOf(str.charAt(i++));  
  145.             code2 = this.table.indexOf(str.charAt(i++));  
  146.             code3 = this.table.indexOf(str.charAt(i++));  
  147.             code4 = this.table.indexOf(str.charAt(i++));  
  148.  
  149.             c1 = (code1 << 2) | (code2 >> 4);  
  150.             c2 = ((code2 & 0xF) << 4) | (code3 >> 2);  
  151.             c3 = ((code3 & 0x3) << 6) | code4;  
  152.  
  153.             res.push(String.fromCharCode(c1));  
  154.  
  155.             if (code3 != 64) {  
  156.                 res.push(String.fromCharCode(c2));  
  157.             }  
  158.             if (code4 != 64) {  
  159.                 res.push(String.fromCharCode(c3));  
  160.             }  
  161.  
  162.         }  
  163.  
  164.         return this.UTF8ToUTF16(res.join(''));  
  165.     }  
  166. };  
  167.  
  168. console.group('Test Base64: ');  
  169. var b64 = Base64.encode('Hello, oschina!又是一年春来到~');  
  170. console.log(b64);  
  171. console.log(Base64.decode(b64));  
  172. console.groupEnd();  
0 0
原创粉丝点击