base64.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. 编码规则
  3. Base64编码的思想是是采用64个基本的ASCII码字符对数据进行重新编码。
  4. 它将需要编码的数据拆分成字节数组。
  5. 以3个字节为一组。按顺序排列24 位数据,再把这24位数据分成4组,即每组6位。
  6. 再在每组的的最高位前补两个0凑足一个字节。
  7. 这样就把一个3字节为一组的数据重新编码成了4个字节。
  8. 当所要编码的数据的字节数不是3的整倍数,
  9. 也就是说在分组时最后一组不够3个字节。
  10. 这时在最后一组填充1到2个0字节。
  11. 并在最后编码完成后在结尾添加1到2个 “=”。
  12. 加密解密base64字符串
  13. Add by mazhuang 2016026
  14. */
  15. var Base64 = {
  16. //下面是64个基本的编码
  17. base64EncodeChars:
  18. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
  19. base64DecodeChars: new Array(
  20. -1,
  21. -1,
  22. -1,
  23. -1,
  24. -1,
  25. -1,
  26. -1,
  27. -1,
  28. -1,
  29. -1,
  30. -1,
  31. -1,
  32. -1,
  33. -1,
  34. -1,
  35. -1,
  36. -1,
  37. -1,
  38. -1,
  39. -1,
  40. -1,
  41. -1,
  42. -1,
  43. -1,
  44. -1,
  45. -1,
  46. -1,
  47. -1,
  48. -1,
  49. -1,
  50. -1,
  51. -1,
  52. -1,
  53. -1,
  54. -1,
  55. -1,
  56. -1,
  57. -1,
  58. -1,
  59. -1,
  60. -1,
  61. -1,
  62. -1,
  63. 62,
  64. -1,
  65. -1,
  66. -1,
  67. 63,
  68. 52,
  69. 53,
  70. 54,
  71. 55,
  72. 56,
  73. 57,
  74. 58,
  75. 59,
  76. 60,
  77. 61,
  78. -1,
  79. -1,
  80. -1,
  81. -1,
  82. -1,
  83. -1,
  84. -1,
  85. 0,
  86. 1,
  87. 2,
  88. 3,
  89. 4,
  90. 5,
  91. 6,
  92. 7,
  93. 8,
  94. 9,
  95. 10,
  96. 11,
  97. 12,
  98. 13,
  99. 14,
  100. 15,
  101. 16,
  102. 17,
  103. 18,
  104. 19,
  105. 20,
  106. 21,
  107. 22,
  108. 23,
  109. 24,
  110. 25,
  111. -1,
  112. -1,
  113. -1,
  114. -1,
  115. -1,
  116. -1,
  117. 26,
  118. 27,
  119. 28,
  120. 29,
  121. 30,
  122. 31,
  123. 32,
  124. 33,
  125. 34,
  126. 35,
  127. 36,
  128. 37,
  129. 38,
  130. 39,
  131. 40,
  132. 41,
  133. 42,
  134. 43,
  135. 44,
  136. 45,
  137. 46,
  138. 47,
  139. 48,
  140. 49,
  141. 50,
  142. 51,
  143. -1,
  144. -1,
  145. -1,
  146. -1,
  147. -1
  148. ),
  149. //编码的方法
  150. base64encode: function (str) {
  151. var out, i, len
  152. var c1, c2, c3
  153. len = str.length
  154. i = 0
  155. out = ''
  156. while (i < len) {
  157. c1 = str.charCodeAt(i++) & 0xff
  158. if (i == len) {
  159. out += Base64.base64EncodeChars.charAt(c1 >> 2)
  160. out += Base64.base64EncodeChars.charAt((c1 & 0x3) << 4)
  161. out += '=='
  162. break
  163. }
  164. c2 = str.charCodeAt(i++)
  165. if (i == len) {
  166. out += Base64.base64EncodeChars.charAt(c1 >> 2)
  167. out += Base64.base64EncodeChars.charAt(
  168. ((c1 & 0x3) << 4) | ((c2 & 0xf0) >> 4)
  169. )
  170. out += Base64.base64EncodeChars.charAt((c2 & 0xf) << 2)
  171. out += '='
  172. break
  173. }
  174. c3 = str.charCodeAt(i++)
  175. out += Base64.base64EncodeChars.charAt(c1 >> 2)
  176. out += Base64.base64EncodeChars.charAt(
  177. ((c1 & 0x3) << 4) | ((c2 & 0xf0) >> 4)
  178. )
  179. out += Base64.base64EncodeChars.charAt(
  180. ((c2 & 0xf) << 2) | ((c3 & 0xc0) >> 6)
  181. )
  182. out += Base64.base64EncodeChars.charAt(c3 & 0x3f)
  183. }
  184. return out
  185. },
  186. //解码的方法
  187. base64decode: function (str) {
  188. var c1, c2, c3, c4
  189. var i, len, out
  190. len = str.length
  191. i = 0
  192. out = ''
  193. while (i < len) {
  194. do {
  195. c1 = Base64.base64DecodeChars[str.charCodeAt(i++) & 0xff]
  196. } while (i < len && c1 == -1)
  197. if (c1 == -1) break
  198. do {
  199. c2 = Base64.base64DecodeChars[str.charCodeAt(i++) & 0xff]
  200. } while (i < len && c2 == -1)
  201. if (c2 == -1) break
  202. out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4))
  203. do {
  204. c3 = str.charCodeAt(i++) & 0xff
  205. if (c3 == 61) return out
  206. c3 = Base64.base64DecodeChars[c3]
  207. } while (i < len && c3 == -1)
  208. if (c3 == -1) break
  209. out += String.fromCharCode(((c2 & 0xf) << 4) | ((c3 & 0x3c) >> 2))
  210. do {
  211. c4 = str.charCodeAt(i++) & 0xff
  212. if (c4 == 61) return out
  213. c4 = Base64.base64DecodeChars[c4]
  214. } while (i < len && c4 == -1)
  215. if (c4 == -1) break
  216. out += String.fromCharCode(((c3 & 0x03) << 6) | c4)
  217. }
  218. return out
  219. },
  220. utf16to8: function (str) {
  221. var out, i, len, c
  222. out = ''
  223. len = str.length
  224. for (i = 0; i < len; i++) {
  225. c = str.charCodeAt(i)
  226. if (c >= 0x0001 && c <= 0x007f) {
  227. out += str.charAt(i)
  228. } else if (c > 0x07ff) {
  229. out += String.fromCharCode(0xe0 | ((c >> 12) & 0x0f))
  230. out += String.fromCharCode(0x80 | ((c >> 6) & 0x3f))
  231. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f))
  232. } else {
  233. out += String.fromCharCode(0xc0 | ((c >> 6) & 0x1f))
  234. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f))
  235. }
  236. }
  237. return out
  238. },
  239. utf8to16: function (str) {
  240. var out, i, len, c
  241. var char2, char3
  242. out = ''
  243. len = str.length
  244. i = 0
  245. while (i < len) {
  246. c = str.charCodeAt(i++)
  247. switch (c >> 4) {
  248. case 0:
  249. case 1:
  250. case 2:
  251. case 3:
  252. case 4:
  253. case 5:
  254. case 6:
  255. case 7:
  256. // 0xxxxxxx
  257. out += str.charAt(i - 1)
  258. break
  259. case 12:
  260. case 13:
  261. // 110x xxxx 10xx xxxx
  262. char2 = str.charCodeAt(i++)
  263. out += String.fromCharCode(((c & 0x1f) << 6) | (char2 & 0x3f))
  264. break
  265. case 14:
  266. // 1110 xxxx 10xx xxxx 10xx xxxx
  267. char2 = str.charCodeAt(i++)
  268. char3 = str.charCodeAt(i++)
  269. out += String.fromCharCode(
  270. ((c & 0x0f) << 12) | ((char2 & 0x3f) << 6) | ((char3 & 0x3f) << 0)
  271. )
  272. break
  273. }
  274. }
  275. return out
  276. }, //end
  277. encode: function (input) {
  278. return Base64.base64encode(Base64.utf16to8(input))
  279. },
  280. decode: function (str) {
  281. return Base64.utf8to16(Base64.base64decode(str))
  282. },
  283. isBase64: function (str) {
  284. return !str.match(/./g).some((x) => this.base64EncodeChars.indexOf(x) == -1)
  285. },
  286. }