Advertisement
ilovepants

XOR Encryption

Aug 7th, 2014
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.06 KB | None | 0 0
  1. local functions = {}
  2.  
  3. functions.xor = function(a, b)
  4.     return (a == b) and 0 or 1
  5. end
  6.  
  7. functions.chop = function(string, interval)
  8.     local components = {}
  9.  
  10.     while string:len() > 0 do
  11.         local component = (string:len() >= interval) and string:sub(1, interval) or string:sub(1)
  12.        
  13.         table.insert(components, component)
  14.         string = string:sub(interval + 1)
  15.     end
  16.  
  17.     return components
  18. end
  19.  
  20. functions.cipher = function(binary, key)
  21.     local components = functions.chop(binary, 256)
  22.     local result = {}
  23.  
  24.     for component = 1, #components do
  25.         for index = 1, components[component]:len() do
  26.             local currBin = tonumber(components[component]:sub(index, index))
  27.             local currKey = tonumber(key:sub(index, index))
  28.            
  29.             table.insert(result, functions.xor(currBin, currKey))
  30.         end
  31.     end
  32.  
  33.     return table.concat(result)
  34. end
  35.  
  36. functions.createKey = function(length)
  37.     math.randomseed(os.time())
  38.  
  39.     local key = {}
  40.  
  41.     for index = 1, length do
  42.         local bit = (math.random() < 0.5) and 0 or 1
  43.  
  44.         table.insert(key, bit)  
  45.     end
  46.  
  47.     return table.concat(key)
  48. end
  49.  
  50. return functions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement