Skip to content

Instantly share code, notes, and snippets.

Created April 9, 2014 18:17
Show Gist options
  • Save anonymous/10299240 to your computer and use it in GitHub Desktop.
Save anonymous/10299240 to your computer and use it in GitHub Desktop.
function Demo()
charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
local value = ""
math.randomseed(os.time())
for i = 1, math.random(1, 100) do
rand = math.random(#charset)
value = value.. string.sub(charset, rand, rand)
end
return value
end
local function caesar(input, shift)
input = input:gsub("%s", Demo())
return input:gsub("%a", function(x)
local test = (x:lower() == x and string.byte('a') or string.byte('A'))
local crypt = x:byte() - test
crypt = crypt + (shift)
crypt = crypt%26
crypt = crypt + test
return string.char(crypt)
end)
end
local function decrypt(input, shift)
local decrypt = caesar(input, -shift)
return (decrypt:gsub(Demo(), function(text) return " " end))
end
function read()
local f = io.open("test.txt", "r")
content = f:read("*all")
f:close()
return content
end
-- Test
do
local input = read()
io.write("Digite o shift: ")
local shift = io.read()
local e = caesar(input, shift)
local d = decrypt(e, shift)
local f = io.open("crypt.txt", "w")
local x = f:write(e)
f:close()
print("Texto Original: ", input) -- testing purposes
print("Texto criptografado: ", e) -- testing purposes
local fp = io.open("descrypt.txt", "w")
local z = fp:write(d)
fp:close()
print("Texto descriptografado: ", d) -- testing purposes
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment