Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- BoomCrypt - the simple file encryptor
- -- Not to be confused with bcrypt, the secure hashing function
- -- Written by MasterOfBooms/BoomBoomLHack
- -- version 1.0
- -- Credits to SquidDev, ElvishJerricco, KillaVanilla, and Anavrins for their APIs
- local version = "1.0"
- os.loadAPI("sha")
- os.loadAPI("b64")
- os.loadAPI("aes")
- os.loadAPI("rng")
- os.loadAPI("json")
- local args = {...}
- local tFile = args[2]
- local passBullet = string.char(7)
- local function PRNG()
- local r = 0
- for i=1,math.random(128,512) do
- local g = math.random(32768)
- local s = math.random(4096)
- r=r+g
- r=r-s
- end
- return r
- end
- local function initCSPRNG()
- rng.initalize_mt_generator(PRNG())
- rng.seed_from_mt(PRNG())
- rng.generate_isaac()
- end
- local function genIV()
- local iv = {}
- initCSPRNG()
- for i=1,16 do
- iv[i] = rng.random(1,255)
- end
- return iv
- end
- local function genKey(i)
- local s = sha.digest(i)
- local p = sha.pbkdf2(i,s,1024)
- return p:toHex()
- end
- local function byteToStr(bs)
- local str = ""
- for i=1,#bs do
- local cha = string.char(bs[i])
- str = str..cha
- end
- return str
- end
- local function strToByte(str)
- local bs = {}
- for i=1,str:len() do
- local cha = string.byte(str:sub(i,i))
- table.insert(bs,cha)
- end
- return bs
- end
- local function encrypt(pwd,msg)
- local key = genKey(pwd)
- local iv = genIV()
- local cr = aes.encrypt(key,msg,aes.AES256,aes.CTRMODE,iv)
- local bs = strToByte(cr)
- local out = b64.encode(bs)
- return out,iv
- end
- local function decrypt(pwd,msg,iv)
- local key = genKey(pwd)
- local bs = b64.decode(msg)
- local ci = byteToStr(bs)
- local out = aes.decrypt(key,ci,aes.AES256,aes.CTRMODE,iv)
- return out
- end
- local function printUsage()
- local progName = shell.getRunningProgram()
- print("Usage:")
- print(progName.." <encrypt/decrypt> <file>")
- end
- local function fileOper(oper,file,data)
- if oper == "r" then
- local file = fs.open(file,"r")
- local out = file.readAll()
- file.close()
- return out
- elseif oper == "w" then
- local file = fs.open(file,"w")
- file.write(data)
- file.close()
- elseif oper == "d" then
- fs.delete(file)
- elseif oper == "e" then
- return fs.exists(file)
- end
- end
- print("BoomCrypt version "..version)
- if #args < 2 then
- printError("Not enough arguments!")
- printUsage()
- return
- end
- if args[1] == "encrypt" then
- if fileOper("e",tFile) then
- write("Password: ")
- local key = read(passBullet)
- write("Encrypting, please wait... ")
- local rf = fileOper("r",tFile)
- local fb = b64.encode(strToByte(rf))
- local cry,iv = encrypt(key,fb)
- local data = {}
- data["boomCrypt"] = version
- data["cryptData"] = cry
- data["IV"] = iv
- data["cryptChecksum"] = sha.digest(cry):toHex()
- data["orginChecksum"] = sha.digest(fb):toHex()
- local out = json.encode(data)
- fileOper("d",tFile)
- fileOper("w",tFile,out)
- print("success!")
- else
- printError("File not found!")
- return
- end
- elseif args[1] == "decrypt" then
- if fileOper("e",tFile) then
- local data = json.decode(fileOper("r",tFile))
- if not data then
- printError("Invaild or corrupted BoomCrypt file!")
- return
- elseif not data["boomCrypt"] == version then
- printError("BoomCrypt file version mismatch")
- print("This BoomCrypt file is not compatible with this version of BoomCrypt.")
- return
- end
- if data["boomCrypt"] then
- write("Password: ")
- local key = read(passBullet)
- write("Decrypting, please wait... ")
- local rf = fileOper("r",tFile)
- local data = json.decode(rf)
- local fb = data["cryptData"]
- local iv = data["IV"]
- local cs = data["cryptChecksum"]
- local gs = data["orginChecksum"]
- local cryChk = sha.digest(fb):toHex()
- if not cryChk == cs then
- print("failed.")
- printError("Checksum mismatch: Encrypted data")
- return
- end
- local dey = decrypt(key,fb,iv)
- if not dey then
- print("failed.")
- printError("Invaild password!")
- return
- end
- local orgChk = sha.digest(dey):toHex()
- if not orgChk == gs then
- print("failed.")
- printError("Checksum mismatch: Decoded data")
- return
- end
- local out = byteToStr(b64.decode(dey))
- fileOper("d",tFile)
- fileOper("w",tFile,out)
- print("success!")
- else
- printError("Invaild or corrupted BoomCrypt file!")
- return
- end
- else
- printError("File not found!")
- end
- else
- printError("Unrecognized subcommand!")
- printUsage()
- return
- end
Add Comment
Please, Sign In to add comment