Skip to content

Commit

Permalink
Add new encryption function (#1704)
Browse files Browse the repository at this point in the history
* Add new encryption function

* Add new encryption function

* Use new encryption algorithm

* Use new encryption algorithm

* Use new encryption algorithm

* Use new encryption algorithm

* Fix typo regarding index arg

* Fix typo regarding index arg
  • Loading branch information
ccuser44 authored Nov 12, 2024
1 parent 6108767 commit e6dd986
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
2 changes: 1 addition & 1 deletion MainModule/Client/Core/Process.luau
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ return function(Vargs, GetEnv)
client.Finish_Loading()
end
elseif Core.Key then
local comString = Remote.Decrypt(com,Core.Key)
local comString = Remote.NewDecrypt(com,Core.Key)
local command = (data.Mode == "Get" and Remote.Returnables[comString]) or Remote.Commands[comString]
if command then
local rets = {service.TrackTask(`Remote: {comString}`, command, false, args)}
Expand Down
32 changes: 30 additions & 2 deletions MainModule/Client/Core/Remote.luau
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ return function(Vargs, GetEnv)
Functions = client.Functions;
Process = client.Process;
Remote = client.Remote;
Remote.NewDecrypt = function(str, key, cache) -- Bxor works both ways. May want to make a seperate function tho
return Remote.NewEncrypt(str, key, cache or Remote.NewDecodeCache)
end;

Remote.Init = nil;
end
Expand Down Expand Up @@ -92,6 +95,8 @@ return function(Vargs, GetEnv)
PendingReturns = {};
EncodeCache = {};
DecodeCache = {};
NewEncodeCache = {};
NewDecodeCache = {};
Received = 0;
Sent = 0;

Expand Down Expand Up @@ -374,7 +379,7 @@ return function(Vargs, GetEnv)

Send = function(com,...)
Core.LastUpdate = os.time()
Remote.Fire(Remote.Encrypt(com,Core.Key),...)
Remote.Fire(Remote.NewEncrypt(com,Core.Key),...)
end;

GetFire = function(...)
Expand Down Expand Up @@ -429,7 +434,7 @@ return function(Vargs, GetEnv)

Get = function(com,...)
Core.LastUpdate = os.time()
local ret = Remote.GetFire(Remote.Encrypt(com,Core.Key),...)
local ret = Remote.GetFire(Remote.NewEncrypt(com,Core.Key),...)
if type(ret) == "table" then
return unpack(ret);
else
Expand Down Expand Up @@ -523,5 +528,28 @@ return function(Vargs, GetEnv)
return keyCache[str]
end
end;

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

if not key or not str then
return str
elseif keyCache[str] then
return keyCache[str]
else
local writeu8, readu8, bxor = buffer.writeu8, buffer.readu8, bit32.bxor
local rawStr, rawKey = buffer.fromstring(str), keyCache[1] or buffer.fromstring(key)
local keyLen = #key

for i = 0, #str - 1 do
writeu8(rawStr, i, bxor(readu8(rawStr, i), readu8(rawKey, i % keyLen)))
end

cache[key] = keyCache
keyCache[str], keyCache[1] = buffer.tostring(rawStr), rawKey
return keyCache[str]
end
end;
}
end
4 changes: 2 additions & 2 deletions MainModule/Server/Core/Process.luau
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ return function(Vargs, GetEnv)
logError = logError or env.logError;
Routine = Routine or env.Routine;
Commands = Remote.Commands
Decrypt = Remote.Decrypt
Encrypt = Remote.Encrypt
Decrypt = Remote.NewDecrypt
Encrypt = Remote.NewEncrypt
AddLog = Logs.AddLog
TrackTask = service.TrackTask
Pcall = server.Pcall
Expand Down
33 changes: 30 additions & 3 deletions MainModule/Server/Core/Remote.luau
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ return function(Vargs, GetEnv)
Settings = server.Settings;
Defaults = server.Defaults.Settings;
Commands = server.Commands;

logError = env.logError;
Remote.NewDecrypt = function(str, key, cache) -- Bxor works both ways. May want to make a seperate function tho
return Remote.NewEncrypt(str, key, cache or Remote.NewDecodeCache)
end;

Remote.Init = nil;
Logs:AddLog("Script", "Remote Module Initialized")
Expand Down Expand Up @@ -60,6 +62,8 @@ return function(Vargs, GetEnv)
PendingReturns = {};
EncodeCache = {};
DecodeCache = {};
NewEncodeCache = {};
NewDecodeCache = {};
RemoteExecutionConfirmed = {};

TimeUntilKeyDestroyed = 60 * 5; --// How long until a player's key data should be completely removed?
Expand Down Expand Up @@ -1140,7 +1144,7 @@ return function(Vargs, GetEnv)
assert(p and p:IsA("Player"), `Remote.Send: {p.Name} is not a valid Player`)
local keys = Remote.Clients[tostring(p.UserId)]
if keys and keys.RemoteReady == true then
Remote.Fire(p, Remote.Encrypt(com, keys.Key, keys.Cache), ...)
Remote.Fire(p, Remote.NewEncrypt(com, keys.Key, keys.Cache), ...)
end
end;

Expand All @@ -1156,7 +1160,7 @@ return function(Vargs, GetEnv)
Get = function(p: Player, com: string, ...)
local keys = Remote.Clients[tostring(p.UserId)]
if keys and keys.RemoteReady == true then
local ret = Remote.GetFire(p, Remote.Encrypt(com, keys.Key, keys.Cache), ...)
local ret = Remote.GetFire(p, Remote.NewEncrypt(com, keys.Key, keys.Cache), ...)
if type(ret) == "table" then
return unpack(ret)
else
Expand Down Expand Up @@ -1394,5 +1398,28 @@ return function(Vargs, GetEnv)
return keyCache[str]
end
end;

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

if not key or not str then
return str
elseif keyCache[str] then
return keyCache[str]
else
local writeu8, readu8, bxor = buffer.writeu8, buffer.readu8, bit32.bxor
local rawStr, rawKey = buffer.fromstring(str), keyCache[1] or buffer.fromstring(key)
local keyLen = #key

for i = 0, #str - 1 do
writeu8(rawStr, i, bxor(readu8(rawStr, i), readu8(rawKey, i % keyLen)))
end

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

0 comments on commit e6dd986

Please sign in to comment.