首页 > 解决方案 > ('%02x'): 节点 Js 中的格式(数字)

问题描述

尝试将 lua 加密转换为 Node.JS,只需要它。 ("%02x"):format(c) 有一次,我知道我会在 node.js 中拥有它,所以我尝试了缓冲区,但它没有用。尝试了其他一些东西,它们也没有奏效。

此加密的原始帖子: 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

标签: node.jslua

解决方案


%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

推荐阅读