Wojbie

Enigma Security Software Api

Mar 1st, 2015 (edited)
954
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.07 KB | None | 0 0
  1. --# Enigma Security Software v4.6 - Api ,simple/sample program
  2. --# Made By Wojbie
  3. --# http://pastebin.com/WYuNMxQx
  4.  
  5. --   Copyright (c) 2015-2021 Wojbie (wojbie@wojbie.net)
  6. --   Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met:
  7. --   1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  8. --   2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  9. --   3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  10. --   4. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. --   5. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
  12. --   NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. YOU ACKNOWLEDGE THAT THIS SOFTWARE IS NOT DESIGNED, LICENSED OR INTENDED FOR USE IN THE DESIGN, CONSTRUCTION, OPERATION OR MAINTENANCE OF ANY NUCLEAR FACILITY.
  13.  
  14. --# This part defines functions that will be used in this api.
  15.  
  16. --# Table reversor
  17. local function rev(A) local tRev,i,j={} for i,j in pairs(A) do tRev[j]=i end return tRev end
  18.  
  19. --# Table duplicator
  20. local function copyT(A)
  21.     if not A or type(A)~="table" then return A end
  22.     local B={}
  23.         for i,k in pairs(A) do
  24.             if type(k)=="table" then B[i]=copyT(k)
  25.             else B[i]=k end
  26.         end
  27.     return B
  28. end
  29.  
  30. --# Rotors
  31. local rot = {
  32. {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},
  33. {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},
  34. {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},
  35. {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},
  36. {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},
  37. {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},
  38. {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},
  39. {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},
  40. {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},
  41. {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},
  42. }
  43.  
  44. 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?
  45. local la = rev(al)
  46. local alen = #al
  47.  
  48. --# Crypting helper functions
  49. local function norm(A)  while A>alen do A=A-alen end while A<1 do A=A+alen end return A end
  50. 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
  51.  
  52. --# Validator functions.
  53. --number validator
  54.  
  55. local function inRange(m,low,high)
  56.     low = low or -math.huge
  57.     high = high or math.huge
  58.     return m >= low and m <= high
  59. end
  60.  
  61. --table validator
  62.  
  63. local function valTable(t)
  64.     local left={}
  65.     local right={}
  66.  
  67.     for i,k in pairs(t) do
  68.         left[i] = true
  69.         right[k] = true
  70.     end
  71.        
  72.     if #left ~= alen or #right ~= alen then return false end
  73.  
  74.     for i=1,alen do
  75.         if not (left[i] and right[i]) then return false end
  76.     end
  77.  
  78.     return true
  79. end
  80.  
  81. --# Api Functions
  82. --Main function - creates crypter
  83. 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.
  84.  
  85.     if type(tA) == "table" and not valTable(tA) or
  86.     type(tA) == "table" and not valTable(tA) or
  87.     type(tA) == "table" and not valTable(tA) then
  88.     return false,"One or more rotor tables are invalid." end
  89.  
  90.     if type(tA) == "number" and not rot[tA] or
  91.     type(tB) == "number" and not rot[tB] or
  92.     type(tC) == "number" and not rot[tC] then
  93.     return false,"One or more rotor outside of range." end
  94.    
  95.     tA,tB,tC= rot[tA] or tA , rot[tB] or tB , rot[tC] or tC
  96.    
  97.     if type(tA) ~= "table" or
  98.     type(tA) ~= "table" or
  99.     type(tA) ~= "table" then
  100.     return false,"One or more rotor selectors is not valid variable." end
  101.    
  102.     if type(nA) ~= "number" or type(nA) == "number" and not inRange(nA,1,alen) or
  103.         type(nB) ~= "number" or type(nB) == "number" and not inRange(nB,1,alen) or
  104.         type(nC) ~= "number" or type(nB) == "number" and not inRange(nC,1,alen) then
  105.         return false,"One or more initial positions are invalid. Positions need to be in range of 1-"..alen.."."
  106.     end
  107.    
  108.     --All is valid by this point.
  109.    
  110.     local t = {copyT(tA),copyT(tB),copyT(tC),rev(tC),rev(tB),rev(tA),norm(nA),norm(nB),norm(nC)}
  111.  
  112.     --# Metatable for crypting with table.
  113.     local metaCrypter = {
  114.         ["__newindex"]=function(sT,k,v) --Don't let any changes to happen.
  115.         end,
  116.         ["__call"]=function(sT,k) --Call crypter like function? Crypting    time!
  117.             if type(k)=="string" then
  118.                 return string.gsub(k,".", function(c)
  119.                     --crypt one char here
  120.                     if la[c] then
  121.                         local L=la[c]
  122.                         L = norm(L+t[1][ t[7] ])
  123.                         L = norm(L+t[2][ t[8] ])
  124.                         L = norm(L+t[3][ t[9] ])
  125.                         L = alen - L
  126.                         L = norm(L-t[4][ t[9] ])
  127.                         L = norm(L-t[5][ t[8] ])
  128.                         L = norm(L-t[6][ t[7] ])
  129.                         tick(t)
  130.                         return al[L]
  131.                     else
  132.                         return false --keep orginal in the string unchanged
  133.                     end
  134.                 end)
  135.             else
  136.                 return nil --Give back nothing
  137.             end
  138.         end,
  139.         ["__metatable"]={},--Protection from accidental removal of matatable.
  140.     }
  141.    
  142.     local locked = bLock and true
  143.     local blockCopy = function() locked=true end
  144.     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
  145.     return setmetatable({copy=copy,clone=copy,blockCopy=blockCopy},metaCrypter)
  146. end
  147.  
  148. --# Example of crypter set to rotors 1,2,3 and to position 1,1,1
  149. local Example = enigmaCrypter(1,2,3,1,1,1)
  150.  
  151. --# end of Api definition
  152.  
  153. if shell or multishell then --program version
  154.  
  155.     local tArgs={...}
  156.  
  157.     if #tArgs==0 then
  158.         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.")
  159.         return
  160.     end
  161.  
  162.     if not fs.exists(tArgs[1]) or fs.isDir(tArgs[1]) or fs.isReadOnly(tArgs[1]) then
  163.         print("File "..tArgs[1].." don't exist, is a directory or is in readonly folder. Can't Enigma that!")
  164.         return
  165.     end
  166.  
  167.     --# Basic Gui stuff
  168.  
  169.     local A,B,C,a,b,c
  170.  
  171.     --#  Basic Pass Screen
  172.     local function Bwreact()
  173.         term.clear()
  174.         term.setCursorPos(1,1)
  175.         print("Select rotors [0-9]")
  176.         while not A or A<0 or A>9 do print("rotor 1:") A=tonumber(read("*")) end
  177.         while not B or B<0 or B>9 do print("rotor 2:") B=tonumber(read("*")) end
  178.         while not C or C<0 or C>9 do print("rotor 3:") C=tonumber(read("*")) end
  179.         A,B,C=A+1,B+1,C+1
  180.         term.clear()
  181.         term.setCursorPos(1,1)
  182.         print("Enter rotors Positons [0-99]")
  183.         while not a or a<0 or a>99 do print("rotor 1 Positons:") a=tonumber(read("*")) end
  184.         while not b or b<0 or b>99 do print("rotor 2 Positons:") b=tonumber(read("*")) end
  185.         while not c or c<0 or c>99 do print("rotor 3 Positons:") c=tonumber(read("*")) end
  186.         term.clear()
  187.         term.setCursorPos(1,1)
  188.     end
  189.  
  190.     Bwreact()
  191.  
  192.     local process = enigmaCrypter(A,B,C,a,b,c)
  193.  
  194.     --# Working example code
  195.  
  196.     local input=fs.open(tArgs[1],"r")
  197.     local data=input.readAll()
  198.     input.close()
  199.     data=process(data)
  200.     local output=fs.open(tArgs[1],"w")
  201.     output.write(data)
  202.     output.close()
  203.  
  204.     return
  205.  
  206. end
  207.  
  208. --# Api version - define crypter
  209.  
  210. crypter = enigmaCrypter
  211. return enigmaCrypter
  212.  
  213. --A coin has two sides, and one is always the winner. Flip accordingly.
Add Comment
Please, Sign In to add comment