Guest User

Security Card API

a guest
Apr 12th, 2013
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.77 KB | None | 0 0
  1. --Security Card API
  2.  
  3.  local password = "Password for Encryption"
  4.  --You could change this per computer or have it the same...
  5. --up to you really I would save it somewhere on the computer encrypted
  6. --so a password is needed when the computer boots up to let anyone with
  7. --the card through but that might be awkward in multiplayer
  8.  
  9.  
  10. function encrypt(msg,pass)
  11.     -- msg= (32-126)
  12.     local str =""
  13.     local key = 0
  14.     msg= msg:reverse()
  15.     for  x = 1, #pass do
  16.         key = key + pass:byte(x)
  17.     end
  18.     key = (key+2)%50
  19.     for x = 1, #msg do
  20.         local ch = string.byte(msg,x)
  21.         ch = ch - key
  22.         if(ch<32)then
  23.             ch = 126 - (32-ch)
  24.         end
  25.         ch = string.char(ch)
  26.         str = str..ch
  27.     end
  28.     return str
  29. end
  30.  
  31. function decrypt(msg,pass)
  32.     -- msg= (32-126)
  33.     local str =""
  34.     local key = 0
  35.     for  x = 1, #pass do
  36.         key = key + pass:byte(x)
  37.     end
  38.     key = (key+2)%50
  39.     for x = 1, #msg do
  40.         local ch = 0
  41.         local t = 0
  42.         ch = string.byte(msg,x)+key
  43.         if(ch>126)then
  44.             ch = 32 + (ch-126)
  45.         end
  46.         ch = string.char(ch)
  47.         str = str..ch
  48.     end
  49.     str= str:reverse()
  50.     return str
  51. end
  52.  
  53. function setDiskLevel(side, lvl1, lvl2, psw)
  54.     if fs.exists("disk/level") then
  55.         fs.delete("disk/level")
  56.     end
  57.  
  58.     local h = fs.open("disk/level", "w")
  59.     h.writeLine("level1:"..encrypt(lvl1,password))
  60.     h.writeLine("level2:"..encrypt(lvl2,password))
  61.     h.writeLine("Password:"..encrypt(psw,password))
  62.     h.close()
  63.     disk.setLabel(side, "Level: "..lvl1.."-"..lvl2.." Card")
  64. end
  65.  
  66. function getDiskLevel(side)
  67. if fs.exists("disk/level") then
  68.     local h = fs.open("disk/level", "r")
  69.     local lvl1 = decrypt(string.sub(h.readLine(), 8),password)
  70.     local lvl2 = decrypt(string.sub(h.readLine(), 8),password)
  71.     local psw = decrypt(string.sub(h.readLine(), 10),password)
  72.     h.close()
  73.     return lvl1, lvl2, psw
  74. else
  75.     return false
  76. end
  77. end
Advertisement
Add Comment
Please, Sign In to add comment