SHARE
TWEET

AES.lua




Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- local component = require("component")
- local data = component.data
- local AES = {}
- -- key of AES
- AES.key = ""
- -- make 128bit AES key
- AES.makeKeyAES = function()
- return data.random(16)
- end
- -- encrypt the line with key,
- AES.encrypt = function(line,key)
- if key == nil then
- key = AES.key
- end
- return data.encrypt(line,key,key)
- end
- -- decrypt the line with key
- AES.decrypt= function(line,key)
- if key == nil then
- key = AES.key
- end
- return data.decrypt(line,key,key)
- end
- --save current key to file
- AES.saveKeyAES = function(address,key)
- local file = io.open(address,"w")
- if key == nil then
- key = AES.key
- end
- file:write(AES.key)
- file:close()
- end
- -- read key value from file
- AES.readKeyAES = function(address)
- local file = io.open(address,"r")
- local line = file:read()
- AES.key = line
- file:close()
- return line
- end
- -- change string to byte arrays, bytes line
- AES.string2Bytes = function(line)
- local newBytes = {}
- local newBytesLine = ""
- len = #line
- for i=1, len do
- newBytes[i] = string.byte(line,i)
- newBytesLine = newBytesLine .. " " .. tostring(newBytes[i])
- end
- return newBytes,newBytesLine
- end
- -- change bytelines to string
- AES.byteLine2string = function(bLine)
- local chars = {}
- for char in bLine:gmatch("%w+") do
- table.insert(chars,char)
- end
- local line = ""
- for i,v in ipairs(chars) do
- line = line..string.char(v)
- end
- return line,chars
- end
- -- encrypt the file with key using AES
- -- file will be represented as space and ints
- AES.encryptFile = function(oriAdd, encAdd, key)
- if key == nil then
- key = AES.key
- end
- print("read file...")
- local ori = io.open(oriAdd,"r")
- local enc = io.open(encAdd,"w")
- i = 1
- local line = ""
- local lines = {}
- local encLine = ""
- local encByteLine = ""
- while true do
- line = ori:read()
- if line == nil then
- break
- else
- lines[i] = line
- i = i + 1
- end
- end
- local linesNum = #lines
- print("total "..tostring(linesNum).." lines will be encrypted")
- for i,v in ipairs(lines) do
- if v == nil then
- break
- end
- print("encrypting... "..tostring(i).."/"..tostring(linesNum))
- encLine = AES.encrypt(v,key)
- print(encLine)
- _,encByteLine = AES.string2Bytes(encLine)
- print(encByteLine)
- enc:write(encByteLine)
- enc:write("\n")
- end
- ori:close()
- enc:close()
- end
- -- decrypt file with key using AES
- -- decrypted file will be the same as original
- AES.decryptFile = function(oriAdd,decAdd,key)
- if key == nil then
- key = AES.key
- end
- print("read File...")
- local ori = io.open(oriAdd,"r")
- local dec = io.open(decAdd,"w")
- i=1
- local line = ""
- local lines = {}
- local charLine = ""
- local decLine = ""
- while true do
- line = ori:read()
- if line == nil then
- break
- else
- lines[i] = line
- i = i+1
- end
- end
- local len = #lines
- print("total "..tostring(len).." lines will be decrypted")
- for i,v in ipairs(lines) do
- if v == nill then
- break
- end
- print("decrypting... "..tostring(i).."/"..tonumber(len))
- charLine = AES.byteLine2string(v)
- print(charLine)
- decLine = AES.decrypt(charLine,key)
- print(decLine)
- dec:write(decLine)
- dec:write("\n")
- end
- ori:close()
- dec:close()
- end
- return AES
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.