randomdude999

FileHider CLI

Oct 16th, 2015
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- FileHider
  2. -- By KillaVanilla
  3. -- CLI by randomdude999
  4.  
  5. local args = {...}
  6.  
  7. if #args == 0 then
  8.     print("FileHiderCLI - A command-line file encryption tool")
  9.     print("Usage: "..fs.getName(shell.getRunningProgram()).." <file> <encrypt | decrypt> <key> <init. vector> <output>")
  10.     return
  11. end
  12.  
  13. if not fs.exists(args[1]) then
  14.     error("File "..args[1].." does not exist",0)
  15. end
  16.  
  17. -- Download the AES API if we don't already have it:
  18.  
  19. if not fs.exists("/.apis/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("/.apis/AES", "w")
  25.         apiHandle.write(apiData)
  26.         apiHandle.close()
  27.     else
  28.         error("Could not retrieve AES API.\nMake sure Internet connection is available.")
  29.     end
  30. end
  31.  
  32. os.loadAPI("AES")
  33.  
  34. local function getParam(param)
  35.     local key = {}
  36.     local keyStr = args[param]
  37.     while true do
  38.         if #keyStr > 16 then
  39.             error("Key too long. (Must be smaller than or equal to 16)",0)
  40.         else
  41.             for i=1, 16 do
  42.                 if i > #keyStr then
  43.                     key[i] = 0
  44.                 else
  45.                     key[i] = string.byte(keyStr, i, i)
  46.                 end
  47.             end
  48.             break
  49.         end
  50.     end
  51.     return key, keyStr
  52. end
  53.  
  54. local key, keyStr = getParam(3)
  55. local iv, ivStr = getParam(4)
  56. local outputFile = args[5]
  57. local fData = {}
  58. local blocks = {}
  59. local blockNum = 1
  60.  
  61. local fHandle = fs.open(args[1], "rb")
  62. while true do
  63.     local byte = fHandle.read()
  64.     if byte then
  65.         if byte > 0xFF then
  66.             local t1 = bit.band(byte, 0x000000FF)
  67.             local t2 = bit.brshift(bit.band(byte, 0x0000FF00), 8)
  68.             local t3 = bit.brshift(bit.band(byte, 0x00FF0000), 16)
  69.             local t4 = bit.brshift(bit.band(byte, 0xFF000000), 24)
  70.             table.insert(fData, t1)
  71.             table.insert(fData, t2)
  72.             if t3 ~= 0 then
  73.                 table.insert(fData, t3)
  74.             end
  75.             if t4 ~= 0 then
  76.                 table.insert(fData, t4)
  77.             end
  78.         else
  79.             table.insert(fData, byte)
  80.         end
  81.     else
  82.         break
  83.     end
  84. end
  85. fHandle.close()
  86.  
  87. if args[2] == "encrypt" then
  88.     -- Insert the signature:
  89.     table.insert(fData, 1, string.byte("A"))
  90.     table.insert(fData, 1, string.byte("T"))
  91.     table.insert(fData, 1, string.byte("A"))
  92.     table.insert(fData, 1, string.byte("D"))
  93.     local fileName = args[5]
  94.     local bytes = AES.encrypt_bytestream(fData, key, iv)
  95.     local fHandle = fs.open(fileName, "wb")
  96.     for byte=1, #bytes do
  97.         fHandle.write(bytes[byte])
  98.     end
  99.     fHandle.close()
  100. elseif args[2] == "decrypt" then
  101.     local bytes = AES.decrypt_bytestream(fData, key, iv)
  102.     if string.char( bytes[1], bytes[2], bytes[3], bytes[4] ) == "DATA" then
  103.         local fileName = args[5]
  104.         local fHandle = fs.open(fileName, "wb")
  105.         for byte=5, #bytes do
  106.             fHandle.write(bytes[byte])
  107.         end
  108.         fHandle.close()
  109.     else
  110.         error("Decryption failed. Is the key / init. vector incorrect, or was the file encrypted with this tool?\n(Technical info: First 4 bytes are not equal to \"DATA\")",0)
  111.     end
  112. else
  113.     error("2nd parameter should be either encrypt or decrypt.")
  114. end
Advertisement
Add Comment
Please, Sign In to add comment