lieudusty

XOR Encryption

May 25th, 2013
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.80 KB | None | 0 0
  1. local tArgs = {...}
  2. if #tArgs < 2 then
  3.     print("Usage: "..shell.getRunningProgram().." [infile] <outfile> [key]")
  4.     return
  5. end
  6.  
  7. local infile, outfile, key
  8. if #tArgs == 2 then
  9.     infile, key = unpack(tArgs)
  10.     outfile = infile
  11. else
  12.     infile, outfile, key = unpack(tArgs)
  13. end
  14.  
  15. function convert(str, key)
  16.     local res = ""
  17.     for i = 1,#str do
  18.         local keyIndex = (i - 1) % key:len() + 1
  19.         res = res .. string.char( bit.bxor( str:sub(i,i):byte(), key:sub(keyIndex,keyIndex):byte() ) )
  20.     end
  21.  
  22.     return res
  23. end
  24.  
  25. local h = fs.open(infile, 'rb')
  26.  
  27. if h == nil then
  28.     error("No such file!")
  29. end
  30.  
  31. local str = ""
  32. for c in h.read do
  33.     str = str .. string.char(c)
  34. end
  35. h.close()
  36. local converted = convert(str, key)
  37. h = fs.open(outfile, 'wb')
  38. for c in converted:gmatch('.') do
  39.     h.write(c:byte())
  40. end
  41. h.close()
Advertisement
Add Comment
Please, Sign In to add comment