Metalhead33

Cipher for LuaBank (meant for ComputerCraft)

Jun 6th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.04 KB | None | 0 0
  1. function string:split( inSplitPattern, outResults )
  2.   if not outResults then
  3.     outResults = { }
  4.   end
  5.   local theStart = 1
  6.   local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
  7.   while theSplitStart do
  8.     table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
  9.     theStart = theSplitEnd + 1
  10.     theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
  11.   end
  12.   table.insert( outResults, string.sub( self, theStart ) )
  13.   return outResults
  14. end
  15.  
  16. Crypto = {}
  17.  
  18. function Crypto.DEC_HEXX(IN)
  19.     local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
  20.     while IN>0 do
  21.         I=I+1
  22.         IN,D=math.floor(IN/B),math.mod(IN,B)+1
  23.         OUT=string.sub(K,D,D)..OUT
  24.     end
  25.     return OUT
  26. end
  27.  
  28. function Crypto.BYTE_HEX(Bytes)
  29.     local HexBytes = {}
  30.     for k,v in ipairs(Bytes) do
  31.         HexBytes[k] = Crypto.DEC_HEXX(v)
  32.     end
  33.     return table.concat(HexBytes,'-')
  34. end
  35.  
  36. function Crypto.HEX_BYTE(HexString)
  37.     local Bytes = {}
  38.     local SplitHex = HexString:split("-")
  39.     for k,v in pairs(SplitHex) do
  40.         Bytes[k] = tonumber("0x" .. v)
  41.     end
  42.     return Bytes
  43. end
  44.  
  45. function Crypto.Xor(x, y) --Made by Arno Wagner <arno@wagner.name>
  46.    local z = 0
  47.    for i = 0, 31 do
  48.       if (x % 2 == 0) then                      -- x had a '0' in bit i
  49.          if ( y % 2 == 1) then                  -- y had a '1' in bit i
  50.             y = y - 1
  51.             z = z + 2 ^ i                       -- set bit i of z to '1'
  52.          end
  53.       else                                      -- x had a '1' in bit i
  54.          x = x - 1
  55.          if (y % 2 == 0) then                  -- y had a '0' in bit i
  56.             z = z + 2 ^ i                       -- set bit i of z to '1'
  57.          else
  58.             y = y - 1
  59.          end
  60.       end
  61.       y = y / 2
  62.       x = x / 2
  63.    end
  64.    return z
  65. end
  66.  
  67. function Crypto.Compare(t)
  68. local key, min = 1, t[1]
  69. for k, v in ipairs(t) do
  70.     if t[k] < min then
  71.         key, min = k, v
  72.     end
  73. end
  74.     return key
  75. end
  76.  
  77. function Crypto.BytesToString(bytes) --Made by Philippe Lhoste
  78.   local s = ""
  79.   for i = 1, table.getn(bytes) do
  80.     s = s .. string.char(bytes[i])
  81.   end
  82.   return s
  83. end
  84.  
  85. function Crypto.StringToBytes(string)
  86.     local bytes = {}
  87.     for i = 1, string.len(string) do
  88.     bytes[i] = string.byte(string,i)
  89.     end
  90.     return bytes
  91. end
  92.  
  93. function Crypto.Encrypt(string,key,airbag)
  94.     local msg = {}
  95.     msg.Bytes = {}
  96.     msg.Bytes = Crypto.StringToBytes(string)
  97.     local keyBytes = Crypto.StringToBytes(key)
  98.     for i in ipairs(msg.Bytes) do
  99.         msg.Bytes[i] = Crypto.Xor(msg.Bytes[i],keyBytes[(i-1) % table.maxn(keyBytes)+1])
  100.     end
  101.     local key2 = Crypto.Compare(msg.Bytes)
  102.     if airbag == true then
  103.         msg.Airbag = msg.Bytes[key2] - 1
  104.     else
  105.         msg.Airbag = 0
  106.     end
  107.     for i in ipairs(msg.Bytes) do
  108.         msg.Bytes[i] = msg.Bytes[i] - msg.Airbag
  109.     end
  110.     msg.Hex = Crypto.BYTE_HEX(msg.Bytes)
  111.     return msg
  112. end
  113.  
  114. function Crypto.Decrypt(string,key,airbag)
  115.     local Bytes = {}
  116.     Bytes = Crypto.HEX_BYTE(string)
  117.     local keyBytes = Crypto.StringToBytes(key)
  118.     for i in ipairs(Bytes) do
  119.         Bytes[i] = Crypto.Xor(Bytes[i] + airbag,keyBytes[(i-1) % table.maxn(keyBytes)+1])
  120.     end
  121.     return Bytes
  122. end
  123.  
  124. Test = {}
  125. function Test.FullTest(string,key,airbag)
  126.     print("Original string: " .. string)
  127.     print("Encryption key: " .. key .. "\n")
  128.     local msg = {}
  129.     msg = Crypto.Encrypt(string,key,airbag)
  130.     print("Encrypted String: " .. msg.Hex)
  131.     print("Encryption Airbag: " .. msg.Airbag .. "\n")
  132.     local decrypted = Crypto.BytesToString(Crypto.Decrypt(msg.Hex,key,msg.Airbag))
  133.     print("Decrypted string: " .. decrypted .. "\n")
  134. end
  135. function Test.Decrypt(string,key,airbag)
  136.     print(Crypto.BytesToString(Crypto.Decrypt(string,key,airbag)))
  137. end
  138. function Test.HexTest(string)
  139.     local hex = Crypto.BYTE_HEX(Crypto.StringToBytes(string))
  140.     print("Encrypted: " .. hex)
  141.     local dec = Crypto.BytesToString(Crypto.HEX_BYTE(hex))
  142.     print("Decrypted: " .. dec)
  143. end
Add Comment
Please, Sign In to add comment