SHARE
TWEET

Enigma Security Software Api

Wojbie Mar 1st, 2015 (edited) 654 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --# Enigma Security Software v4.6 - Api ,simple/sample program
  2. --# Made By Wojbie
  3. --# http://pastebin.com/WYuNMxQx
  4.  
  5. --# This part defines functions that will be used in this api.
  6.  
  7. --# Table reversor
  8. local function rev(A) local tRev,i,j={} for i,j in pairs(A) do tRev[j]=i end return tRev end
  9.  
  10. --# Table duplicator
  11. local function copyT(A)
  12.     if not A or type(A)~="table" then return A end
  13.     local B={}
  14.         for i,k in pairs(A) do
  15.             if type(k)=="table" then B[i]=copyT(k)
  16.             else B[i]=k end
  17.         end
  18.     return B
  19. end
  20.  
  21. --# Rotors
  22. local rot = {
  23. {95,96,63,85,3,89,19,62,23,30,11,29,75,83,67,25,36,41,91,14,9,61,54,44,94,33,26,69,18,8,15,76,43,5,16,66,72,92,52,46,45,24,39,68,12,55,80,77,2,17,78,81,6,60,32,59,10,65,4,34,42,50,70,64,93,47,21,57,37,49,13,28,48,35,27,56,88,31,58,1,40,71,82,22,74,20,86,38,84,79,90,87,53,7,73,51},
  24. {95,96,66,2,49,67,43,20,61,76,51,50,35,82,13,17,1,91,3,85,16,27,63,23,33,87,39,81,30,59,42,70,78,9,72,7,37,54,89,44,36,31,57,64,41,45,83,21,38,65,25,90,48,18,40,6,74,22,62,46,88,77,32,34,12,84,68,29,69,75,73,4,24,94,56,15,19,79,58,53,92,55,14,26,47,80,52,10,60,71,5,86,28,93,11,8},
  25. {95,96,51,69,50,34,28,17,43,42,1,75,4,25,85,32,87,58,63,73,19,36,16,64,47,26,71,76,78,3,93,13,61,46,70,49,68,45,39,81,40,62,7,88,5,38,92,48,12,83,82,24,18,9,8,90,55,29,44,35,14,6,52,72,10,60,21,79,11,80,27,91,31,30,33,53,66,89,77,22,57,41,2,15,94,20,86,56,23,37,59,65,67,84,74,54},
  26. {76,30,55,90,91,83,22,48,74,64,77,40,71,57,92,51,66,85,56,41,1,17,18,93,36,27,16,43,50,75,21,70,38,31,84,58,82,45,53,52,78,96,12,34,3,11,68,6,46,9,94,67,61,44,37,47,35,63,69,4,25,81,2,65,32,62,7,29,33,14,28,19,72,24,60,54,39,95,23,73,79,20,59,87,10,88,13,86,42,5,49,26,8,89,80,15},
  27. {13,89,7,80,58,3,1,65,92,81,30,8,96,48,43,47,79,61,51,4,88,29,78,27,94,22,16,45,74,72,70,95,36,59,39,60,75,12,64,77,10,49,26,17,11,46,9,6,76,85,19,2,66,5,73,62,41,87,54,71,28,21,14,15,23,44,69,82,24,20,90,42,57,33,50,34,93,31,83,84,18,37,53,91,56,38,63,40,52,86,25,68,32,35,67,55},
  28. {70,10,14,8,48,88,63,12,37,91,71,20,50,65,87,78,79,46,39,75,29,9,53,36,13,21,24,19,44,81,49,58,22,61,42,96,66,64,56,27,86,47,89,84,59,74,32,30,16,6,72,33,95,57,80,69,34,5,85,94,41,90,60,2,23,67,35,76,77,52,68,11,7,51,73,26,62,25,15,17,45,43,82,1,92,83,18,93,28,3,55,4,40,38,31,54},
  29. {9,60,38,64,85,91,63,1,53,81,18,94,35,55,16,57,19,27,47,71,11,17,50,84,31,6,34,88,25,43,46,33,21,5,89,48,37,2,75,87,59,52,76,70,69,23,45,80,40,56,14,73,62,32,30,92,82,78,61,72,44,54,12,68,29,7,65,24,96,8,74,79,95,26,20,4,41,51,90,42,22,39,3,36,67,15,58,49,10,13,28,66,93,86,77,83},
  30. {82,16,57,49,4,20,61,93,47,90,28,3,35,81,84,24,69,94,19,32,21,79,33,68,38,64,76,26,72,31,30,13,39,70,92,18,5,10,25,11,14,71,46,45,78,41,88,15,53,56,51,74,89,8,83,9,86,87,50,2,73,95,34,40,29,43,60,12,96,58,42,23,52,6,1,55,36,7,17,66,77,67,80,59,63,44,37,85,48,75,62,22,91,65,54,27},
  31. {79,64,53,47,28,39,63,43,26,9,5,19,69,29,12,84,11,15,18,46,60,56,27,33,24,17,74,85,34,95,51,88,72,13,16,4,77,49,59,71,25,22,70,30,14,42,8,52,92,41,87,55,83,6,67,57,38,61,54,35,81,45,1,23,48,3,80,90,96,75,32,76,36,37,86,89,78,40,62,58,66,10,73,7,44,65,82,94,50,91,20,21,93,2,68,31},
  32. {24,88,80,16,37,30,6,19,7,22,3,75,81,90,46,18,21,1,4,9,59,72,23,91,11,86,54,49,69,50,66,5,51,41,71,74,17,38,20,67,96,84,85,14,76,53,36,70,89,47,94,2,13,25,39,78,92,42,28,95,31,82,56,26,87,65,44,55,34,45,35,68,33,77,8,32,57,40,93,79,61,64,62,52,63,83,73,27,12,60,15,58,48,10,43,29},
  33. }
  34.  
  35. local al = {" ","!","\"","#","$","%","&","'","(",")","*","+",",","-",".","/","0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?","@","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","[","\\","]","^","_","`","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","{","|","}","~","\n",} -- changed \000 to \n for experiment - should cause it to switch enters in coded file while leaving \r be. Add both \000 and \r?
  36. local la = rev(al)
  37. local alen = #al
  38.  
  39. --# Crypting helper functions
  40. local function norm(A)  while A>alen do A=A-alen end while A<1 do A=A+alen end return A end
  41. local function tick(t) t[7]=t[7]+1 if t[7]>alen then t[7]=1 t[8]=t[8]+1 end if t[8]>alen then t[8]=1 t[9]=t[9]+1 end if t[9]>alen then t[9]=1 end end
  42.  
  43. --# Validator functions.
  44. --number validator
  45.  
  46. local function inRange(m,low,high)
  47.     low = low or -math.huge
  48.     high = high or math.huge
  49.     return m >= low and m <= high
  50. end
  51.  
  52. --table validator
  53.  
  54. local function valTable(t)
  55.     local left={}
  56.     local right={}
  57.  
  58.     for i,k in pairs(t) do
  59.         left[i] = true
  60.         right[k] = true
  61.     end
  62.        
  63.     if #left ~= alen or #right ~= alen then return false end
  64.  
  65.     for i=1,alen do
  66.         if not (left[i] and right[i]) then return false end
  67.     end
  68.  
  69.     return true
  70. end
  71.  
  72. --# Api Functions
  73. --Main function - creates crypter
  74. local function enigmaCrypter(tA,tB,tC,nA,nB,nC,bLock) --Create Crypter using tA,tB,tC rotors and set it to nA,nB,nC positions. If tA,tB,tC are numbers use provided rotors from table if they are tables use them as rotor.
  75.  
  76.     if type(tA) == "table" and not valTable(tA) or
  77.     type(tA) == "table" and not valTable(tA) or
  78.     type(tA) == "table" and not valTable(tA) then
  79.     return false,"One or more rotor tables are invalid." end
  80.  
  81.     if type(tA) == "number" and not rot[tA] or
  82.     type(tB) == "number" and not rot[tB] or
  83.     type(tC) == "number" and not rot[tC] then
  84.     return false,"One or more rotor outside of range." end
  85.    
  86.     tA,tB,tC= rot[tA] or tA , rot[tB] or tB , rot[tC] or tC
  87.    
  88.     if type(tA) ~= "table" or
  89.     type(tA) ~= "table" or
  90.     type(tA) ~= "table" then
  91.     return false,"One or more rotor selectors is not valid variable." end
  92.    
  93.     if type(nA) ~= "number" or type(nA) == "number" and not inRange(nA,1,alen) or
  94.         type(nB) ~= "number" or type(nB) == "number" and not inRange(nB,1,alen) or
  95.         type(nC) ~= "number" or type(nB) == "number" and not inRange(nC,1,alen) then
  96.         return false,"One or more initial positions are invalid. Positions need to be in range of 1-"..alen.."."
  97.     end
  98.    
  99.     --All is valid by this point.
  100.    
  101.     local t = {copyT(tA),copyT(tB),copyT(tC),rev(tC),rev(tB),rev(tA),norm(nA),norm(nB),norm(nC)}
  102.  
  103.     --# Metatable for crypting with table.
  104.     local metaCrypter = {
  105.         ["__newindex"]=function(sT,k,v) --Don't let any changes to happen.
  106.         end,
  107.         ["__call"]=function(sT,k) --Call crypter like function? Crypting    time!
  108.             if type(k)=="string" then
  109.                 return string.gsub(k,".", function(c)
  110.                     --crypt one char here
  111.                     if la[c] then
  112.                         local L=la[c]
  113.                         L = norm(L+t[1][ t[7] ])
  114.                         L = norm(L+t[2][ t[8] ])
  115.                         L = norm(L+t[3][ t[9] ])
  116.                         L = alen - L
  117.                         L = norm(L-t[4][ t[9] ])
  118.                         L = norm(L-t[5][ t[8] ])
  119.                         L = norm(L-t[6][ t[7] ])
  120.                         tick(t)
  121.                         return al[L]
  122.                     else
  123.                         return false --keep orginal in the string unchanged
  124.                     end
  125.                 end)
  126.             else
  127.                 return nil --Give back nothing
  128.             end
  129.         end,
  130.         ["__metatable"]={},--Protection from accidental removal of matatable.
  131.     }
  132.    
  133.     local locked = bLock and true
  134.     local blockCopy = function() locked=true end
  135.     local copy = function(bFlag) if not locked then return enigmaCrypter(t[1],t[2],t[3],t[7],t[8],t[9],bFlag) else return false,"Copying disabled" end end
  136.     return setmetatable({copy=copy,clone=copy,blockCopy=blockCopy},metaCrypter)
  137. end
  138.  
  139. --# Example of crypter set to rotors 1,2,3 and to position 1,1,1
  140. local Example = enigmaCrypter(1,2,3,1,1,1)
  141.  
  142. --# end of Api definition
  143.  
  144. if shell or multishell then --program version
  145.  
  146.     local tArgs={...}
  147.  
  148.     if #tArgs==0 then
  149.         print("You have run this api instead of loading it. You can use it as single file crypter by using 'enigma <absotue path>'. Selected file will be overwritten with encrypred version. This is still an example program and enigma was made as api for you to use. Use os.loadApi on this file to load it.")
  150.         return
  151.     end
  152.  
  153.     if not fs.exists(tArgs[1]) or fs.isDir(tArgs[1]) or fs.isReadOnly(tArgs[1]) then
  154.         print("File "..tArgs[1].." don't exist, is a directory or is in readonly folder. Can't Enigma that!")
  155.         return
  156.     end
  157.  
  158.     --# Basic Gui stuff
  159.  
  160.     local A,B,C,a,b,c
  161.  
  162.     --#  Basic Pass Screen
  163.     local function Bwreact()
  164.         term.clear()
  165.         term.setCursorPos(1,1)
  166.         print("Select rotors [0-9]")
  167.         while not A or A<0 or A>9 do print("rotor 1:") A=tonumber(read("*")) end
  168.         while not B or B<0 or B>9 do print("rotor 2:") B=tonumber(read("*")) end
  169.         while not C or C<0 or C>9 do print("rotor 3:") C=tonumber(read("*")) end
  170.         A,B,C=A+1,B+1,C+1
  171.         term.clear()
  172.         term.setCursorPos(1,1)
  173.         print("Enter rotors Positons [0-99]")
  174.         while not a or a<0 or a>99 do print("rotor 1 Positons:") a=tonumber(read("*")) end
  175.         while not b or b<0 or b>99 do print("rotor 2 Positons:") b=tonumber(read("*")) end
  176.         while not c or c<0 or c>99 do print("rotor 3 Positons:") c=tonumber(read("*")) end
  177.         term.clear()
  178.         term.setCursorPos(1,1)
  179.     end
  180.  
  181.     Bwreact()
  182.  
  183.     local process = enigmaCrypter(A,B,C,a,b,c)
  184.  
  185.     --# Working example code
  186.  
  187.     local input=fs.open(tArgs[1],"r")
  188.     local data=input.readAll()
  189.     input.close()
  190.     data=process(data)
  191.     local output=fs.open(tArgs[1],"w")
  192.     output.write(data)
  193.     output.close()
  194.  
  195.     return
  196.  
  197. end
  198.  
  199. --# Api version - define crypter
  200.  
  201. crypter = enigmaCrypter
  202. return enigmaCrypter
  203.  
  204. --A coin has two sides, and one is always the winner. Flip accordingly.
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. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top