Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert encryption to use buffers #1652

Merged
merged 2 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 22 additions & 27 deletions MainModule/Client/Core/Remote.luau
Original file line number Diff line number Diff line change
Expand Up @@ -476,56 +476,51 @@ return function(Vargs, GetEnv)
Remote.Send("PlayerEvent",event,...)
end;

Encrypt = function(str, key, cache)
Encrypt = function(str: string, key: string, cache: {[string]: (buffer|{[string]: string})}?)
cache = cache or Remote.EncodeCache or {}
local keyCache = cache[key] or {}

if not key or not str then
return str
elseif cache[key] and cache[key][str] then
return cache[key][str]
elseif keyCache[str] then
return keyCache[str]
else
local byte = string.byte
local sub = string.sub
local char = string.char

local keyCache = cache[key] or {}
local endStr = {}
local writeu8, readu8 = buffer.writeu8, buffer.readu8
local rawStr, rawKey = buffer.fromstring(str), keyCache[1] or buffer.fromstring(key)
local keyLen = #key

for i = 1, #str do
local keyPos = (i % #key) + 1
endStr[i] = char(((byte(sub(str, i, i)) + byte(sub(key, keyPos, keyPos)))%126) + 1)
local keyPos = (i % keyLen) + 1
writeu8(rawStr, (readu8(rawStr, i) + readu8(rawKey, keyPos)) % 126 + 1)
end

endStr = table.concat(endStr)
cache[key] = keyCache
keyCache[str] = endStr
return endStr
keyCache[str], keyCache[1] = buffer.tostring(rawStr), rawKey
return keyCache[str]
end
end;

Decrypt = function(str, key, cache)
Decrypt = function(str: string, key: string, cache: {[string]: (buffer|{[string]: string})}?)
cache = cache or Remote.DecodeCache or {}
local keyCache = cache[key] or {}

if not key or not str then
return str
elseif cache[key] and cache[key][str] then
return cache[key][str]
elseif keyCache[str] then
return keyCache[str]
else
local keyCache = cache[key] or {}
local byte = string.byte
local sub = string.sub
local char = string.char
local endStr = {}
local writeu8, readu8 = buffer.writeu8, buffer.readu8
local rawStr, rawKey = buffer.fromstring(str), keyCache[1] or buffer.fromstring(key)
local keyLen = #key

for i = 1, #str do
local keyPos = (i % #key)+1
endStr[i] = char(((byte(sub(str, i, i)) - byte(sub(key, keyPos, keyPos)))%126) - 1)
local keyPos = (i % keyLen) + 1
writeu8(rawStr, (readu8(rawStr, i) - readu8(rawKey, keyPos)) % 126 - 1)
end

endStr = table.concat(endStr)
cache[key] = keyCache
keyCache[str] = endStr
return endStr
keyCache[str], keyCache[1] = buffer.tostring(rawStr), rawKey
return keyCache[str]
end
end;
}
Expand Down
49 changes: 22 additions & 27 deletions MainModule/Server/Core/Remote.luau
Original file line number Diff line number Diff line change
Expand Up @@ -1347,56 +1347,51 @@ return function(Vargs, GetEnv)
end
end;

Encrypt = function(str: string?, key: string?, cache: {[string]: any}?)
Encrypt = function(str: string, key: string, cache: {[string]: (buffer|{[string]: string})}?)
cache = cache or Remote.EncodeCache or {}
local keyCache = cache[key] or {}

if not key or not str then
return str
elseif cache[key] and cache[key][str] then
return cache[key][str]
elseif keyCache[str] then
return keyCache[str]
else
local byte = string.byte
local sub = string.sub
local char = string.char

local keyCache = cache[key] or {}
local endStr = {}
local writeu8, readu8 = buffer.writeu8, buffer.readu8
local rawStr, rawKey = buffer.fromstring(str), keyCache[1] or buffer.fromstring(key)
local keyLen = #key

for i = 1, #str do
local keyPos = (i % #key) + 1
endStr[i] = char(((byte(sub(str, i, i)) + byte(sub(key, keyPos, keyPos)))%126) + 1)
local keyPos = (i % keyLen) + 1
writeu8(rawStr, (readu8(rawStr, i) + readu8(rawKey, keyPos)) % 126 + 1)
end

endStr = table.concat(endStr)
cache[key] = keyCache
keyCache[str] = endStr
return endStr
keyCache[str], keyCache[1] = buffer.tostring(rawStr), rawKey
return keyCache[str]
end
end;

Decrypt = function(str: string?, key: string?, cache: {[string]: any}?)
Decrypt = function(str: string, key: string, cache: {[string]: (buffer|{[string]: string})}?)
cache = cache or Remote.DecodeCache or {}
local keyCache = cache[key] or {}

if not key or not str then
return str
elseif cache[key] and cache[key][str] then
return cache[key][str]
elseif keyCache[str] then
return keyCache[str]
else
local keyCache = cache[key] or {}
local byte = string.byte
local sub = string.sub
local char = string.char
local endStr = {}
local writeu8, readu8 = buffer.writeu8, buffer.readu8
local rawStr, rawKey = buffer.fromstring(str), keyCache[1] or buffer.fromstring(key)
local keyLen = #key

for i = 1, #str do
local keyPos = (i % #key)+1
endStr[i] = char(((byte(sub(str, i, i)) - byte(sub(key, keyPos, keyPos)))%126) - 1)
local keyPos = (i % keyLen) + 1
writeu8(rawStr, (readu8(rawStr, i) - readu8(rawKey, keyPos)) % 126 - 1)
end

endStr = table.concat(endStr)
cache[key] = keyCache
keyCache[str] = endStr
return endStr
keyCache[str], keyCache[1] = buffer.tostring(rawStr), rawKey
return keyCache[str]
end
end;
};
Expand Down
Loading