JS实现base64编码与解码

  1 var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  2 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);
  3 /**
  4  * base64编码
  5  * @param {Object} str
  6  */
  7 function base64encode(str){
  8     var out, i, len;
  9     var c1, c2, c3;
 10     len = str.length;
 11     i = 0;
 12     out = "";
 13     while (i < len) {
 14         c1 = str.charCodeAt(i++) & 0xff;
 15         if (i == len) {
 16             out += base64EncodeChars.charAt(c1 >> 2);
 17             out += base64EncodeChars.charAt((c1 & 0x3) << 4);
 18             out += "==";
 19             break;
 20         }
 21         c2 = str.charCodeAt(i++);
 22         if (i == len) {
 23             out += base64EncodeChars.charAt(c1 >> 2);
 24             out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
 25             out += base64EncodeChars.charAt((c2 & 0xF) << 2);
 26             out += "=";
 27             break;
 28         }
 29         c3 = str.charCodeAt(i++);
 30         out += base64EncodeChars.charAt(c1 >> 2);
 31         out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
 32         out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
 33         out += base64EncodeChars.charAt(c3 & 0x3F);
 34     }
 35     return out;
 36 }
 37 /**
 38  * base64解码
 39  * @param {Object} str
 40  */
 41 function base64decode(str){
 42     var c1, c2, c3, c4;
 43     var i, len, out;
 44     len = str.length;
 45     i = 0;
 46     out = "";
 47     while (i < len) {
 48         /* c1 */
 49         do {
 50             c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
 51         }
 52         while (i < len && c1 == -1);
 53         if (c1 == -1) 
 54             break;
 55         /* c2 */
 56         do {
 57             c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
 58         }
 59         while (i < len && c2 == -1);
 60         if (c2 == -1) 
 61             break;
 62         out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
 63         /* c3 */
 64         do {
 65             c3 = str.charCodeAt(i++) & 0xff;
 66             if (c3 == 61) 
 67                 return out;
 68             c3 = base64DecodeChars[c3];
 69         }
 70         while (i < len && c3 == -1);
 71         if (c3 == -1) 
 72             break;
 73         out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
 74         /* c4 */
 75         do {
 76             c4 = str.charCodeAt(i++) & 0xff;
 77             if (c4 == 61) 
 78                 return out;
 79             c4 = base64DecodeChars[c4];
 80         }
 81         while (i < len && c4 == -1);
 82         if (c4 == -1) 
 83             break;
 84         out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
 85     }
 86     return out;
 87 }
 88 /**
 89  * utf16转utf8
 90  * @param {Object} str
 91  */
 92 function utf16to8(str){
 93     var out, i, len, c;
 94     out = "";
 95     len = str.length;
 96     for (i = 0; i < len; i++) {
 97         c = str.charCodeAt(i);
 98         if ((c >= 0x0001) && (c <= 0x007F)) {
 99             out += str.charAt(i);
100         }
101         else 
102             if (c > 0x07FF) {
103                 out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
104                 out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
105                 out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
106             }
107             else {
108                 out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
109                 out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
110             }
111     }
112     return out;
113 }
114 /**
115  * utf8转utf16
116  * @param {Object} str
117  */
118 function utf8to16(str){
119     var out, i, len, c;
120     var char2, char3;
121     out = "";
122     len = str.length;
123     i = 0;
124     while (i < len) {
125         c = str.charCodeAt(i++);
126         switch (c >> 4) {
127             case 0:
128             case 1:
129             case 2:
130             case 3:
131             case 4:
132             case 5:
133             case 6:
134             case 7:
135                 // 0xxxxxxx
136                 out += str.charAt(i - 1);
137                 break;
138             case 12:
139             case 13:
140                 // 110x xxxx 10xx xxxx
141                 char2 = str.charCodeAt(i++);
142                 out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
143                 break;
144             case 14:
145                 // 1110 xxxx10xx xxxx10xx xxxx
146                 char2 = str.charCodeAt(i++);
147                 char3 = str.charCodeAt(i++);
148                 out += String.fromCharCode(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
149                 break;
150         }
151     }
152     return out;
153 }
154 //demo
155 //function doit(){
156 //    var f = document.f;
157 //    f.output.value = base64encode(utf16to8(f.source.value));
158 //    f.decode.value = utf8to16(base64decode(f.output.value));
159 //}