boomboomthehacker

BoomCrypt

Mar 19th, 2017
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.14 KB | None | 0 0
  1. -- BoomCrypt - the simple file encryptor
  2. -- Not to be confused with bcrypt, the secure hashing function
  3. -- Written by MasterOfBooms/BoomBoomLHack
  4. -- version 1.2
  5. -- Credits to SquidDev, ElvishJerricco, KillaVanilla, and Anavrins for their APIs
  6.  
  7. local version = "1.2"
  8. local pbk = 1024
  9. os.loadAPI(".bc/sha")
  10. os.loadAPI(".bc/b64")
  11. os.loadAPI(".bc/aes")
  12. os.loadAPI(".bc/rng")
  13. os.loadAPI(".bc/json")
  14.  
  15. local args = {...}
  16. local tFile = args[2]
  17. local passBullet = string.char(7)
  18. local function PRNG()
  19.  local r = 0
  20.  for i=1,math.random(128,512) do
  21.   local g = math.random(32768)
  22.   local s = math.random(4096)
  23.   r=r+g
  24.   r=r-s
  25.  end
  26.  return r
  27. end
  28. local function unloadAPI()
  29.  os.unloadAPI("sha")
  30.  os.unloadAPI("b64")
  31.  os.unloadAPI("aes")
  32.  os.unloadAPI("rng")
  33.  os.unloadAPI("json")
  34. end
  35. local function initCSPRNG()
  36.  rng.initalize_mt_generator(PRNG())
  37.  rng.seed_from_mt(PRNG())
  38.  rng.generate_isaac()
  39. end
  40. local function genIV()
  41.  local iv = {}
  42.  initCSPRNG()
  43.  for i=1,16 do
  44.   iv[i] = rng.random(1,255)
  45.  end
  46.  return iv
  47. end
  48. local function genKey(i,iter)
  49.  local hs = sha.digest(i)
  50.  local ps = sha.hmac(i,hs)
  51.  local p = sha.pbkdf2(i,ps,iter)
  52.  return p:toHex()
  53. end
  54. local function byteToStr(bs)
  55.  local str = ""
  56.  for i=1,#bs do
  57.   local cha = string.char(bs[i])
  58.   str = str..cha
  59.  end
  60.  return str
  61. end
  62. local function strToByte(str)
  63.  local bs = {}
  64.  for i=1,str:len() do
  65.   local cha = string.byte(str:sub(i,i))
  66.   table.insert(bs,cha)
  67.  end
  68.  return bs
  69. end
  70. local function encrypt(pwd,msg,iter)
  71.  local key = genKey(pwd,iter)
  72.  local iv = genIV()
  73.  local cr = aes.encrypt(key,msg,aes.AES256,aes.CTRMODE,iv)
  74.  local bs = strToByte(cr)
  75.  local out = b64.encode(bs)
  76.  return out,iv
  77. end
  78. local function decrypt(pwd,msg,iv,iter)
  79.  local key = genKey(pwd,iter)
  80.  local bs = b64.decode(msg)
  81.  local ci = byteToStr(bs)
  82.  local out = aes.decrypt(key,ci,aes.AES256,aes.CTRMODE,iv)
  83.  return out
  84. end
  85. local function printUsage()
  86.  local progName = shell.getRunningProgram()
  87.  print("Usage:")
  88.  print(progName.." <encrypt/decrypt> <file> [PBKDF2 iter]")
  89. end
  90. local function fileOper(oper,file,data)
  91.  if oper == "r" then
  92.   local file = fs.open(file,"r")
  93.   local out = file.readAll()
  94.   file.close()
  95.   return out
  96.  elseif oper == "w" then
  97.   local file = fs.open(file,"w")
  98.   file.write(data)
  99.   file.close()
  100.  elseif oper == "d" then
  101.   fs.delete(file)
  102.  elseif oper == "e" then
  103.   return fs.exists(file)
  104.  end
  105. end
  106.  
  107. print("BoomCrypt version "..version)
  108. if #args < 2 then
  109.  printError("Not enough arguments!")
  110.  printUsage()
  111.  return
  112. end
  113. if args[3] then
  114.  if tonumber(args[3]) < 1024 then
  115.   local pbk = 1024
  116.  end
  117.  pbk = tonumber(args[3])
  118. end
  119. if args[1] == "encrypt" then
  120.  if fileOper("e",tFile) then
  121.   write("Password: ")
  122.   local key = read(passBullet)
  123.   if #key < 8 then
  124.    printError("Password must be 8 characters or more!")
  125.    return
  126.    unloadAPI()
  127.   end
  128.   write("Encrypting, please wait... ")
  129.   local rf = fileOper("r",tFile)
  130.   local fb = b64.encode(strToByte(rf))
  131.   local cry,iv = encrypt(key,fb,pbk)
  132.   local data = {}
  133.   data["boomCrypt"] = version
  134.   data["cryptData"] = cry
  135.   data["IV"] = iv
  136.   data["cryptChecksum"] = sha.digest(cry):toHex()
  137.   data["orginChecksum"] = sha.digest(fb):toHex()
  138.   data["pbkdf2Iter"] = pbk
  139.   local out = json.encode(data)
  140.   fileOper("d",tFile)
  141.   fileOper("w",tFile,out)
  142.   print("success!")
  143.   unloadAPI()
  144.  else
  145.   printError("File not found!")
  146.   return
  147.   unloadAPI()
  148.  end
  149. elseif args[1] == "decrypt" then
  150.  if fileOper("e",tFile) then
  151.   local data = json.decode(fileOper("r",tFile))
  152.   if not data then
  153.    printError("Invaild or corrupted BoomCrypt file!")
  154.    return
  155.   elseif not data["boomCrypt"] == version then
  156.    printError("BoomCrypt file version mismatch")
  157.    print("This BoomCrypt file is not compatible with this version of BoomCrypt.")
  158.    return
  159.    unloadAPI()
  160.   end
  161.   if data["boomCrypt"] then
  162.    write("Password: ")
  163.    local key = read(passBullet)
  164.    if #key < 8 then
  165.     printError("Password must be 8 characters or more!")
  166.     return
  167.     unloadAPI()
  168.    end
  169.    write("Decrypting, please wait... ")
  170.    local rf = fileOper("r",tFile)
  171.    local data = json.decode(rf)
  172.    local fb = data["cryptData"]
  173.    local iv = data["IV"]
  174.    local cs = data["cryptChecksum"]
  175.    local gs = data["orginChecksum"]
  176.    local pbk = data["pbkdf2Iter"]
  177.    local cryChk = sha.digest(fb):toHex()
  178.    if not (cryChk == cs) then
  179.     print("failed.")
  180.     printError("Checksum mismatch: Encrypted data")
  181.     return
  182.     unloadAPI()
  183.    end
  184.    local dey = decrypt(key,fb,iv,pbk)
  185.    if not dey then
  186.      print("failed.")
  187.      printError("Invaild password!")
  188.      return
  189.      unloadAPI()
  190.    end
  191.    local orgChk = sha.digest(dey):toHex()
  192.    if not (orgChk == gs) then
  193.     print("failed.")
  194.     printError("Checksum mismatch: Decoded data")
  195.     return
  196.     unloadAPI()
  197.    end
  198.    local out = byteToStr(b64.decode(dey))
  199.    fileOper("d",tFile)
  200.    fileOper("w",tFile,out)
  201.    print("success!")
  202.   else
  203.    printError("Invaild or corrupted BoomCrypt file!")
  204.    return
  205.    unloadAPI()
  206.   end
  207.  else
  208.   printError("File not found!")
  209.   return
  210.   unloadAPI()
  211.  end
  212. else
  213.  printError("Unrecognized subcommand!")
  214.  printUsage()
  215.  unloadAPI()
  216.  return
  217. end
Advertisement
Add Comment
Please, Sign In to add comment