Base64.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * Created by Administrator on 2017/6/15.
  3. */
  4. var base64DecodeChars = new Array(
  5. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  6. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  7. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
  8. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
  9. 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  10. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
  11. 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  12. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
  13. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  14. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  15. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  16. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  17. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  18. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  19. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  20. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64);
  21. function base64encode(str) {
  22. var out, i, len;
  23. var c1, c2, c3;
  24. len = str.length;
  25. i = 0;
  26. out = "";
  27. while(i < len) {
  28. c1 = str.charCodeAt(i++) & 0xff;
  29. if(i == len)
  30. {
  31. out += base64EncodeChars.charAt(c1 >> 2);
  32. out += base64EncodeChars.charAt((c1 & 0x3) << 4);
  33. out += "==";
  34. break;
  35. }
  36. c2 = str.charCodeAt(i++);
  37. if(i == len)
  38. {
  39. out += base64EncodeChars.charAt(c1 >> 2);
  40. out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
  41. out += base64EncodeChars.charAt((c2 & 0xF) << 2);
  42. out += "=";
  43. break;
  44. }
  45. c3 = str.charCodeAt(i++);
  46. out += base64EncodeChars.charAt(c1 >> 2);
  47. out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
  48. out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
  49. out += base64EncodeChars.charAt(c3 & 0x3F);
  50. }
  51. return out;
  52. }
  53. function base64decode(str) {
  54. var c1, c2, c3, c4;
  55. var i, len, out;
  56. len = str.length;
  57. i = 0;
  58. out = "";
  59. while(i < len) {
  60. /* c1 */
  61. do {
  62. c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
  63. } while(i < len && c1 == -1);
  64. if(c1 == -1)
  65. break;
  66. /* c2 */
  67. do {
  68. c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
  69. } while(i < len && c2 == -1);
  70. if(c2 == -1)
  71. break;
  72. out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
  73. /* c3 */
  74. do {
  75. c3 = str.charCodeAt(i++) & 0xff;
  76. if(c3 == 61)
  77. return out;
  78. c3 = base64DecodeChars[c3];
  79. } while(i < len && c3 == -1);
  80. if(c3 == -1)
  81. break;
  82. out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
  83. /* c4 */
  84. do {
  85. c4 = str.charCodeAt(i++) & 0xff;
  86. if(c4 == 61)
  87. return out;
  88. c4 = base64DecodeChars[c4];
  89. } while(i < len && c4 == -1);
  90. if(c4 == -1)
  91. break;
  92. out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
  93. }
  94. return out;
  95. }
  96. function utf16to8(str) {
  97. var out, i, len, c;
  98. out = "";
  99. len = str.length;
  100. for(i = 0; i < len; i++) {
  101. c = str.charCodeAt(i);
  102. if ((c >= 0x0001) && (c <= 0x007F)) {
  103. out += str.charAt(i);
  104. } else if (c > 0x07FF) {
  105. out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
  106. out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
  107. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  108. } else {
  109. out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
  110. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  111. }
  112. }
  113. return out;
  114. }
  115. function utf8to16(str) {
  116. var out, i, len, c;
  117. var char2, char3;
  118. out = "";
  119. len = str.length;
  120. i = 0;
  121. while(i < len) {
  122. c = str.charCodeAt(i++);
  123. switch(c >> 4)
  124. {
  125. case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
  126. // 0xxxxxxx
  127. out += str.charAt(i-1);
  128. break;
  129. case 12: case 13:
  130. // 110x xxxx 10xx xxxx
  131. char2 = str.charCodeAt(i++);
  132. out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
  133. break;
  134. case 14:
  135. // 1110 xxxx 10xx xxxx 10xx xxxx
  136. char2 = str.charCodeAt(i++);
  137. char3 = str.charCodeAt(i++);
  138. out += String.fromCharCode(((c & 0x0F) << 12) |
  139. ((char2 & 0x3F) << 6) |
  140. ((char3 & 0x3F) << 0));
  141. break;
  142. }
  143. }
  144. return out;
  145. }
  146. function CharToHex(str) {
  147. var out, i, len, c, h;
  148. out = "";
  149. len = str.length;
  150. i = 0;
  151. while(i < len)
  152. {
  153. c = str.charCodeAt(i++);
  154. h = c.toString(16);
  155. if(h.length < 2)
  156. h = "0" + h;
  157. out += "\\x" + h + " ";
  158. if(i > 0 && i % 8 == 0)
  159. out += "\r\n";
  160. }
  161. return out;
  162. }
  163. function doEncode() {
  164. var src = document.getElementById('src').value;
  165. document.getElementById('dest').value = base64encode(utf16to8(src));
  166. }
  167. function doDecode() {
  168. var src = document.getElementById('src').value;
  169. var opts = document.getElementById('opt');
  170. if(opts.checked)
  171. {
  172. document.getElementById('dest').value = CharToHex(base64decode(src));
  173. }
  174. else
  175. {
  176. document.getElementById('dest').value = utf8to16(base64decode(src));
  177. }
  178. }