Advertisement
killer64

disk encrypt

Oct 16th, 2012
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.47 KB | None | 0 0
  1. -- OPTIONS
  2.  
  3. local encrhandle="encrypted"
  4. local chkshandle="encrypted_password"
  5.  
  6. -- END OPTIONS
  7.  
  8. local function serializeImpl(t)
  9.     local sType = type(t)
  10.     if sType == "table" then
  11.         local lstcnt=0
  12.         for k,v in pairs(t) do
  13.             lstcnt = lstcnt + 1
  14.         end
  15.         local result = "{"
  16.         local aset=1
  17.         for k,v in pairs(t) do
  18.             if k==aset then
  19.                 result = result..serializeImpl(v)..","
  20.                 aset=aset+1
  21.             else
  22.                 result = result..("["..serializeImpl(k).."]="..serializeImpl(v)..",")
  23.             end
  24.         end
  25.         result = result.."}"
  26.         return result
  27.     elseif sType == "string" then
  28.         return string.gsub(string.gsub(string.format("%q",t),"\\\n","\\n"),"\n","\\n")
  29.     elseif sType == "number" or sType == "boolean" or sType == "nil" then
  30.         return tostring(t)
  31.     elseif sType == "function" then
  32.         local status,data=pcall(string.dump,t)
  33.         if status then
  34.             return 'func('..string.format("%q",data)..')'
  35.         else
  36.             error()
  37.         end
  38.     else
  39.         error()
  40.     end
  41. end
  42.  
  43. function split(T,func)
  44.     if func then
  45.         T=func(T)
  46.     end
  47.     local Out={}
  48.     if type(T)=="table" then
  49.         for k,v in pairs(T) do
  50.             Out[split(k)]=split(v)
  51.         end
  52.     else
  53.         Out=T
  54.     end
  55.     return Out
  56. end
  57.  
  58. local function serialize( t )
  59.     t=split(t)
  60.     return serializeImpl( t, tTracking )
  61. end
  62.  
  63. local function unserialize( s )
  64.     local func, e = loadstring( "return "..s, "serialize" )
  65.     local funcs={}
  66.     if not func then
  67.         return e
  68.     end
  69.     setfenv( func, {
  70.         func=function(S)
  71.             local new={}
  72.             funcs[new]=S
  73.             return new
  74.         end,
  75.     })
  76.     return split(func(),function(val)
  77.         if funcs[val] then
  78.             return loadstring(funcs[val])
  79.         else
  80.             return val
  81.         end
  82.     end)
  83. end
  84.  
  85. local function textbox(bartext)
  86.     term.setCursorBlink( true )
  87.     local sLine = ""
  88.     local nPos = 0
  89.     local oW, oH = term.getSize()
  90.     local w,h=oW-4,math.floor(oH/2)
  91.     local sx,sy=4,math.floor(oH/2) 
  92.     local function redraw()
  93.         local nScroll = 0
  94.         if sx + nPos >= w then
  95.             nScroll = (sx + nPos) - w
  96.         end
  97.         term.setCursorPos(3,math.floor(oH/2)-1)
  98.         write(string.rep("-",oW-5))
  99.         term.setCursorPos(3,math.floor(oH/2)+1)
  100.         write(string.rep("-",oW-5))
  101.         term.setCursorPos(math.floor(oW/2)-math.floor(#bartext/2),math.floor(oH/2)-1)
  102.         write(bartext)
  103.         term.setCursorPos( sx, sy )
  104.         term.write(term.write( string.rep(" ", w - sx + 1) ))
  105.         term.setCursorPos( sx, sy )
  106.         term.write(term.write( string.sub( sLine, nScroll + 1 ) ))
  107.         term.setCursorPos(3,math.floor(oH/2))
  108.         write("|")
  109.         term.setCursorPos(oW-3,math.floor(oH/2))
  110.         write("|")
  111.         term.setCursorPos( sx + nPos - nScroll, sy )
  112.     end
  113.     redraw()
  114.     while true do
  115.         local sEvent, param = os.pullEvent()
  116.         if sEvent == "char" then
  117.             sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  118.             nPos = nPos + 1
  119.             redraw()
  120.         elseif sEvent == "key" then
  121.             if param == 28 then
  122.                 break
  123.             elseif param == 203 then
  124.                 if nPos > 0 then
  125.                     nPos = nPos - 1
  126.                     redraw()
  127.                 end
  128.             elseif param == 205 then           
  129.                 if nPos < string.len(sLine) then
  130.                     nPos = nPos + 1
  131.                     redraw()
  132.                 end
  133.             elseif param == 200 or param == 208 then
  134.                 if _tHistory then
  135.                     if param == 200 then
  136.                         if nHistoryPos == nil then
  137.                             if #_tHistory > 0 then
  138.                                 nHistoryPos = #_tHistory
  139.                             end
  140.                         elseif nHistoryPos > 1 then
  141.                             nHistoryPos = nHistoryPos - 1
  142.                         end
  143.                     else
  144.                         if nHistoryPos == #_tHistory then
  145.                             nHistoryPos = nil
  146.                         elseif nHistoryPos ~= nil then
  147.                             nHistoryPos = nHistoryPos + 1
  148.                         end                    
  149.                     end
  150.                     if nHistoryPos then
  151.                         sLine = _tHistory[nHistoryPos]
  152.                         nPos = string.len( sLine )
  153.                     else
  154.                         sLine = ""
  155.                         nPos = 0
  156.                     end
  157.                     redraw()
  158.                 end
  159.             elseif param == 14 then
  160.                 if nPos > 0 then
  161.                     sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  162.                     nPos = nPos - 1                
  163.                     redraw()
  164.                 end
  165.             elseif param == 29 then
  166.                 term.setCursorBlink( false )
  167.                 return nil
  168.             end
  169.         end
  170.     end
  171.     term.setCursorBlink( false )
  172.     term.setCursorPos( w + 1, sy )
  173.     return sLine
  174. end
  175. local encryptapi="apis/enc"
  176. if not enc then
  177.     if not fs.exists(encryptapi) then
  178.         local web_response = http.get("http://pastebin.com/raw.php?i=WRTfH0yx")
  179.         if not fs.exists("apis") then
  180.             fs.makeDir("apis")
  181.         end
  182.         local akmn = io.open(encryptapi, "w")
  183.         akmn:write(web_response:readAll())
  184.         akmn:close()
  185.     end
  186.     os.loadAPI(encryptapi)
  187. end
  188. local file=fs.open(shell.getRunningProgram(),"r")
  189. local compiled=file.readAll()
  190. file:close()
  191.  
  192.  
  193. local function clear()
  194.     term.clear()
  195.     term.setCursorPos(1,1)
  196. end
  197.  
  198.  
  199. clear()
  200. if fs.exists(encrhandle) then
  201.     os.pullEvent=os.pullEventRaw
  202.     local file=fs.open(chkshandle,"r")
  203.     local chk=file.readAll()
  204.     file.close()
  205.     local pass=textbox("Password required")
  206.     if not pass then
  207.         os.shutdown()
  208.     end
  209.     if enc.checksum(pass,1000)~=tonumber(chk) then
  210.         clear()
  211.         print("Incorrect password.")
  212.         sleep(1)
  213.         os.reboot()
  214.     end
  215.     local file=fs.open(encrhandle,"r")
  216.     local data=file.readAll()
  217.     file.close()
  218.     fs.delete(encrhandle)
  219.     local data=enc.decrypt(data,pass)
  220.     if shell.getRunningProgram()=="startup" then
  221.         fs.delete("startup")
  222.     end
  223.     for k,v in pairs(data) do
  224.         if v then
  225.             file=io.open(k,"w")
  226.             file:write(v)
  227.             file:close()
  228.         else
  229.             fs.makeDir(k)
  230.         end
  231.         sleep(0)
  232.     end
  233.     os.reboot()
  234. else
  235.     local pass
  236.     if not fs.exists(chkshandle) then
  237.         pass=textbox("Password to encrypt")
  238.         if not pass then
  239.             clear()
  240.             error()
  241.         end
  242.         local file=io.open(chkshandle,"w")
  243.         file:write(pass)
  244.         file:close()
  245.     end
  246.     if not pass then
  247.         local file=fs.open(chkshandle,"r")
  248.         pass=file.readAll()
  249.         file.close()
  250.     end
  251.     local data={}
  252.     local pending={"/"}
  253.     clear()
  254.     print("packing...")
  255.     while true do
  256.         if not pending[1] then
  257.             break
  258.         end
  259.         local file=pending[1]
  260.         print(file)
  261.         table.remove(pending,1)
  262.         if fs.isReadOnly(file) and not (file=="disk" or file==encryptapi) then
  263.         elseif fs.isDir(file) then
  264.             data[file]=false
  265.             for k,v in pairs(fs.list(file)) do
  266.                 table.insert(pending,fs.combine(file,v))
  267.             end
  268.         else
  269.             local file2=fs.open(file,"r")
  270.             data[file]=file2.readAll()
  271.             file2.close()
  272.             fs.delete(file)
  273.         end
  274.         sleep(0)
  275.     end
  276.     local file=io.open(encrhandle,"w")
  277.     file:write(enc.encrypt(data,pass))
  278.     file:close()
  279.     pass=enc.checksum(pass,1000)
  280.     local file=io.open(chkshandle,"w")
  281.     file:write(pass)
  282.     file:close()
  283.     local file=io.open("startup","w")
  284.     file:write(compiled)
  285.     file:close()
  286.     local file=io.open(chkshandle,"w")
  287.     file:write(pass)
  288.     file:close()
  289.     print("done!")
  290.     sleep(1)
  291.     os.shutdown()
  292. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement