Advertisement
Tatantyler

FileHider

Apr 27th, 2013
719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.25 KB | None | 0 0
  1. -- FileHider
  2. -- By KillaVanilla
  3.  
  4. local args = {...}
  5.  
  6. if #args == 0 then
  7.     print("FileHider - A file encryption tool")
  8.     print("Usage: "..fs.getName(shell.getRunningProgram()).." [target]")
  9.     return
  10. end
  11.  
  12. if not fs.exists(args[1]) then
  13.     print("Target file "..args[1].." does not exist!")
  14.     return
  15. end
  16.  
  17. -- Download the AES API if we don't already have it:
  18.  
  19. if not fs.exists("AES") then
  20.     local webHandle = http.get("http://pastebin.com/raw.php?i=rCYDnCxn")
  21.     if webHandle then
  22.         local apiData = webHandle.readAll()
  23.         webHandle.close()
  24.         local apiHandle = fs.open("AES", "w")
  25.         apiHandle.write(apiData)
  26.         apiHandle.close()
  27.     else
  28.         print("Could not retrieve AES API!")
  29.         print("Ensure that the HTTP API is enabled.")
  30.         return
  31.     end
  32. end
  33.  
  34. os.loadAPI("AES")
  35.  
  36. local function selectParameter(parameterName)
  37.     local x,y = term.getCursorPos()
  38.     local key = {}
  39.     local keyStr = ""
  40.     while true do
  41.         term.setCursorPos(1,y)
  42.         term.clearLine()
  43.         term.setCursorPos(1,y+1)
  44.         term.clearLine()
  45.         term.setCursorPos(1,y)
  46.         print("Please type in the "..parameterName..":")
  47.         write(">")
  48.         keyStr = read()
  49.         if #keyStr > 16 then
  50.             print("Please type in a valid "..parameterName..".")
  51.             os.sleep(1.5)
  52.             term.setCursorPos(1, y+2)
  53.             term.clearLine()
  54.         else
  55.             for i=1, 16 do
  56.                 if i > #keyStr then
  57.                     key[i] = 0
  58.                 else
  59.                     key[i] = string.byte(keyStr, i, i)
  60.                 end
  61.             end
  62.             break
  63.         end
  64.     end
  65.     return key, keyStr
  66. end
  67.  
  68. local doDecrypt = false
  69. local sX, sY = term.getSize()
  70. local key, keyStr = selectParameter("encryption key")
  71. local iv, ivStr = selectParameter("initalization vector")
  72. local fData = {}
  73. local blocks = {}
  74. local blockNum = 1
  75.  
  76. local fHandle = fs.open(args[1], "rb")
  77. while true do
  78.     local byte = fHandle.read()
  79.     if byte then
  80.         if byte > 0xFF then
  81.             local t1 = bit.band(byte, 0x000000FF)
  82.             local t2 = bit.brshift(bit.band(byte, 0x0000FF00), 8)
  83.             local t3 = bit.brshift(bit.band(byte, 0x00FF0000), 16)
  84.             local t4 = bit.brshift(bit.band(byte, 0xFF000000), 24)
  85.             table.insert(fData, t1)
  86.             table.insert(fData, t2)
  87.             if t3 ~= 0 then
  88.                 table.insert(fData, t3)
  89.             end
  90.             if t4 ~= 0 then
  91.                 table.insert(fData, t4)
  92.             end
  93.         else
  94.             table.insert(fData, byte)
  95.         end
  96.     else
  97.         break
  98.     end
  99. end
  100. fHandle.close()
  101.  
  102. while true do
  103.     term.clear()
  104.     term.setCursorPos(1,1)
  105.     print("Options")
  106.     print("Using key: "..keyStr)
  107.     print("Using IV: "..ivStr)
  108.     print("Size: "..#fData.." bytes, "..math.ceil(#fData/16).." blocks")
  109.     print(string.rep("=", sX))
  110.     print("1 - Encrypt")
  111.     print("2 - Decrypt")
  112.     print("3 - Reselect Key")
  113.     print("4 - Reselect IV")
  114.     print("5 - Exit")
  115.     print("Use your keyboard to make selections.")
  116.     local event, key = os.pullEvent("key")
  117.     term.clear()
  118.     term.setCursorPos(1,1)
  119.     if key == 2 then
  120.         -- Insert the signature:
  121.         table.insert(fData, 1, string.byte("A"))
  122.         table.insert(fData, 1, string.byte("T"))
  123.         table.insert(fData, 1, string.byte("A"))
  124.         table.insert(fData, 1, string.byte("D"))
  125.         break
  126.     elseif key == 3 then
  127.         doDecrypt = true
  128.         break
  129.     elseif key == 4 then
  130.         key, keyStr = selectParameter("encryption key")
  131.     elseif key == 5 then
  132.         iv, ivStr = selectParameter("initalization vector")
  133.     elseif key == 6 then
  134.         return
  135.     end
  136. end
  137.  
  138. --[[
  139. for i=1, #fData, 16 do
  140.     blocks[blockNum] = {}
  141.     for j=1, 16 do
  142.         blocks[blockNum][j] = fData[i+(j-1)]
  143.     end
  144.     if #blocks[blockNum] < 16 then
  145.         for j=#blocks[blockNum], 16 do
  146.             table.insert(blocks[blockNum], 0)
  147.         end
  148.     end
  149.     blockNum = blockNum+1
  150. end
  151. ]]
  152.  
  153. if not doDecrypt then
  154.     print("Please type in the output file path:")
  155.     write(">")
  156.     local fileName = read()
  157.     print("Encrypting file: "..args[1])
  158.     local bytes = AES.encrypt_bytestream(fData, key, iv)
  159.     --[[
  160.     local x,y = term.getCursorPos()
  161.     for i=1, #blocks do
  162.         term.setCursorPos(x,y)
  163.         term.clearLine()
  164.         term.write("Block: "..i.." of "..#blocks..". ("..math.floor( (i/#blocks)*100 ).."%)")
  165.         blocks[i] = AES.encrypt_block(blocks[i], key)
  166.         os.queueEvent("i_can_fly") -- Credit to MCUniverse, who's primeanac program led to this faster way of yielding.
  167.         os.pullEvent()
  168.     end
  169.     term.setCursorPos(1,y+1)
  170.     ]]
  171.     local fHandle = fs.open(fileName, "wb")
  172.     for byte=1, #bytes do
  173.         fHandle.write(bytes[byte])
  174.     end
  175.     fHandle.close()
  176.     print("Done. Encrypted data saved to:")
  177.     print(fileName)
  178. else
  179.     print("Decrypting file: "..args[1])
  180.     local bytes = AES.decrypt_bytestream(fData, key, iv)
  181.     --[[
  182.     local x,y = term.getCursorPos()
  183.     for i=1, #blocks do
  184.         term.setCursorPos(x,y)
  185.         term.clearLine()
  186.         term.write("Block: "..i.." of "..#blocks..". ("..math.floor( (i/#blocks)*100 ).."%)")
  187.         blocks[i] = AES.decrypt_block(blocks[i], key)
  188.         os.queueEvent("i_can_fly")
  189.         os.pullEvent()
  190.     end
  191.     term.setCursorPos(1,y+1)
  192.     ]]
  193.     if string.char( bytes[1], bytes[2], bytes[3], bytes[4] ) == "DATA" then
  194.         print("Decryption successful.")
  195.         print("Please type in the output file path:")
  196.         write(">")
  197.         local fileName = read()
  198.         local fHandle = fs.open(fileName, "wb")
  199.         for byte=5, #bytes do
  200.             fHandle.write(bytes[byte])
  201.         end
  202.         --[[
  203.         for i=1, #blocks do
  204.             if i==1 then
  205.                 for j=5, 16 do
  206.                     fHandle.write(blocks[i][j])
  207.                 end
  208.             else
  209.                 for j=1, 16 do
  210.                     fHandle.write(blocks[i][j])
  211.                 end
  212.             end
  213.         end
  214.         ]]
  215.         fHandle.close()
  216.         print("Data saved to:")
  217.         print(fileName)
  218.     else
  219.         print("Decryption failed.")
  220.     end
  221. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement