Advertisement
Deftry

Caesar lib - OpenComputers

Apr 14th, 2021
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.11 KB | None | 0 0
  1. local caesar = {}
  2.  
  3. function caesar.convert(Text,Action,Shift)
  4.  local function Check(C) --Checker For Check If Current Letters Is A-Z or a-z .
  5.  if string.byte(C) >= 65 and string.byte(C) <= 90 then
  6.    return "U" --string.upper
  7.  elseif string.byte(C) >= 97 and string.byte(C) <= 122 then
  8.   return "L" --string.lower
  9.  end
  10. end
  11. Shift = Shift%26
  12. if Shift == 0 then Shift = 26 end
  13. local Enc = function (a,b)
  14.   local Byte = (string.byte(a)-b+Shift)%26
  15.     if Byte == 0 then Byte = 26 end
  16.   return string.char(Byte+b)
  17. end
  18. local Dec = function (a,b)
  19.     local Byte = (string.byte(a)-b-Shift)%26
  20.     if Byte == 0 then Byte = 26 end
  21.   return string.char(Byte+b)
  22. end
  23. local E , Activity = ""
  24. if tostring(Action) == "Enc" then
  25. Activity = Enc
  26. elseif tostring(Action) == "Dec" then
  27. Activity = Dec
  28. else
  29. print("Error : Action Not Selected")
  30. os.exit()
  31. end
  32. for I = 1,#Text do
  33.   local C = string.sub(Text,I,I)
  34.   local Checked = Check(C)
  35.   if Checked == "U" then
  36.      E = E..Activity(C,64)
  37.  elseif Checked == "L" then
  38.            E = E..Activity(C,96)
  39.  else
  40.  E = E..C
  41.   end
  42.  end
  43.   return E
  44. end
  45.  
  46.  
  47.  
  48. return caesar
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement