decrypt.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. function useDecrypt() {
  2. const keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
  3. function encode64(input) {
  4. let output = ''
  5. let chr1,
  6. chr2,
  7. chr3 = ''
  8. let enc1,
  9. enc2,
  10. enc3,
  11. enc4 = ''
  12. let i = 0
  13. do {
  14. chr1 = input.charCodeAt(i++)
  15. chr2 = input.charCodeAt(i++)
  16. chr3 = input.charCodeAt(i++)
  17. enc1 = chr1 >> 2
  18. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4)
  19. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6)
  20. enc4 = chr3 & 63
  21. if (isNaN(chr2)) {
  22. enc3 = enc4 = 64
  23. } else if (isNaN(chr3)) {
  24. enc4 = 64
  25. }
  26. output =
  27. output +
  28. keyStr.charAt(enc1) +
  29. keyStr.charAt(enc2) +
  30. keyStr.charAt(enc3) +
  31. keyStr.charAt(enc4)
  32. chr1 = chr2 = chr3 = ''
  33. enc1 = enc2 = enc3 = enc4 = ''
  34. } while (i < input.length)
  35. return output
  36. }
  37. function utf16to8(str) {
  38. let out,
  39. i,
  40. len = 0,
  41. c
  42. out = ''
  43. len = str.length
  44. for (i = 0; i < len; i++) {
  45. c = str.charCodeAt(i)
  46. if (c >= 0x0001 && c <= 0x007f) {
  47. out += str.charAt(i)
  48. } else if (c > 0x07ff) {
  49. out += String.fromCharCode(0xe0 | ((c >> 12) & 0x0f))
  50. out += String.fromCharCode(0x80 | ((c >> 6) & 0x3f))
  51. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f))
  52. } else {
  53. out += String.fromCharCode(0xc0 | ((c >> 6) & 0x1f))
  54. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f))
  55. }
  56. }
  57. return out
  58. }
  59. function utf8to16(str) {
  60. let out,
  61. i,
  62. len = 0,
  63. c
  64. let char2, char3
  65. out = ''
  66. len = str.length
  67. i = 0
  68. while (i < len) {
  69. c = str.charCodeAt(i++)
  70. switch (c >> 4) {
  71. case 0:
  72. case 1:
  73. case 2:
  74. case 3:
  75. case 4:
  76. case 5:
  77. case 6:
  78. case 7:
  79. // 0xxxxxxx
  80. out += str.charAt(i - 1)
  81. break
  82. case 12:
  83. case 13:
  84. // 110x xxxx 10xx xxxx
  85. char2 = str.charCodeAt(i++)
  86. out += String.fromCharCode(((c & 0x1f) << 6) | (char2 & 0x3f))
  87. break
  88. case 14:
  89. // 1110 xxxx 10xx xxxx 10xx xxxx
  90. char2 = str.charCodeAt(i++)
  91. char3 = str.charCodeAt(i++)
  92. out += String.fromCharCode(
  93. ((c & 0x0f) << 12) | ((char2 & 0x3f) << 6) | ((char3 & 0x3f) << 0)
  94. )
  95. break
  96. }
  97. }
  98. return out
  99. }
  100. function hex_md5(s) {
  101. return binl2hex(core_md5(str2binl(s), s.length * chrsz))
  102. }
  103. const hexcase = 0 /* hex output format. 0 - lowercase; 1 - uppercase */
  104. const b64pad = '' /* base-64 pad character. "=" for strict RFC compliance */
  105. const chrsz = 8 /* bits per input character. 8 - ASCII; 16 - Unicode */
  106. /*
  107. * Convert a string to an array of little-endian words
  108. * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
  109. */
  110. function str2binl(str) {
  111. const bin = []
  112. const mask = (1 << chrsz) - 1
  113. for (let i = 0; i < str.length * chrsz; i += chrsz)
  114. bin[i >> 5] |= (str.charCodeAt(i / chrsz) & mask) << i % 32
  115. return bin
  116. }
  117. /*
  118. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  119. * to work around bugs in some JS interpreters.
  120. */
  121. function safe_add(x, y) {
  122. const lsw = (x & 0xffff) + (y & 0xffff)
  123. const msw = (x >> 16) + (y >> 16) + (lsw >> 16)
  124. return (msw << 16) | (lsw & 0xffff)
  125. }
  126. /*
  127. * Bitwise rotate a 32-bit number to the left.
  128. */
  129. function bit_rol(num, cnt) {
  130. return (num << cnt) | (num >>> (32 - cnt))
  131. }
  132. /*
  133. * These functions implement the four basic operations the algorithm uses.
  134. */
  135. function md5_cmn(q, a, b, x, s, t) {
  136. return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b)
  137. }
  138. function md5_ff(a, b, c, d, x, s, t) {
  139. return md5_cmn((b & c) | (~b & d), a, b, x, s, t)
  140. }
  141. function md5_gg(a, b, c, d, x, s, t) {
  142. return md5_cmn((b & d) | (c & ~d), a, b, x, s, t)
  143. }
  144. function md5_hh(a, b, c, d, x, s, t) {
  145. return md5_cmn(b ^ c ^ d, a, b, x, s, t)
  146. }
  147. function md5_ii(a, b, c, d, x, s, t) {
  148. return md5_cmn(c ^ (b | ~d), a, b, x, s, t)
  149. }
  150. function core_md5(x, len) {
  151. /* append padding */
  152. x[len >> 5] |= 0x80 << len % 32
  153. x[(((len + 64) >>> 9) << 4) + 14] = len
  154. let a = 1732584193
  155. let b = -271733879
  156. let c = -1732584194
  157. let d = 271733878
  158. for (let i = 0; i < x.length; i += 16) {
  159. const olda = a
  160. const oldb = b
  161. const oldc = c
  162. const oldd = d
  163. a = md5_ff(a, b, c, d, x[i + 0], 7, -680876936)
  164. d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586)
  165. c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819)
  166. b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330)
  167. a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897)
  168. d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426)
  169. c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341)
  170. b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983)
  171. a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416)
  172. d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417)
  173. c = md5_ff(c, d, a, b, x[i + 10], 17, -42063)
  174. b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162)
  175. a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682)
  176. d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101)
  177. c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290)
  178. b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329)
  179. a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510)
  180. d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632)
  181. c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713)
  182. b = md5_gg(b, c, d, a, x[i + 0], 20, -373897302)
  183. a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691)
  184. d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083)
  185. c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335)
  186. b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848)
  187. a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438)
  188. d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690)
  189. c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961)
  190. b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501)
  191. a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467)
  192. d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784)
  193. c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473)
  194. b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734)
  195. a = md5_hh(a, b, c, d, x[i + 5], 4, -378558)
  196. d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463)
  197. c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562)
  198. b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556)
  199. a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060)
  200. d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353)
  201. c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632)
  202. b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640)
  203. a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174)
  204. d = md5_hh(d, a, b, c, x[i + 0], 11, -358537222)
  205. c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979)
  206. b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189)
  207. a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487)
  208. d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835)
  209. c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520)
  210. b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651)
  211. a = md5_ii(a, b, c, d, x[i + 0], 6, -198630844)
  212. d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415)
  213. c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905)
  214. b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055)
  215. a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571)
  216. d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606)
  217. c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523)
  218. b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799)
  219. a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359)
  220. d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744)
  221. c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380)
  222. b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649)
  223. a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070)
  224. d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379)
  225. c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259)
  226. b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551)
  227. a = safe_add(a, olda)
  228. b = safe_add(b, oldb)
  229. c = safe_add(c, oldc)
  230. d = safe_add(d, oldd)
  231. }
  232. return [a, b, c, d]
  233. }
  234. function binl2hex(binarray) {
  235. const hex_tab = hexcase ? '0123456789ABCDEF' : '0123456789abcdef'
  236. let str = ''
  237. for (let i = 0; i < binarray.length * 4; i++) {
  238. str +=
  239. hex_tab.charAt((binarray[i >> 2] >> ((i % 4) * 8 + 4)) & 0xf) +
  240. hex_tab.charAt((binarray[i >> 2] >> ((i % 4) * 8)) & 0xf)
  241. }
  242. return str
  243. }
  244. function decode64(input) {
  245. let output = ''
  246. let chr1,
  247. chr2,
  248. chr3 = ''
  249. let enc1,
  250. enc2,
  251. enc3,
  252. enc4 = ''
  253. let i = 0
  254. if (input.length % 4 != 0) {
  255. return ''
  256. }
  257. const base64test = /[^A-Za-z0-9\\+\\/\\=]/g
  258. if (base64test.exec(input)) {
  259. return ''
  260. }
  261. do {
  262. enc1 = keyStr.indexOf(input.charAt(i++))
  263. enc2 = keyStr.indexOf(input.charAt(i++))
  264. enc3 = keyStr.indexOf(input.charAt(i++))
  265. enc4 = keyStr.indexOf(input.charAt(i++))
  266. chr1 = (enc1 << 2) | (enc2 >> 4)
  267. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2)
  268. chr3 = ((enc3 & 3) << 6) | enc4
  269. output = output + String.fromCharCode(chr1)
  270. if (enc3 != 64) {
  271. output += String.fromCharCode(chr2)
  272. }
  273. if (enc4 != 64) {
  274. output += String.fromCharCode(chr3)
  275. }
  276. chr1 = chr2 = chr3 = ''
  277. enc1 = enc2 = enc3 = enc4 = ''
  278. } while (i < input.length)
  279. return output
  280. }
  281. function encrypt(data, key) {
  282. //key = "123456789";
  283. data = native2ascii(data)
  284. key = hex_md5(key)
  285. let str = ''
  286. let char = ''
  287. key = key.toUpperCase()
  288. let x = 0
  289. const len = data.length
  290. const l = key.length
  291. for (let i = 0; i < len; i++) {
  292. if (x == l) {
  293. x = 0
  294. }
  295. char += key.substr(x, 1)
  296. x++
  297. }
  298. for (let i = 0; i < len; i++) {
  299. str += String.fromCharCode(data[i].charCodeAt() + (char[i].charCodeAt() % 256))
  300. }
  301. let k = utf16to8(str)
  302. k = encode64(k)
  303. return k
  304. }
  305. function decrypt(data, key) {
  306. key = hex_md5(key)
  307. key = key.toUpperCase()
  308. let x = 0
  309. data = decode64(data)
  310. data = utf8to16(data)
  311. let char = ''
  312. let str = ''
  313. const len = data.length
  314. const l = key.length
  315. for (let i = 0; i < len; i++) {
  316. if (x == l) {
  317. x = 0
  318. }
  319. char += key.substr(x, 1)
  320. x++
  321. }
  322. for (let i = 0; i < len; i++) {
  323. if (data.substr(i, 1).charCodeAt() < char.substr(i, 1).charCodeAt()) {
  324. str += String.fromCharCode(
  325. data.substr(i, 1).charCodeAt() + 256 - char.substr(i, 1).charCodeAt()
  326. )
  327. } else {
  328. str += String.fromCharCode(data.substr(i, 1).charCodeAt() - char.substr(i, 1).charCodeAt())
  329. }
  330. }
  331. return ascii2native(str)
  332. }
  333. function ascii2native(strAscii) {
  334. let output = ''
  335. let posFrom = 0
  336. let posTo = strAscii.indexOf('\\u', posFrom)
  337. while (posTo >= 0) {
  338. output += strAscii.substring(posFrom, posTo)
  339. output += toChar(strAscii.substr(posTo, 6))
  340. posFrom = posTo + 6
  341. posTo = strAscii.indexOf('\\u', posFrom)
  342. }
  343. output += strAscii.substr(posFrom)
  344. return output
  345. }
  346. function native2ascii(strNative) {
  347. let output = ''
  348. for (let i = 0; i < strNative.length; i++) {
  349. const c = strNative.charAt(i)
  350. const cc = strNative.charCodeAt(i)
  351. if (cc > 0xff) output += '\\u' + toHex(cc >> 8) + toHex(cc & 0xff)
  352. else output += c
  353. }
  354. return output
  355. }
  356. function toHex(n) {
  357. const nH = (n >> 4) & 0x0f
  358. const nL = n & 0x0f
  359. return hexChars.charAt(nH) + hexChars.charAt(nL)
  360. }
  361. const hexChars = '0123456789ABCDEF'
  362. function toChar(str) {
  363. if (str.substr(0, 2) != '\\u') return str
  364. let code = 0
  365. for (let i = 2; i < str.length; i++) {
  366. let cc = str.charCodeAt(i)
  367. if (cc >= 0x30 && cc <= 0x39) cc = cc - 0x30
  368. else if (cc >= 0x41 && cc <= 0x5a) cc = cc - 0x41 + 10
  369. else if (cc >= 0x61 && cc <= 0x7a) cc = cc - 0x61 + 10
  370. code <<= 4
  371. code += cc
  372. }
  373. if (code < 0xff) return str
  374. return String.fromCharCode(code)
  375. }
  376. return {
  377. encrypt,
  378. decrypt
  379. }
  380. }