Alakazard12

Bank client

Apr 27th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.32 KB | None | 0 0
  1. local BANK_CHANNEL = 1329
  2.  
  3.  
  4. local function convert(chars,dist,inv)
  5.     local charInt = string.byte(chars);
  6.     for i=1,dist do
  7.         if(inv)then charInt = charInt - 1; else charInt = charInt + 1; end
  8.         if(charInt<32)then
  9.             if(inv)then charInt = 126; else charInt = 126; end
  10.         elseif(charInt>126)then
  11.             if(inv)then charInt = 32; else charInt = 32; end
  12.         end
  13.     end
  14.     return string.char(charInt);
  15. end
  16.  
  17. local function crypt(str,k,inv)
  18.     local enc= "";
  19.     for i=1,#str do
  20.         if(#str-k[5] >= i or not inv)then
  21.             for inc=0,3 do
  22.                 if(i%4 == inc)then
  23.                     enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
  24.                     break;
  25.                 end
  26.             end
  27.         end
  28.     end
  29.     if(not inv)then
  30.         for i=1,k[5] do
  31.             enc = enc .. string.char(math.random(32,126));
  32.         end
  33.     end
  34.     return enc;
  35. end
  36.  
  37. local modem
  38.  
  39. local function getModem()
  40.     for i,v in pairs(peripheral.getNames()) do
  41.         if peripheral.getType(v) == "modem" then
  42.             modem = peripheral.wrap(v)
  43.             modem.open(BANK_CHANNEL)
  44.             break
  45.         end
  46.     end
  47. end
  48.  
  49. local myKey
  50.  
  51. local function sendMessage(text)
  52.     text = textutils.serialize(text)
  53.     print(text)
  54.     modem.transmit(BANK_CHANNEL, os.getComputerID(), crypt(text, myKey))
  55. end
  56.  
  57. local function receiveMessage()
  58.     local event, side, from, id, text
  59.     repeat
  60.         event, side, from, id, text = os.pullEvent("modem_message")
  61.         text = textutils.unserialize(crypt(text, myKey, true))
  62.     until from == BANK_CHANNEL and id == BANK_CHANNEL and type(text) == "table" and text[1] == os.getComputerID()
  63.     return text[2]
  64. end
  65.  
  66. function createUser(user, pass)
  67.     sendMessage({"createUser", user, pass})
  68.     local tab = receiveMessage()
  69.     if tab[1] == false then
  70.         return false, tab[2]
  71.     else
  72.         return true
  73.     end
  74. end
  75.  
  76. local function loadKey()
  77.     if fs.exists(".bankkey") then
  78.         local file = fs.open(".bankkey", "r")
  79.         myKey = textutils.unserialize(file.readAll())
  80.         file.close()
  81.     else
  82.         modem.transmit(BANK_CHANNEL, os.getComputerID(), "@!)#njsdf)(#84njd)")
  83.         local event, side, from, id, text
  84.         repeat
  85.             event, side, from, id, text = os.pullEvent("modem_message")
  86.         until from == BANK_CHANNEL and id == BANK_CHANNEL
  87.         myKey = textutils.unserialize(text)
  88.         local file = fs.open(".bankkey", "w")
  89.         file.write(textutils.serialize(myKey))
  90.         file.close()
  91.     end
  92. end
  93.  
  94. function load()
  95.     getModem()
  96.     if not modem then
  97.         error("No modem attached", 0)
  98.     end
  99.     loadKey()
  100. end
Add Comment
Please, Sign In to add comment