吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 230|回复: 2
收起左侧

[其他求助] 求大佬将这个JS加解密函数翻译成LUA

[复制链接]
donghaostdio 发表于 2024-10-4 12:20
25吾爱币
[JavaScript] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
function (str, operation, key, expiry) {
    var operation = operation ? operation : 'DECODE';
    var key = key ? key : '';
    var expiry = expiry ? expiry : 0;
 
    var ckey_length = 4;
    key = md5(key);
 
    // 密匙a会参与加解密
    var keya = md5(key.substr(0, 16));
    // 密匙b会用来做数据完整性验证
    var keyb = md5(key.substr(16, 16));
    // 密匙c用于变化生成的密文
    // IE下不支持substr第一个参数为负数的情况
    if (ckey_length) {
        if (operation == 'DECODE') {
            var keyc = str.substr(0, ckey_length);
        } else {
            var md5_time = md5(microtime());
            var start = md5_time.length - ckey_length;
            var keyc = md5_time.substr(start, ckey_length)
        }
    } else {
        var keyc = '';
    }
    // 参与运算的密匙
    var cryptkey = keya + md5(keya + keyc);
 
    var strbuf;
 
    if (operation == 'DECODE') {
        str = str.substr(ckey_length);
        strbuf = base64_decode(str);
        //string = b.toString();
    } else {
        expiry = expiry ? expiry + time() : 0;
        tmpstr = expiry.toString();
        if (tmpstr.length >= 10) str = tmpstr.substr(0, 10) + md5(str + keyb).substr(0, 16) + str;
        else {
            var count = 10 - tmpstr.length;
            for (var i = 0; i < count; i++) {
                tmpstr = '0' + tmpstr;
            }
            str = tmpstr + md5(str + keyb).substr(0, 16) + str;
        }
        strbuf = str;
    }
 
    var box = new Array(256);
    for (var i = 0; i < 256; i++) {
        box[i] = i;
    }
    var rndkey = new Array();
    // 产生密匙簿
    for (var i = 0; i < 256; i++) {
        rndkey[i] = cryptkey.charCodeAt(i % cryptkey.length);
    }
    // 用固定的算法,打乱密匙簿,增加随机性,好像很复杂,实际上对并不会增加密文的强度
    for (var j = i = 0; i < 256; i++) {
        j = (j + box[i] + rndkey[i]) % 256;
        tmp = box[i];
        box[i] = box[j];
        box[j] = tmp;
    }
 
    // 核心加解密部分
    var s = '';
    //IE下不支持直接通过下标访问字符串的字符,需要先转换为数组
    strbuf = strbuf.split('');
    for (var a = j = i = 0; i < strbuf.length; i++) {
        a = (a + 1) % 256;
        j = (j + box[a]) % 256;
        tmp = box[a];
        box[a] = box[j];
        box[j] = tmp;
        // 从密匙簿得出密匙进行异或,再转成字符
        s += chr(ord(strbuf[i]) ^ (box[(box[a] + box[j]) % 256]));
    }
 
    if (operation == 'DECODE') {
        if ((s.substr(0, 10) == 0 || s.substr(0, 10) - time() > 0) && s.substr(10, 16) == md5(s.substr(26) + keyb).substr(0, 16)) {
            s = s.substr(26);
        } else {
            s = '';
        }
    } else {
        s = base64_encode(s);
        var regex = new RegExp('=', "g");
        s = s.replace(regex, '');
        s = keyc + s;
    }
 
    return s;
}

最佳答案

查看完整内容

AI翻译的,你看看对不对,不对就算了 [md]``` local function md5(str) -- Function to compute MD5 hash return require('md5').sumhexa(str) end local function microtime() return os.time() + os.clock() end local function base64_encode(input) return (require('base64').encode(input)) end local function base64_decode(input) return (require('base64').decode(input) ...

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

selfobj455 发表于 2024-10-4 12:20
AI翻译的,你看看对不对,不对就算了;www

local function md5(str)
    -- Function to compute MD5 hash
    return require('md5').sumhexa(str)
end

local function microtime()
    return os.time() + os.clock()
end

local function base64_encode(input)
    return (require('base64').encode(input))
end

local function base64_decode(input)
    return (require('base64').decode(input))
end

local function chr(value)
    return string.char(value)
end

local function ord(char)
    return string.byte(char)
end

local function encryption_function(str, operation, key, expiry)
    operation = operation or 'DECODE'
    key = key or ''
    expiry = expiry or 0

    local ckey_length = 4
    key = md5(key)

    -- 密匙a会参与加解密
    local keya = md5(key:sub(1, 16))
    -- 密匙b会用来做数据完整性验证
    local keyb = md5(key:sub(17, 32))
    -- 密匙c用于变化生成的密文
    local keyc
    if ckey_length > 0 then
        if operation == 'DECODE' then
            keyc = str:sub(1, ckey_length)
        else
            local md5_time = md5(tostring(microtime()))
            local start = #md5_time - ckey_length + 1
            keyc = md5_time:sub(start, start + ckey_length - 1)
        end
    else
        keyc = ''
    end

    local cryptkey = keya .. md5(keya .. keyc)

    local strbuf

    if operation == 'DECODE' then
        str = str:sub(ckey_length + 1)
        strbuf = base64_decode(str)
    else
        if expiry > 0 then
            expiry = expiry + os.time()
        end
        local tmpstr = tostring(expiry)
        if #tmpstr >= 10 then
            str = tmpstr:sub(1, 10) .. md5(str .. keyb):sub(1, 16) .. str
        else
            local count = 10 - #tmpstr
            tmpstr = string.rep('0', count) .. tmpstr
            str = tmpstr .. md5(str .. keyb):sub(1, 16) .. str
        end
        strbuf = str
    end

    local box = {}
    for i = 0, 255 do
        box[i] = i
    end
    local rndkey = {}
    for i = 0, 255 do
        rndkey[i] = ord(cryptkey:sub((i % #cryptkey) + 1, (i % #cryptkey) + 1))
    end

    local j = 0
    for i = 0, 255 do
        j = (j + box[i] + rndkey[i]) % 256
        box[i], box[j] = box[j], box[i]
    end

    local s = ''
    strbuf = {strbuf:byte(1, #strbuf)}
    local a, b = 0, 0
    for i = 1, #strbuf do
        a = (a + 1) % 256
        b = (b + box[a]) % 256
        box[a], box[b] = box[b], box[a]
        s = s .. chr(ord(string.char(strbuf[i])) ~ (box[(box[a] + box[b]) % 256]))
    end

    if operation == 'DECODE' then
        if (s:sub(1, 10) == '0' or tonumber(s:sub(1, 10)) > os.time()) and 
            s:sub(11, 26) == md5(s:sub(27) .. keyb):sub(1, 16) then
            s = s:sub(27)
        else
            s = ''
        end
    else
        s = base64_encode(s)
        s = s:gsub('=', '')
        s = keyc .. s
    end

    return s
end
 楼主| donghaostdio 发表于 2024-10-5 07:47
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-4-3 18:01

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表