Advertisement
jasuk500

AES.api

Nov 19th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.41 KB | None | 0 0
  1. local component = require("component")
  2. local data = component.data
  3.  
  4. local AES = {}
  5.  
  6. -- key of AES
  7. AES.key = ""
  8.  
  9. -- make 128bit AES key
  10. AES.makeKeyAES = function()
  11.   return data.random(16)
  12. end
  13.  
  14. -- encrypt the line with key,
  15. AES.encrypt = function(line,key)
  16.   if key == nil then
  17.     key = AES.key
  18.   end
  19.  
  20.   return data.encrypt(line,key,key)
  21. end
  22.  
  23. -- decrypt the line with key
  24. AES.decrypt= function(line,key)
  25.   if key == nil then
  26.     key = AES.key
  27.   end
  28.  
  29.   return data.decrypt(line,key,key)
  30. end
  31.  
  32. --save current key to file
  33. AES.saveKeyAES = function(address,key)
  34.   local file = io.open(address,"w")
  35.  
  36.   if key == nil then
  37.     key = AES.key
  38.   end
  39.  
  40.   file:write(AES.key)
  41.   file:close()
  42. end
  43.  
  44. -- read key value from file
  45. AES.readKeyAES = function(address)
  46.   local file = io.open(address,"r")
  47.   local line = file:read()
  48.   AES.key = line
  49.   file:close()
  50.   return line
  51. end
  52.  
  53. -- change string to byte arrays, bytes line
  54. AES.string2Bytes = function(line)
  55.   local newBytes = {}
  56.   local newBytesLine = ""
  57.   len = #line
  58.  
  59.   for i=1, len do
  60.     newBytes[i] = string.byte(line,i)
  61.     newBytesLine = newBytesLine .. " " .. tostring(newBytes[i])
  62.   end
  63.  
  64.   return newBytes,newBytesLine
  65. end
  66.  
  67. -- change bytelines to string
  68. AES.byteLine2string = function(bLine)
  69.  
  70.   local chars = {}
  71.   for char in bLine:gmatch("%w+") do
  72.     table.insert(chars,char)
  73.   end
  74.  
  75.   local line = ""
  76.   for i,v in ipairs(chars) do
  77.     line = line..string.char(v)
  78.   end
  79.  
  80.   return line,chars
  81. end
  82.  
  83. -- encrypt the file with key using AES
  84. -- file will be represented as space and ints
  85. AES.encryptFile = function(oriAdd, encAdd, key)
  86.  
  87.   if key == nil then
  88.     key = AES.key
  89.   end
  90.  
  91.   print("read file...")
  92.   local ori = io.open(oriAdd,"r")
  93.   local enc = io.open(encAdd,"w")
  94.  
  95.   i = 1
  96.   local line = ""
  97.   local lines = {}
  98.   local encLine = ""
  99.   local encByteLine = ""
  100.   while true do
  101.     line = ori:read()
  102.  
  103.     if line == nil then
  104.       break
  105.     else
  106.       lines[i] = line
  107.       i = i + 1
  108.     end
  109.   end
  110.  
  111.   local linesNum = #lines
  112.   print("total "..tostring(linesNum).." lines will be encrypted")
  113.  
  114.   for i,v in ipairs(lines) do
  115.     if v == nil then
  116.       break
  117.     end
  118.  
  119.     print("encrypting... "..tostring(i).."/"..tostring(linesNum))
  120.     encLine = AES.encrypt(v,key)
  121.     print(encLine)
  122.     _,encByteLine = AES.string2Bytes(encLine)
  123.     print(encByteLine)
  124.     enc:write(encByteLine)
  125.     enc:write("\n")
  126.   end
  127.  
  128.   ori:close()
  129.   enc:close()
  130. end
  131.  
  132. -- decrypt file with key using AES
  133. -- decrypted file will be the same as original
  134. AES.decryptFile = function(oriAdd,decAdd,key)
  135.   if key == nil then
  136.      key = AES.key
  137.   end
  138.  
  139.   print("read File...")
  140.   local ori = io.open(oriAdd,"r")
  141.   local dec = io.open(decAdd,"w")
  142.  
  143.   i=1
  144.   local line = ""
  145.   local lines = {}
  146.   local charLine = ""
  147.   local decLine = ""
  148.  
  149.   while true do
  150.     line = ori:read()
  151.     if line == nil then
  152.       break
  153.     else
  154.       lines[i] = line
  155.       i = i+1
  156.     end
  157.   end
  158.  
  159.   local len = #lines
  160.  
  161.   print("total "..tostring(len).." lines will be decrypted")
  162.  
  163.   for i,v in ipairs(lines) do
  164.     if v == nill then
  165.       break
  166.     end
  167.  
  168.     print("decrypting... "..tostring(i).."/"..tonumber(len))
  169.     charLine = AES.byteLine2string(v)
  170.     print(charLine)
  171.     decLine = AES.decrypt(charLine,key)
  172.     print(decLine)
  173.     dec:write(decLine)
  174.     dec:write("\n")
  175.   end
  176.  
  177.   ori:close()
  178.   dec:close()
  179.  
  180. end
  181.  
  182.  
  183. return AES
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement