node.js - ('%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
解决方案
%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
推荐阅读
- python - 如何使用 NetGear 发送视频
- python - 如何在构建数据集时处理内存错误
- algorithm - 凝聚聚类和分裂聚类最终能得到相同的结果吗?
- arrays - 使用 powershell 将 exe 转换为 hexdump
- android - 包含布局中的 OnClick 侦听器
- python - 如何为 QPushButton 的背景颜色设置动画(动态更改按钮颜色)
- azure - 将 Azure AD 凭据集成到 Kubeflow 笔记本 pod
- android - 如果我需要在某些参数更改后重新加载数据,那么在 MVVM 中使用实时数据的正确方法是什么?
- javascript - h3 不会显示可能是因为导航栏
- python - Azure Function ImportError while importing test module / ModuleNotFoundError: No module named