Advertisement
Guest User

Untitled

a guest
Oct 13th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.74 KB | None | 0 0
  1. -- Migrate Encryption
  2. -- Decrypt Autorun
  3. local pass
  4.  
  5. -- Turns off debug so people can't steel mi password
  6. _G.debug = nil
  7.  
  8. -- Encryption API
  9. -- Nathan's Basic Encryption Library
  10. -- Encryption API
  11. -- Nathan's Basic Encryption Library
  12. local function encrypt(str, password, iter)
  13.   iter = iter or 0
  14.  
  15.   -- Make Seed
  16.   local seed = 0
  17.   local seedtable = {string.byte(password, 1, -1)}
  18.   math.randomseed(string.byte(password, -1) * string.byte(password, 1))
  19.  
  20.   for k,v in pairs(seedtable) do
  21.     math.random() math.random()
  22.     local operation = math.random(4)
  23.     if operation == 1 then
  24.       seed = seed + v
  25.     elseif operation == 2 then
  26.       seed = seed - v
  27.     elseif operation == 3 then
  28.       seed = seed * v
  29.     elseif operation == 4 then
  30.       seed = seed / v
  31.     end
  32.     seed = math.ceil(seed)
  33.   end
  34.  
  35.   -- Start Encryption Process
  36.   math.randomseed(seed)
  37.  
  38.   for i=1,iter do
  39.     math.random()
  40.   end
  41.  
  42.   local cipher = ""
  43.   for i=1,#str do
  44.     if string.byte(str, i) == 10 then
  45.       cipher = cipher..str:sub(i,i)
  46.     else
  47.       local randseed = seedtable[math.random(#seedtable)]
  48.    
  49.       while string.byte(str, i)+randseed == 10 or string.byte(str, i)-randseed == 10 do
  50.         randseed = seedtable[math.random(#seedtable)]
  51.       end
  52.    
  53.       if string.byte(str, i)+randseed > 255 then
  54.         randseed = -randseed
  55.       end
  56.         cipher = cipher..string.char(string.byte(str, i)+randseed)
  57.     end
  58.   end
  59.  
  60.   return cipher
  61. end
  62.  
  63. local function decrypt(cipher, password, iter)
  64.   iter = iter or 0
  65.  
  66.     -- Check for args
  67.   if cipher == nil then
  68.     return nil
  69.   end
  70.  
  71.   -- Make Seed
  72.   local seed = 0
  73.   local seedtable = {string.byte(password, 1, -1)}
  74.   math.randomseed(string.byte(password, -1) * string.byte(password, 1))
  75.  
  76.   for k,v in pairs(seedtable) do
  77.     math.random() math.random()
  78.     local operation = math.random(4)
  79.     if operation == 1 then
  80.       seed = seed + v
  81.     elseif operation == 2 then
  82.       seed = seed - v
  83.     elseif operation == 3 then
  84.       seed = seed * v
  85.     elseif operation == 4 then
  86.       seed = seed / v
  87.     end
  88.     seed = math.ceil(seed)
  89.   end
  90.  
  91.   -- Start Encryption Process
  92.   math.randomseed(seed)
  93.  
  94.   for i=1,iter do
  95.     math.random()
  96.   end
  97.  
  98.   local str = ""
  99.   for i=1,#cipher do
  100.     if string.byte(cipher, i) == 10 then
  101.       str = str..cipher:sub(i,i)
  102.     else
  103.       local randseed = seedtable[math.random(#seedtable)]
  104.    
  105.       while string.byte(cipher, i)+randseed == 10 or string.byte(cipher, i)-randseed == 10 do
  106.         randseed = seedtable[math.random(#seedtable)]
  107.       end
  108.    
  109.       if string.byte(cipher, i)-randseed < 0 then
  110.         randseed = -randseed
  111.       end
  112.         str = str..string.char(string.byte(cipher, i)-randseed)
  113.     end
  114.   end
  115.  
  116.   return str
  117. end
  118.  
  119. print("Testing Encryption System")
  120. local success, returned = pcall(function() return decrypt(encrypt("hellowurld",string.char(128)..string.char(0)), string.char(128)..string.char(0)) end)
  121. if not success or returned ~= "hellowurld" then error("Test Failed: "..returned) else print("Success!") end
  122.  
  123. print("Enter key: ")
  124. pass = read("*")
  125.  
  126. print("Rewriting fs API...")
  127.  
  128. -- Set up encryption
  129. local oldfsopen = fs.open
  130. local thisname = shell.getRunningProgram()
  131.  
  132. _G.fs.open = function(dir, mode)
  133.  
  134.   if dir and (dir:sub(1,5) == "/rom/" or dir:sub(1,4) == "rom/" or dir:sub(1,4) == "disk" or dir:sub(1,5) == "/disk" or thisname == dir) then
  135.     return oldfsopen(dir, mode)
  136.   end
  137.  
  138.   local fopen = oldfsopen(dir, mode)
  139.   if not fopen then return nil end
  140.  
  141.   local charIter = 0
  142.  
  143.   local returntable = {}
  144.  
  145.     returntable["readLine"] = function()
  146.       local results = fopen.readLine()
  147.       local dresult =  decrypt(results, pass, charIter)
  148.       if dresult then
  149.         charIter = charIter+#dresult+1
  150.       end
  151.       return dresult
  152.     end
  153.  
  154.     returntable.readAll = function()
  155.       local results
  156.       local dresult = ""
  157.       repeat
  158.         results = fopen.readLine()
  159.         local rawresult = decrypt(results, pass, charIter)
  160.         if rawresult then
  161.           charIter = charIter+#rawresult+1
  162.           dresult = dresult..rawresult.."\n"
  163.         end
  164.       until not results
  165.      
  166.       return dresult
  167.     end
  168.  
  169.     returntable["write"] = function(data)
  170.         -- Detect bunary mode
  171.       if type(data) == "number" then
  172.         data = string.char(data)
  173.       end
  174.       local dresult =  fopen.write(encrypt(data, pass, charIter))
  175.       if data then
  176.         charIter = charIter+#data
  177.       end
  178.       return dresult
  179.     end
  180.    
  181.     returntable["writeLine"] = function(data)
  182.       local dresult =  fopen.writeLine(encrypt(data, pass, charIter))
  183.       if data then
  184.         charIter = charIter+#data+1
  185.       end
  186.       return dresult
  187.     end
  188.  
  189.     returntable["flush"] = fopen.flush
  190.  
  191.     returntable["read"] = function()
  192.       local dresult =  string.byte(decrypt(string.char(fopen.read()), pass, charIter))
  193.       charIter = charIter+1
  194.       return dresult
  195.     end
  196.  
  197.   -- Essential Functions
  198.   returntable["close"] = fopen.close
  199.  
  200.   return returntable
  201. end
  202.  
  203. print("Done!")
  204.  
  205. print("Migrating files in root...")
  206.  
  207. function scanAllFiles(dir, r)
  208.   local files = {}
  209.   for _,v in pairs(fs.list(dir)) do
  210.     if fs.isDir(v) then
  211.       if v:sub(1,3) ~= "rom" and v:sub(1,7) ~= "startup" and r then
  212.         local subfiles = scanAllFiles(v,r)
  213.         for _,v1 in pairs(subfiles) do
  214.           table.insert(files,v.."/"..v1)
  215.         end
  216.       end
  217.     else
  218.       table.insert(files, v)
  219.     end
  220.   end
  221.   return files
  222. end
  223.  
  224. local files = scanAllFiles("", true)
  225. for _,v in pairs(files) do
  226.   local f = oldfsopen(v, "r")
  227.   local migrate = encrypt(f.readAll(), pass)
  228.   f.close()
  229.  
  230.   local f1 = oldfsopen(v, "w")
  231.   f1.write(migrate)
  232.   f1.close()
  233. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement