还是直接上代码吧~
function _long2str(v, w) local len = #v local n = bit.lshift((len - 1) , 2) if (w) then local m = v [len] if ((m < n - 3) or (m > n)) then return false end n = m end local s = {} for i = 1 , len do s [i] = string.packL(v[i]) end if (w) then return string.sub(table.concat(s, ''), 0, n) else return table.concat(s, '') end end function _str2long(s, w) local v = string.unpackL(s .. string.rep("\0", (4 - bit.band((string.len(s) % 4) , 3)))) if (w) then v [#v+1] = string.len(s) end return v end function _int32(n) while (n >= 2147483649) do n = n - 4294967296 end while (n <= -2147483649) do n = n + 4294967296 end return n end
function security.xxTEAEncrypt(str, key, toBase64) toBase64 = toBase64 or true if (str == "") then return "" end local v = _str2long(str, true) local k = _str2long(key, false) if (#k < 4) then for i = #k+1, 4, 1 do k [i] = 0 end end local n = #v local z = v [n] local y = v [1] local delta = -0x61C88647 local q = math.floor(6 + 52 / (n)) --设定加密轮数 local sum = 0 while 0 < q do q = q -1 sum = _int32(sum + delta) e = bit.band(bit.rshift(sum , 2), 3) local p = 0 for p = 0, n-2 do y = v [p + 2] mx = bit.bxor(_int32(bit.bxor(bit.band(bit.rshift(z, 5), 0x07ffffff), bit.lshift(y , 2)) + bit.bxor(bit.band(bit.rshift(y, 3), 0x1fffffff), bit.lshift(z, 4))), _int32(bit.bxor(sum, y) + bit.bxor(k[bit.bxor(bit.band((p), 3), e)+1], z))) z = _int32(v[p+1] + mx) v [p+1] = z end p = #v - 1 y = v [1] mx = bit.bxor(_int32(bit.bxor(bit.band(bit.rshift(z, 5), 0x07ffffff), bit.lshift(y , 2)) + bit.bxor(bit.band(bit.rshift(y, 3), 0x1fffffff), bit.lshift(z, 4))), _int32(bit.bxor(sum, y) + bit.bxor(k[bit.bxor(bit.band((p), 3), e)+1], z))) z = _int32(v[n] + mx) v [n] = z end if (toBase64) then local r = security.url_safe_base64_encode(_long2str(v, false)) return r end return _long2str(v, false) end function security.xxTEADecrypt(str, key, toBase64) toBase64 = toBase64 or true if (str == "") then return "" end -- logcat(str) if toBase64 then str = security.url_safe_base64_decode(str) end local v = _str2long(str, false) local k = _str2long(key, false) if (#k < 4) then for i = #k+1, 4, 1 do k [i] = 0 end end local n = #v - 1 local z = v [n] local y = v [1] local delta = -0x61C88647 local q = math.floor(6 + 52 / (n+1)) --设定加密轮数 local sum = _int32(q * delta) while (sum ~= 0) do e = bit.band(bit.rshift(sum , 2), 3) -- test_result = test_result .."e: "..e.."\n" -- logcat ("e: "..e) local p = n for p = n, 1, -1 do z = v [p] -- test_result = test_result .."z: "..z.."p-1: "..(p-1).."\n" -- logcat ("z: "..z.."p-1: "..p-1) -- test_result = test_result .."y: "..y.."\n" -- logcat ("y: "..y) -- test_result = test_result .."p: "..p.."\n" -- logcat ("p: "..p) -- test_result = test_result .."k: "..(k[bit.bxor(bit.band((p), 3), e)+1]).."\n" -- logcat ("k: "..k[bit.bxor(bit.band((p), 3), e)+1]) mx = bit.bxor(_int32(bit.bxor(bit.band(bit.rshift(z, 5), 0x07ffffff), bit.lshift(y , 2)) + bit.bxor(bit.band(bit.rshift(y, 3), 0x1fffffff), bit.lshift(z, 4))), _int32(bit.bxor(sum, y) + bit.bxor(k[bit.bxor(bit.band((p), 3), e)+1], z))) -- test_result = test_result .."mx: "..mx.."\n" -- logcat ("mx: "..mx) y = _int32(v [p+1] - mx) -- test_result = test_result .."y: "..y.."p: "..p.."\n" -- logcat ("y: "..y.."p: "..p) v [p+1] = y end p = 0 z = v[n+1] -- test_result = test_result .."z2: "..z.."\n" -- logcat ("z2: "..z) mx = bit.bxor(_int32(bit.bxor(bit.band(bit.rshift(z, 5), 0x07ffffff), bit.lshift(y , 2)) + bit.bxor(bit.band(bit.rshift(y, 3), 0x1fffffff), bit.lshift(z, 4))), _int32(bit.bxor(sum, y) + bit.bxor(k[bit.bxor(bit.band((p), 3), e)+1], z))) -- test_result = test_result .."mx2: "..mx.."\n" -- logcat ("mx2: "..mx) y = _int32(v[1] - mx) -- test_result = test_result .."y2: "..y.."\n" -- logcat ("y2: "..y) v [1] = y sum = _int32(sum - delta) end return _long2str(v, true) end
PS: 由于不是纯LUA代码(为Xscript LUA脚本代码),所以需要修改部分代码才可以运行,需注意。