123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- function useDecrypt() {
- const keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
- function encode64(input) {
- let output = ''
- let chr1,
- chr2,
- chr3 = ''
- let enc1,
- enc2,
- enc3,
- enc4 = ''
- let i = 0
- do {
- chr1 = input.charCodeAt(i++)
- chr2 = input.charCodeAt(i++)
- chr3 = input.charCodeAt(i++)
- enc1 = chr1 >> 2
- enc2 = ((chr1 & 3) << 4) | (chr2 >> 4)
- enc3 = ((chr2 & 15) << 2) | (chr3 >> 6)
- enc4 = chr3 & 63
- if (isNaN(chr2)) {
- enc3 = enc4 = 64
- } else if (isNaN(chr3)) {
- enc4 = 64
- }
- output =
- output +
- keyStr.charAt(enc1) +
- keyStr.charAt(enc2) +
- keyStr.charAt(enc3) +
- keyStr.charAt(enc4)
- chr1 = chr2 = chr3 = ''
- enc1 = enc2 = enc3 = enc4 = ''
- } while (i < input.length)
- return output
- }
- function utf16to8(str) {
- let out,
- i,
- len = 0,
- c
- out = ''
- len = str.length
- for (i = 0; i < len; i++) {
- c = str.charCodeAt(i)
- if (c >= 0x0001 && c <= 0x007f) {
- out += str.charAt(i)
- } else if (c > 0x07ff) {
- out += String.fromCharCode(0xe0 | ((c >> 12) & 0x0f))
- out += String.fromCharCode(0x80 | ((c >> 6) & 0x3f))
- out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f))
- } else {
- out += String.fromCharCode(0xc0 | ((c >> 6) & 0x1f))
- out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f))
- }
- }
- return out
- }
- function utf8to16(str) {
- let out,
- i,
- len = 0,
- c
- let char2, char3
- out = ''
- len = str.length
- i = 0
- while (i < len) {
- c = str.charCodeAt(i++)
- switch (c >> 4) {
- case 0:
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- case 6:
- case 7:
- // 0xxxxxxx
- out += str.charAt(i - 1)
- break
- case 12:
- case 13:
- // 110x xxxx 10xx xxxx
- char2 = str.charCodeAt(i++)
- out += String.fromCharCode(((c & 0x1f) << 6) | (char2 & 0x3f))
- break
- case 14:
- // 1110 xxxx 10xx xxxx 10xx xxxx
- char2 = str.charCodeAt(i++)
- char3 = str.charCodeAt(i++)
- out += String.fromCharCode(
- ((c & 0x0f) << 12) | ((char2 & 0x3f) << 6) | ((char3 & 0x3f) << 0)
- )
- break
- }
- }
- return out
- }
- function hex_md5(s) {
- return binl2hex(core_md5(str2binl(s), s.length * chrsz))
- }
- const hexcase = 0 /* hex output format. 0 - lowercase; 1 - uppercase */
- const b64pad = '' /* base-64 pad character. "=" for strict RFC compliance */
- const chrsz = 8 /* bits per input character. 8 - ASCII; 16 - Unicode */
- /*
- * Convert a string to an array of little-endian words
- * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
- */
- function str2binl(str) {
- const bin = []
- const mask = (1 << chrsz) - 1
- for (let i = 0; i < str.length * chrsz; i += chrsz)
- bin[i >> 5] |= (str.charCodeAt(i / chrsz) & mask) << i % 32
- return bin
- }
- /*
- * Add integers, wrapping at 2^32. This uses 16-bit operations internally
- * to work around bugs in some JS interpreters.
- */
- function safe_add(x, y) {
- const lsw = (x & 0xffff) + (y & 0xffff)
- const msw = (x >> 16) + (y >> 16) + (lsw >> 16)
- return (msw << 16) | (lsw & 0xffff)
- }
- /*
- * Bitwise rotate a 32-bit number to the left.
- */
- function bit_rol(num, cnt) {
- return (num << cnt) | (num >>> (32 - cnt))
- }
- /*
- * These functions implement the four basic operations the algorithm uses.
- */
- function md5_cmn(q, a, b, x, s, t) {
- return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b)
- }
- function md5_ff(a, b, c, d, x, s, t) {
- return md5_cmn((b & c) | (~b & d), a, b, x, s, t)
- }
- function md5_gg(a, b, c, d, x, s, t) {
- return md5_cmn((b & d) | (c & ~d), a, b, x, s, t)
- }
- function md5_hh(a, b, c, d, x, s, t) {
- return md5_cmn(b ^ c ^ d, a, b, x, s, t)
- }
- function md5_ii(a, b, c, d, x, s, t) {
- return md5_cmn(c ^ (b | ~d), a, b, x, s, t)
- }
- function core_md5(x, len) {
- /* append padding */
- x[len >> 5] |= 0x80 << len % 32
- x[(((len + 64) >>> 9) << 4) + 14] = len
- let a = 1732584193
- let b = -271733879
- let c = -1732584194
- let d = 271733878
- for (let i = 0; i < x.length; i += 16) {
- const olda = a
- const oldb = b
- const oldc = c
- const oldd = d
- a = md5_ff(a, b, c, d, x[i + 0], 7, -680876936)
- d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586)
- c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819)
- b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330)
- a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897)
- d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426)
- c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341)
- b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983)
- a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416)
- d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417)
- c = md5_ff(c, d, a, b, x[i + 10], 17, -42063)
- b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162)
- a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682)
- d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101)
- c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290)
- b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329)
- a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510)
- d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632)
- c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713)
- b = md5_gg(b, c, d, a, x[i + 0], 20, -373897302)
- a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691)
- d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083)
- c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335)
- b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848)
- a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438)
- d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690)
- c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961)
- b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501)
- a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467)
- d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784)
- c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473)
- b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734)
- a = md5_hh(a, b, c, d, x[i + 5], 4, -378558)
- d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463)
- c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562)
- b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556)
- a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060)
- d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353)
- c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632)
- b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640)
- a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174)
- d = md5_hh(d, a, b, c, x[i + 0], 11, -358537222)
- c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979)
- b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189)
- a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487)
- d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835)
- c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520)
- b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651)
- a = md5_ii(a, b, c, d, x[i + 0], 6, -198630844)
- d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415)
- c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905)
- b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055)
- a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571)
- d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606)
- c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523)
- b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799)
- a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359)
- d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744)
- c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380)
- b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649)
- a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070)
- d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379)
- c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259)
- b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551)
- a = safe_add(a, olda)
- b = safe_add(b, oldb)
- c = safe_add(c, oldc)
- d = safe_add(d, oldd)
- }
- return [a, b, c, d]
- }
- function binl2hex(binarray) {
- const hex_tab = hexcase ? '0123456789ABCDEF' : '0123456789abcdef'
- let str = ''
- for (let i = 0; i < binarray.length * 4; i++) {
- str +=
- hex_tab.charAt((binarray[i >> 2] >> ((i % 4) * 8 + 4)) & 0xf) +
- hex_tab.charAt((binarray[i >> 2] >> ((i % 4) * 8)) & 0xf)
- }
- return str
- }
- function decode64(input) {
- let output = ''
- let chr1,
- chr2,
- chr3 = ''
- let enc1,
- enc2,
- enc3,
- enc4 = ''
- let i = 0
- if (input.length % 4 != 0) {
- return ''
- }
- const base64test = /[^A-Za-z0-9\\+\\/\\=]/g
- if (base64test.exec(input)) {
- return ''
- }
- do {
- enc1 = keyStr.indexOf(input.charAt(i++))
- enc2 = keyStr.indexOf(input.charAt(i++))
- enc3 = keyStr.indexOf(input.charAt(i++))
- enc4 = keyStr.indexOf(input.charAt(i++))
- chr1 = (enc1 << 2) | (enc2 >> 4)
- chr2 = ((enc2 & 15) << 4) | (enc3 >> 2)
- chr3 = ((enc3 & 3) << 6) | enc4
- output = output + String.fromCharCode(chr1)
- if (enc3 != 64) {
- output += String.fromCharCode(chr2)
- }
- if (enc4 != 64) {
- output += String.fromCharCode(chr3)
- }
- chr1 = chr2 = chr3 = ''
- enc1 = enc2 = enc3 = enc4 = ''
- } while (i < input.length)
- return output
- }
- function encrypt(data, key) {
- //key = "123456789";
- data = native2ascii(data)
- key = hex_md5(key)
- let str = ''
- let char = ''
- key = key.toUpperCase()
- let x = 0
- const len = data.length
- const l = key.length
- for (let i = 0; i < len; i++) {
- if (x == l) {
- x = 0
- }
- char += key.substr(x, 1)
- x++
- }
- for (let i = 0; i < len; i++) {
- str += String.fromCharCode(data[i].charCodeAt() + (char[i].charCodeAt() % 256))
- }
- let k = utf16to8(str)
- k = encode64(k)
- return k
- }
- function decrypt(data, key) {
- key = hex_md5(key)
- key = key.toUpperCase()
- let x = 0
- data = decode64(data)
- data = utf8to16(data)
- let char = ''
- let str = ''
- const len = data.length
- const l = key.length
- for (let i = 0; i < len; i++) {
- if (x == l) {
- x = 0
- }
- char += key.substr(x, 1)
- x++
- }
- for (let i = 0; i < len; i++) {
- if (data.substr(i, 1).charCodeAt() < char.substr(i, 1).charCodeAt()) {
- str += String.fromCharCode(
- data.substr(i, 1).charCodeAt() + 256 - char.substr(i, 1).charCodeAt()
- )
- } else {
- str += String.fromCharCode(data.substr(i, 1).charCodeAt() - char.substr(i, 1).charCodeAt())
- }
- }
- return ascii2native(str)
- }
- function ascii2native(strAscii) {
- let output = ''
- let posFrom = 0
- let posTo = strAscii.indexOf('\\u', posFrom)
- while (posTo >= 0) {
- output += strAscii.substring(posFrom, posTo)
- output += toChar(strAscii.substr(posTo, 6))
- posFrom = posTo + 6
- posTo = strAscii.indexOf('\\u', posFrom)
- }
- output += strAscii.substr(posFrom)
- return output
- }
- function native2ascii(strNative) {
- let output = ''
- for (let i = 0; i < strNative.length; i++) {
- const c = strNative.charAt(i)
- const cc = strNative.charCodeAt(i)
- if (cc > 0xff) output += '\\u' + toHex(cc >> 8) + toHex(cc & 0xff)
- else output += c
- }
- return output
- }
- function toHex(n) {
- const nH = (n >> 4) & 0x0f
- const nL = n & 0x0f
- return hexChars.charAt(nH) + hexChars.charAt(nL)
- }
- const hexChars = '0123456789ABCDEF'
- function toChar(str) {
- if (str.substr(0, 2) != '\\u') return str
- let code = 0
- for (let i = 2; i < str.length; i++) {
- let cc = str.charCodeAt(i)
- if (cc >= 0x30 && cc <= 0x39) cc = cc - 0x30
- else if (cc >= 0x41 && cc <= 0x5a) cc = cc - 0x41 + 10
- else if (cc >= 0x61 && cc <= 0x7a) cc = cc - 0x61 + 10
- code <<= 4
- code += cc
- }
- if (code < 0xff) return str
- return String.fromCharCode(code)
- }
- return {
- encrypt,
- decrypt
- }
- }
|