doublequestionmark

CC-EncryptFS

May 14th, 2016
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.52 KB | None | 0 0
  1. local MakeStartup = false --Makes the EncryptFS Primer run at startup. After priming EncryptFS 'start' will be ran as the secondary startup file
  2. local background  = colors.white --Sets the background color
  3. local text        = colors.black   --Sets the text color
  4. local afterBG    = colors.black -- sets the background color after EncryptFS finishes
  5. local aftertext  = colors.white -- sets the text color after EncryptFS finishes
  6.  
  7. --Do not modify below
  8.  
  9. local checkstring = "TheQuickBrownFoxJumpedOverTheLazyDog" -- i just wanted to practice my typing
  10. local password = "" --Populated when user enters correct password on boot
  11.  
  12.  
  13. --SquidDev's Implementation of aes is as follows
  14.  
  15.  
  16. local function e(n)
  17. local s=setmetatable({},{__index=_ENV or getfenv()})if setfenv then setfenv(n,s)end;return n(s)or s end
  18. local t=e(function(n,...)local s=math.floor;local h,r
  19. r=function(d,l)return s(d%4294967296/2^l)end
  20. h=function(d,l)return(d*2^l)%4294967296 end
  21. return{bnot=bit.bnot,band=bit.band,bor=bit.bor,bxor=bit.bxor,rshift=r,lshift=h}end)
  22. local a=e(function(n,...)local s=t.bxor;local h=t.lshift;local r=0x100;local d=0xff;local l=0x11b;local u={}local c={}
  23. local function m(k,q)return s(k,q)end;local function f(k,q)return s(k,q)end
  24. local function w(k)if(k==1)then return 1 end;local q=d-c[k]return u[q]end;local function y(k,q)if(k==0 or q==0)then return 0 end;local j=c[k]+c[q]
  25. if(j>=d)then j=j-d end;return u[j]end
  26. local function p(k,q)
  27. if(k==0)then return 0 end;local j=c[k]-c[q]if(j<0)then j=j+d end;return u[j]end
  28. local function v()for k=1,r do print("log(",k-1,")=",c[k-1])end end
  29. local function b()for k=1,r do print("exp(",k-1,")=",u[k-1])end end;local function g()local k=1
  30. for q=0,d-1 do u[q]=k;c[k]=q;k=s(h(k,1),k)if k>d then k=f(k,l)end end end;g()return
  31. {add=m,sub=f,invert=w,mul=y,div=dib,printLog=v,printExp=b}end)
  32. util=e(function(n,...)local s=t.bxor;local h=t.rshift;local r=t.band;local d=t.lshift;local l;local function u(O)O=s(O,h(O,4))
  33. O=s(O,h(O,2))O=s(O,h(O,1))return r(O,1)end
  34. local function c(O,I)if(I==0)then return
  35. r(O,0xff)else return r(h(O,I*8),0xff)end end;local function m(O,I)
  36. if(I==0)then return r(O,0xff)else return d(r(O,0xff),I*8)end end
  37. local function f(O,I,N)local S={}
  38. for H=0,N-1 do
  39. S[H+1]=
  40. m(O[I+ (H*4)],3)+m(O[I+ (H*4)+1],2)+
  41. m(O[I+ (H*4)+2],1)+m(O[I+ (H*4)+3],0)if N%10000 ==0 then l()end end;return S end;local function w(O,I,N,S)S=S or#O;for H=0,S-1 do
  42. for R=0,3 do I[N+H*4+ (3-R)]=c(O[H+1],R)end;if S%10000 ==0 then l()end end
  43. return I end;local function y(O)local I=""
  44. for N,S in
  45. ipairs(O)do I=I..string.format("%02x ",S)end;return I end
  46. local function p(O)local I={}for N=1,#O,2 do I[#I+1]=tonumber(O:sub(N,
  47. N+1),16)end;return I end
  48. local function v(O)local I=type(O)
  49. if(I=="number")then return string.format("%08x",O)elseif
  50. (I=="table")then return y(O)elseif(I=="string")then local N={string.byte(O,1,#O)}
  51. return y(N)else return O end end
  52. local function b(O)local I=#O;local N=math.random(0,255)local S=math.random(0,255)
  53. local H=string.char(N,S,N,S,c(I,3),c(I,2),c(I,1),c(I,0))O=H..O;local R=math.ceil(#O/16)*16-#O;local D=""for L=1,R do D=D..
  54. string.char(math.random(0,255))end;return O..D end
  55. local function g(O)local I={string.byte(O,1,4)}if
  56. (I[1]==I[3]and I[2]==I[4])then return true end;return false end
  57. local function k(O)if(not g(O))then return nil end
  58. local I=
  59. m(string.byte(O,5),3)+
  60. m(string.byte(O,6),2)+m(string.byte(O,7),1)+m(string.byte(O,8),0)return string.sub(O,9,8+I)end;local function q(O,I)for N=1,16 do O[N]=s(O[N],I[N])end end
  61. local function j(O)
  62. local I=16;while true do local N=O[I]+1
  63. if N>=256 then O[I]=N-256;I=(I-2)%16+1 else O[I]=N;break end end end;local x,z,_=os.queueEvent,coroutine.yield,os.time;local E=_()
  64. local function l()local O=_()if O-E>=0.03 then E=O
  65. x("sleep")z("sleep")end end
  66. local function T(O)local I,N,S,H=string.char,math.random,l,table.insert;local R={}for D=1,O do
  67. H(R,N(0,255))if D%10240 ==0 then S()end end;return R end
  68. local function A(O)local I,N,S,H=string.char,math.random,l,table.insert;local R={}for D=1,O do
  69. H(R,I(N(0,255)))if D%10240 ==0 then S()end end
  70. return table.concat(R)end
  71. return
  72. {byteParity=u,getByte=c,putByte=m,bytesToInts=f,intsToBytes=w,bytesToHex=y,hexToBytes=p,toHexString=v,padByteString=b,properlyDecrypted=g,unpadByteString=k,xorIV=q,increment=j,sleepCheckIn=l,getRandomData=T,getRandomString=A}end)
  73. aes=e(function(n,...)local s=util.putByte;local h=util.getByte;local r='rounds'local d="type"local l=1;local u=2
  74. local c={}local m={}local f={}local w={}local y={}local p={}local v={}local b={}local g={}local k={}
  75. local q={0x01000000,0x02000000,0x04000000,0x08000000,0x10000000,0x20000000,0x40000000,0x80000000,0x1b000000,0x36000000,0x6c000000,0xd8000000,0xab000000,0x4d000000,0x9a000000,0x2f000000}
  76. local function j(M)mask=0xf8;result=0
  77. for F=1,8 do result=t.lshift(result,1)
  78. parity=util.byteParity(t.band(M,mask))result=result+parity;lastbit=t.band(mask,1)
  79. mask=t.band(t.rshift(mask,1),0xff)
  80. if(lastbit~=0)then mask=t.bor(mask,0x80)else mask=t.band(mask,0x7f)end end;return t.bxor(result,0x63)end;local function x()
  81. for M=0,255 do if(M~=0)then inverse=a.invert(M)else inverse=M end
  82. mapped=j(inverse)c[M]=mapped;m[mapped]=M end end
  83. local function z()
  84. for M=0,255 do
  85. byte=c[M]
  86. f[M]=
  87. s(a.mul(0x03,byte),0)+s(byte,1)+s(byte,2)+s(a.mul(0x02,byte),3)w[M]=s(byte,0)+s(byte,1)+s(a.mul(0x02,byte),2)+
  88. s(a.mul(0x03,byte),3)y[M]=
  89.  
  90. s(byte,0)+s(a.mul(0x02,byte),1)+s(a.mul(0x03,byte),2)+s(byte,3)p[M]=
  91.  
  92. s(a.mul(0x02,byte),0)+s(a.mul(0x03,byte),1)+s(byte,2)+s(byte,3)end end
  93. local function _()
  94. for M=0,255 do byte=m[M]
  95. v[M]=
  96. s(a.mul(0x0b,byte),0)+s(a.mul(0x0d,byte),1)+s(a.mul(0x09,byte),2)+
  97. s(a.mul(0x0e,byte),3)
  98. b[M]=
  99. s(a.mul(0x0d,byte),0)+s(a.mul(0x09,byte),1)+s(a.mul(0x0e,byte),2)+
  100. s(a.mul(0x0b,byte),3)
  101. g[M]=
  102. s(a.mul(0x09,byte),0)+s(a.mul(0x0e,byte),1)+s(a.mul(0x0b,byte),2)+
  103. s(a.mul(0x0d,byte),3)
  104. k[M]=
  105. s(a.mul(0x0e,byte),0)+s(a.mul(0x0b,byte),1)+s(a.mul(0x0d,byte),2)+
  106. s(a.mul(0x09,byte),3)end end;local function E(M)local F=t.band(M,0xff000000)return
  107. (t.lshift(M,8)+t.rshift(F,24))end
  108. local function T(M)return
  109. s(c[h(M,0)],0)+s(c[h(M,1)],1)+s(c[h(M,2)],2)+
  110. s(c[h(M,3)],3)end
  111. local function A(M)local F={}local W=math.floor(#M/4)if(
  112. (W~=4 and W~=6 and W~=8)or(W*4 ~=#M))then
  113. error("Invalid key size: "..tostring(W))return nil end;F[r]=W+6;F[d]=l;for Y=0,W-1
  114. do
  115. F[Y]=
  116. s(M[Y*4+1],3)+s(M[Y*4+2],2)+s(M[Y*4+3],1)+s(M[Y*4+4],0)end
  117. for Y=W,(F[r]+1)*4-1 do
  118. local P=F[Y-1]
  119. if(Y%W==0)then P=E(P)P=T(P)local V=math.floor(Y/W)P=t.bxor(P,q[V])elseif(
  120. W>6 and Y%W==4)then P=T(P)end;F[Y]=t.bxor(F[(Y-W)],P)end;return F end
  121. local function O(M)local F=h(M,3)local W=h(M,2)local Y=h(M,1)local P=h(M,0)
  122. return
  123.  
  124.  
  125.  
  126. s(a.add(a.add(a.add(a.mul(0x0b,W),a.mul(0x0d,Y)),a.mul(0x09,P)),a.mul(0x0e,F)),3)+
  127. s(a.add(a.add(a.add(a.mul(0x0b,Y),a.mul(0x0d,P)),a.mul(0x09,F)),a.mul(0x0e,W)),2)+
  128. s(a.add(a.add(a.add(a.mul(0x0b,P),a.mul(0x0d,F)),a.mul(0x09,W)),a.mul(0x0e,Y)),1)+
  129. s(a.add(a.add(a.add(a.mul(0x0b,F),a.mul(0x0d,W)),a.mul(0x09,Y)),a.mul(0x0e,P)),0)end
  130. local function I(M)local F=h(M,3)local W=h(M,2)local Y=h(M,1)local P=h(M,0)local V=t.bxor(P,Y)
  131. local B=t.bxor(W,F)local G=t.bxor(V,B)G=t.bxor(G,a.mul(0x08,G))
  132. w=t.bxor(G,a.mul(0x04,t.bxor(Y,F)))G=t.bxor(G,a.mul(0x04,t.bxor(P,W)))
  133. return
  134.  
  135.  
  136. s(t.bxor(t.bxor(P,G),a.mul(0x02,t.bxor(F,P))),0)+s(t.bxor(t.bxor(Y,w),a.mul(0x02,V)),1)+
  137. s(t.bxor(t.bxor(W,G),a.mul(0x02,t.bxor(F,P))),2)+
  138. s(t.bxor(t.bxor(F,w),a.mul(0x02,B)),3)end
  139. local function N(M)local F=A(M)if(F==nil)then return nil end;F[d]=u;for W=4,(F[r]+1)*4-5 do
  140. F[W]=O(F[W])end;return F end;local function S(M,F,W)
  141. for Y=0,3 do M[Y+1]=t.bxor(M[Y+1],F[W*4+Y])end end
  142. local function H(M,F)
  143. F[1]=t.bxor(t.bxor(t.bxor(f[h(M[1],3)],w[h(M[2],2)]),y[h(M[3],1)]),p[h(M[4],0)])
  144. F[2]=t.bxor(t.bxor(t.bxor(f[h(M[2],3)],w[h(M[3],2)]),y[h(M[4],1)]),p[h(M[1],0)])
  145. F[3]=t.bxor(t.bxor(t.bxor(f[h(M[3],3)],w[h(M[4],2)]),y[h(M[1],1)]),p[h(M[2],0)])
  146. F[4]=t.bxor(t.bxor(t.bxor(f[h(M[4],3)],w[h(M[1],2)]),y[h(M[2],1)]),p[h(M[3],0)])end
  147. local function R(M,F)
  148. F[1]=s(c[h(M[1],3)],3)+s(c[h(M[2],2)],2)+
  149. s(c[h(M[3],1)],1)+s(c[h(M[4],0)],0)
  150. F[2]=s(c[h(M[2],3)],3)+s(c[h(M[3],2)],2)+
  151. s(c[h(M[4],1)],1)+s(c[h(M[1],0)],0)
  152. F[3]=s(c[h(M[3],3)],3)+s(c[h(M[4],2)],2)+
  153. s(c[h(M[1],1)],1)+s(c[h(M[2],0)],0)
  154. F[4]=s(c[h(M[4],3)],3)+s(c[h(M[1],2)],2)+
  155. s(c[h(M[2],1)],1)+s(c[h(M[3],0)],0)end
  156. local function D(M,F)
  157. F[1]=t.bxor(t.bxor(t.bxor(v[h(M[1],3)],b[h(M[4],2)]),g[h(M[3],1)]),k[h(M[2],0)])
  158. F[2]=t.bxor(t.bxor(t.bxor(v[h(M[2],3)],b[h(M[1],2)]),g[h(M[4],1)]),k[h(M[3],0)])
  159. F[3]=t.bxor(t.bxor(t.bxor(v[h(M[3],3)],b[h(M[2],2)]),g[h(M[1],1)]),k[h(M[4],0)])
  160. F[4]=t.bxor(t.bxor(t.bxor(v[h(M[4],3)],b[h(M[3],2)]),g[h(M[2],1)]),k[h(M[1],0)])end
  161. local function L(M,F)
  162. F[1]=s(m[h(M[1],3)],3)+s(m[h(M[4],2)],2)+
  163. s(m[h(M[3],1)],1)+s(m[h(M[2],0)],0)
  164. F[2]=s(m[h(M[2],3)],3)+s(m[h(M[1],2)],2)+
  165. s(m[h(M[4],1)],1)+s(m[h(M[3],0)],0)
  166. F[3]=s(m[h(M[3],3)],3)+s(m[h(M[2],2)],2)+
  167. s(m[h(M[1],1)],1)+s(m[h(M[4],0)],0)
  168. F[4]=s(m[h(M[4],3)],3)+s(m[h(M[3],2)],2)+
  169. s(m[h(M[2],1)],1)+s(m[h(M[1],0)],0)end
  170. local function U(M,F,W,Y,P)W=W or 1;Y=Y or{}P=P or 1;local V={}local B={}if(M[d]~=l)then
  171. error("No encryption key: "..
  172. tostring(M[d])..", expected "..l)return end
  173. V=util.bytesToInts(F,W,4)S(V,M,0)local G=1;while(G<M[r]-1)do H(V,B)S(B,M,G)G=G+1;H(B,V)S(V,M,G)
  174. G=G+1 end;H(V,B)S(B,M,G)G=G+1;R(B,V)S(V,M,G)
  175. util.sleepCheckIn()return util.intsToBytes(V,Y,P)end
  176. local function C(M,F,W,Y,P)W=W or 1;Y=Y or{}P=P or 1;local V={}local B={}
  177. if(M[d]~=u)then error("No decryption key: "..
  178. tostring(M[d]))return end;V=util.bytesToInts(F,W,4)S(V,M,M[r])local G=M[r]-1;while(G>2)do
  179. D(V,B)S(B,M,G)G=G-1;D(B,V)S(V,M,G)G=G-1 end;D(V,B)
  180. S(B,M,G)G=G-1;L(B,V)S(V,M,G)util.sleepCheckIn()return
  181. util.intsToBytes(V,Y,P)end;x()z()_()
  182. return{ROUNDS=r,KEY_TYPE=d,ENCRYPTION_KEY=l,DECRYPTION_KEY=u,expandEncryptionKey=A,expandDecryptionKey=N,encrypt=U,decrypt=C}end)
  183. local o=e(function(n,...)local function s()return{}end;local function h(d,l)table.insert(d,l)end;local function r(d)return
  184. table.concat(d)end;return{new=s,addString=h,toString=r}end)
  185. ciphermode=e(function(n,...)local s={}local h=math.random
  186. function s.encryptString(r,d,l,u)if u then local f={}for w=1,16 do f[w]=u[w]end;u=f else
  187. u={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}end
  188. local c=aes.expandEncryptionKey(r)local m=o.new()for f=1,#d/16 do local w=(f-1)*16+1
  189. local y={string.byte(d,w,w+15)}u=l(c,y,u)
  190. o.addString(m,string.char(unpack(y)))end
  191. return o.toString(m)end;function s.encryptECB(r,d,l)aes.encrypt(r,d,1,d,1)end;function s.encryptCBC(r,d,l)
  192. util.xorIV(d,l)aes.encrypt(r,d,1,d,1)return d end;function s.encryptOFB(r,d,l)
  193. aes.encrypt(r,l,1,l,1)util.xorIV(d,l)return l end;function s.encryptCFB(r,d,l)
  194. aes.encrypt(r,l,1,l,1)util.xorIV(d,l)return d end
  195. function s.encryptCTR(r,d,l)
  196. local u={}for c=1,16 do u[c]=l[c]end;aes.encrypt(r,l,1,l,1)
  197. util.xorIV(d,l)util.increment(u)return u end
  198. function s.decryptString(r,d,l,u)if u then local f={}for w=1,16 do f[w]=u[w]end;u=f else
  199. u={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}end;local c
  200. if l==s.decryptOFB or
  201. l==s.decryptCFB or l==s.decryptCTR then
  202. c=aes.expandEncryptionKey(r)else c=aes.expandDecryptionKey(r)end;local m=o.new()for f=1,#d/16 do local w=(f-1)*16+1
  203. local y={string.byte(d,w,w+15)}u=l(c,y,u)
  204. o.addString(m,string.char(unpack(y)))end
  205. return o.toString(m)end;function s.decryptECB(r,d,l)aes.decrypt(r,d,1,d,1)return l end;function s.decryptCBC(r,d,l)
  206. local u={}for c=1,16 do u[c]=d[c]end;aes.decrypt(r,d,1,d,1)
  207. util.xorIV(d,l)return u end;function s.decryptOFB(r,d,l)
  208. aes.encrypt(r,l,1,l,1)util.xorIV(d,l)return l end;function s.decryptCFB(r,d,l)
  209. local u={}for c=1,16 do u[c]=d[c]end;aes.encrypt(r,l,1,l,1)
  210. util.xorIV(d,l)return u end
  211. s.decryptCTR=s.encryptCTR;return s end)AES128=16;AES192=24;AES256=32;ECBMODE=1;CBCMODE=2;OFBMODE=3;CFBMODE=4;CTRMODE=4
  212. local function i(n,s,h)local r=s;if(s==
  213. AES192)then r=32 end
  214. if(r>#n)then local l=""for u=1,r-#n do
  215. l=l..string.char(0)end;n=n..l else n=string.sub(n,1,r)end;local d={string.byte(n,1,#n)}
  216. n=ciphermode.encryptString(d,n,ciphermode.encryptCBC,h)n=string.sub(n,1,s)return{string.byte(n,1,#n)}end
  217. function encrypt(n,s,h,r,d)assert(n~=nil,"Empty password.")
  218. assert(n~=nil,"Empty data.")local r=r or CBCMODE;local h=h or AES128;local l=i(n,h,d)
  219. local u=util.padByteString(s)
  220. if r==ECBMODE then
  221. return ciphermode.encryptString(l,u,ciphermode.encryptECB,d)elseif r==CBCMODE then
  222. return ciphermode.encryptString(l,u,ciphermode.encryptCBC,d)elseif r==OFBMODE then
  223. return ciphermode.encryptString(l,u,ciphermode.encryptOFB,d)elseif r==CFBMODE then
  224. return ciphermode.encryptString(l,u,ciphermode.encryptCFB,d)elseif r==CTRMODE then
  225. return ciphermode.encryptString(l,u,ciphermode.encryptCTR,d)else error("Unknown mode",2)end end
  226. function decrypt(n,s,h,r,d)local r=r or CBCMODE;local h=h or AES128;local l=i(n,h,d)local u
  227. if r==ECBMODE then
  228. u=ciphermode.decryptString(l,s,ciphermode.decryptECB,d)elseif r==CBCMODE then
  229. u=ciphermode.decryptString(l,s,ciphermode.decryptCBC,d)elseif r==OFBMODE then
  230. u=ciphermode.decryptString(l,s,ciphermode.decryptOFB,d)elseif r==CFBMODE then
  231. u=ciphermode.decryptString(l,s,ciphermode.decryptCFB,d)elseif r==CTRMODE then
  232. u=ciphermode.decryptString(l,s,ciphermode.decryptCTR,d)else error("Unknown mode",2)end;result=util.unpadByteString(u)
  233. if(result==nil)then return nil end;return result end
  234.  
  235. --End of AES
  236.  
  237.  
  238. function startup()
  239.     term.setCursorPos(1,1)
  240.     term.setBackgroundColor(afterBG)
  241.     term.setTextColor(aftertext)
  242.     term.clear()
  243.     if MakeStartup and fs.exists('start') then
  244.         shell.run('start')
  245.     end
  246. end
  247.  
  248.  
  249. local function maketest()
  250.     if MakeStartup and shell.getRunningProgram() ~= 'startup' then
  251.         fs.delete('startup')
  252.         fs.move(shell.getRunningProgram(), 'startup')
  253.     end
  254.     term.setCursorPos(1, 1)
  255.     term.setBackgroundColor(background)
  256.     term.setTextColor(text)
  257.     term.clear()
  258.     if fs.exists('/.encryptfs') ~= true then
  259.         --either this file got deleted, or it hasnt been created yet
  260.         --ask user to create a new passphrase for encrypting the fs
  261.         print("OK, Let's setup EncryptFS")
  262.         print("Enter a passphrase to encrypt the files on this system with.")
  263.         print("You will need to enter this passphrase every time you start this computer\n")
  264.         write("Passphrase: ")
  265.         local fp = read("*")
  266.         write("Confirm: ")
  267.         local sp = read("*")
  268.         --Check to see if they match...
  269.         if fp == sp then
  270.             --its a match
  271.             local estring = encrypt(fp, checkstring)
  272.             local handle = fs.open('/.encryptfs', 'w')
  273.             handle.write(textutils.serialize(estring))
  274.             --print("ESTRING", estring)
  275.             handle.close() -- write the encrypted checkstring to a files
  276.             --the reason we do this is so we can check the passphrase at boot without actually storing the passphrase
  277.         else
  278.             print("The passphrases you entered do not match!")
  279.             sleep(3)
  280.             os.reboot()
  281.         end
  282.         os.reboot()
  283.     end
  284. end
  285.  
  286. function checkpw()
  287.     local handle = fs.open('/.encryptfs', 'r')
  288.     local raw = handle.readAll()
  289.     handle.close()
  290.     raw = textutils.unserialize(raw)
  291.     --print("RAW    ", raw)
  292.     print("Enter your EncryptFS passphrase to unlock this computer\n")
  293.     while true do
  294.         write("Passphrase: ")
  295.         local p = read("*")
  296.         print("Checking...")
  297.         local crstring = decrypt(p, raw)
  298.         --print(crstring)
  299.         if tostring(crstring) == tostring(checkstring) then
  300.             password = p
  301.             print("Success")
  302.             break
  303.         end
  304.         print("Incorrect")
  305.         sleep(1.5)
  306.     end
  307.  
  308. end
  309.  
  310. maketest()
  311. checkpw()
  312.  
  313. local function decryptf(pass, msg)
  314.     local umsg = textutils.unserialize(msg)
  315.     local ok, out = pcall(decrypt, pass, umsg)
  316.     if ok then
  317.         msg = out
  318.     end
  319.     return msg
  320. end
  321.  
  322. local function encryptf(pass, msg)
  323.     local msg = encrypt(pass, msg)
  324.     msg = textutils.serialize(msg)
  325.     return msg
  326. end
  327.  
  328.  
  329. local fsopen = _G["fs"]["open"]
  330.  
  331.  
  332.  
  333. _G["fs"]["open"] = function(path, mode)
  334.  
  335.     if mode == 'r' and fs.exists(path) then
  336.         local handle = fsopen(path, 'r')
  337.         local hand = {}
  338.  
  339.         local string = decryptf(password, handle.readAll())
  340.  
  341.  
  342.         function magiclines(s)
  343.                 if s:sub(-1)~="\n" then s=s.."\n" end
  344.                 return s:gmatch("(.-)\n")
  345.         end
  346.  
  347.         function special()
  348.             return string
  349.         end
  350.  
  351.         local speci = 0
  352.  
  353.         function specialr()
  354.             int = 0
  355.             speci = speci + 1
  356.             for line in magiclines(string) do
  357.                 int = int + 1
  358.                 if speci == int then
  359.                     return line
  360.                 end
  361.             end
  362.         end
  363.  
  364.         hand['read'] = specialr
  365.         hand['readLine'] = specialr
  366.         hand['readAll'] = special
  367.         hand['close'] = handle.close
  368.         return hand
  369.     elseif mode == 'w' and not fs.isReadOnly(path) then
  370.         local handle = fsopen(path, 'w')
  371.         local hand = {}
  372.  
  373.         local buffer = ""
  374.  
  375.         function special()
  376.             local to = encryptf(password, buffer)
  377.             if shell.resolve(path) == shell.resolve("/startup") or shell.resolve(path) == shell.resolve('/disk/startup') then -- we dont want to encrypt the startup file
  378.               handle.write(buffer)
  379.             else
  380.               handle.write(to)
  381.             end
  382.             handle.close()
  383.         end
  384.  
  385.         function buff(txt)
  386.             buffer = buffer .. tostring(txt)
  387.         end
  388.  
  389.         hand["write"] = buff
  390.         hand['close'] = special
  391.         return hand
  392.     else
  393.         return fsopen(path, mode)
  394.     end
  395.  
  396. end
  397.  
  398. _G['io'] = _G['fs']
  399.  
  400.  
  401. startup()
Add Comment
Please, Sign In to add comment