In a project on ROBLOX, I am looking for a lightweight pure Lua cipher which can encrypt and decrypt a string fairly quickly using a key. I've been looking around, and most solutions require implementation of a C library, or are too heavy for what I'm looking for. I don't know too much about encryption, but it shouldn't be bluntly obvious, but not too complex. Is there a library out there that I'm somehow missing that would resolve this issue?
3 Answers
Here is an example of a lightweight pure Lua cipher.
It works on Lua 5.1, 5.2, 5.3 without external libraries.
do
-- This is your secret 67-bit key (any random bits are OK)
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
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
end
local s = 'Hello world'
print( encode(s) ) --> 80897dfa1dd85ec196bc84
print(decode(encode(s))) --> Hello world

- 23,359
- 2
- 34
- 64
-
These features serve exactly what I was looking for. Not too complex, but not too obvious. Thanks! – Reinitialized Mar 02 '16 at 02:31
-
[Version for Lua 5.0 with "float" Lua numbers (instead of "double")](https://pastebin.com/Q19edcu0) – Egor Skriptunoff Mar 15 '18 at 12:08
There happens to be a collection of pure Lua crypto programs at plc, however, these use Lua 5.3 whereas ROBLOX uses Lua 5.1.x.
The advantage of Lua 5.3 is that it includes bit manipulation operators. Prior to 5.3 Lua needed external Libraries to do bit manipulations. [Well, not really, there were pure Lua libraries to do bit manipulation using math
functions, e.g., lua-bit-numberlua, but Lua 5.3 is the first practical release for these types of programs.] Lua 5.3 also supports 64-bit integers, whereas previous versions only supported double precision floats (53 bit integers).
You can certainly implement encryption in Lua 5.1 using 32-bit integers. A simple substitution cipher would be the easiest place to start.
A more interesting and suitable cipher to implement might be solitaire

- 40,708
- 1
- 95
- 119
The best encryption is your own encryption. Try doing something unique and it'll be more difficult for someone to guess your algorithm.

- 3,045
- 4
- 20
- 37
-
Although possible, I lack basic understanding on how encryption works. My solution would be to convert everything using string.byte, and that would be it. Thanks for the suggestion, however I found a solution. – Reinitialized Mar 02 '16 at 16:26
-
1This couldn't be further from the truth. NEVER roll your own crypto if you care about security. – Joseph Sible-Reinstate Monica Oct 23 '21 at 20:12