('%02x'):format(Number) 在 Node Js 中的应用

尝试将 lua 加密转换为 Node.JS,只需要加入以下内容。 ("%02x"):format(c) 完成后,就可以在 node.js 中获得它, 我尝试使用缓冲区(buffer),但它没有起作用。 尝试了其他几种方法,它们也都不起作用。

加密公式的原始帖子: 用于 ROBLOX Lua 的低度影响力加密公式 Lua 代码:

local Key53 = 8186484168865098
local Key14 = 4887
local inv256

    function encode(str)
        if not inv256 then
            inv256 = {}
            for M = 0, 127 do
                local inv = -1

                repeat
                    inv = inv + 2
                until inv * (2*M + 1) % 256 == 1
                inv256[M] = inv
            end
        end

        local K, F = Key53, 16384 + Key14
        return (str:gsub('.',
            function(m)
                local L = K % 274877906944  -- 2^38
                local H = (K - L) / 274877906944
                local M = H % 128
                m = m:byte()

                local c = (m * inv256[M] - (H - M) / 128) % 256
                K = L * F + H + c + m

                --print("lol ".. c)
                --print(('%02x'):format(c))
                return ('%02x'):format(c)
            end
        ))
    end

    function decode(str)
        local K, F = Key53, 16384 + Key14
        return (str:gsub('%x%x',
            function(c)
                local L = K % 274877906944  -- 2^38
                local H = (K - L) / 274877906944
                local M = H % 128
                c = tonumber(c, 16)

                local m = (c + (H - M) / 128) * (2*M + 1) % 256
                K = L * F + H + c + m

                return string.char(m)
            end
        ))
    end

原文链接 https://stackoverflow.com/questions/69691311

点赞
stackoverflow用户6367889
stackoverflow用户6367889

%02x 仅仅是将十进制数转换成十六进制数。在 JS 中可以这样实现:

function toHex(value) {
    let hex = value.toString(16);
    if ((hex.length % 2) > 0) {
        hex = "0" + hex;
    }
    return hex;
}

输出:

toHex(120) // 78
('%02x'):format(120) -- 78
2021-10-24 04:26:25