-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCaesar-Cipher.lua
More file actions
61 lines (60 loc) · 1.91 KB
/
Copy pathCaesar-Cipher.lua
File metadata and controls
61 lines (60 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
-- @file Class realize Caesar encryption metods.
-- @class caesarCipher
-- @author Full_droper <full_droper@pm.me>
-- @version 0.0.1
caesarCipher = {}
function caesarCipher:new()
local obj= {}
-- Encode input data by Caesar Cipher.
-- @name encode
-- @param {string} rawData - raw data for encode.
-- @param {number/string} key - (default 1) count of shift.
-- @return {string} The encoded by Caesar Cipher data
function obj:encode(rawData,key)
if rawData == nil and type(rawData) ~= "string" then
return nil
end
key = key or 1
if type(key) == "string" then
key = tonumber(key)
end
if key < 1 then
key = 1
end
local res = ""
rawData:gsub(".",function(c)
res = res .. string.char( string.byte(c) + key)
end)
return res
end
-- Decode input data by Caesar Cipher.
-- @name decode
-- @param {string} rawData - raw data for decode.
-- @param {number/string} key - (default 1) count of shift.
-- @return {string} The decoded by Caesar Cipher data
function obj:decode(rawData,key)
if rawData == nil and type(rawData) ~= "string" then
return nil
end
key = key or 1
if type(key) == "string" then
key = tonumber(key)
end
if key < 1 then
key = 1
end
local res = ""
rawData:gsub(".",function(c)
res = res .. string.char( string.byte(c) - key)
end)
return res
end
setmetatable(obj, self)
self.__index = self; return obj
end
--создаем экземпляр класса
local conv = caesarCipher:new()
local word = "hello word"
local str1 = conv.encode(nil,word, 2)
local str2 = conv.decode(nil,str1, 2)
print(word .. " -> " .. str1 .. " -> " .. str2)