Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function string:split( inSplitPattern, outResults )
- if not outResults then
- outResults = { }
- end
- local theStart = 1
- local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
- while theSplitStart do
- table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
- theStart = theSplitEnd + 1
- theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
- end
- table.insert( outResults, string.sub( self, theStart ) )
- return outResults
- end
- Crypto = {}
- function Crypto.DEC_HEXX(IN)
- local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
- while IN>0 do
- I=I+1
- IN,D=math.floor(IN/B),math.mod(IN,B)+1
- OUT=string.sub(K,D,D)..OUT
- end
- return OUT
- end
- function Crypto.BYTE_HEX(Bytes)
- local HexBytes = {}
- for k,v in ipairs(Bytes) do
- HexBytes[k] = Crypto.DEC_HEXX(v)
- end
- return table.concat(HexBytes,'-')
- end
- function Crypto.HEX_BYTE(HexString)
- local Bytes = {}
- local SplitHex = HexString:split("-")
- for k,v in pairs(SplitHex) do
- Bytes[k] = tonumber("0x" .. v)
- end
- return Bytes
- end
- function Crypto.Xor(x, y) --Made by Arno Wagner <arno@wagner.name>
- local z = 0
- for i = 0, 31 do
- if (x % 2 == 0) then -- x had a '0' in bit i
- if ( y % 2 == 1) then -- y had a '1' in bit i
- y = y - 1
- z = z + 2 ^ i -- set bit i of z to '1'
- end
- else -- x had a '1' in bit i
- x = x - 1
- if (y % 2 == 0) then -- y had a '0' in bit i
- z = z + 2 ^ i -- set bit i of z to '1'
- else
- y = y - 1
- end
- end
- y = y / 2
- x = x / 2
- end
- return z
- end
- function Crypto.Compare(t)
- local key, min = 1, t[1]
- for k, v in ipairs(t) do
- if t[k] < min then
- key, min = k, v
- end
- end
- return key
- end
- function Crypto.BytesToString(bytes) --Made by Philippe Lhoste
- local s = ""
- for i = 1, table.getn(bytes) do
- s = s .. string.char(bytes[i])
- end
- return s
- end
- function Crypto.StringToBytes(string)
- local bytes = {}
- for i = 1, string.len(string) do
- bytes[i] = string.byte(string,i)
- end
- return bytes
- end
- function Crypto.Encrypt(string,key,airbag)
- local msg = {}
- msg.Bytes = {}
- msg.Bytes = Crypto.StringToBytes(string)
- local keyBytes = Crypto.StringToBytes(key)
- for i in ipairs(msg.Bytes) do
- msg.Bytes[i] = Crypto.Xor(msg.Bytes[i],keyBytes[(i-1) % table.maxn(keyBytes)+1])
- end
- local key2 = Crypto.Compare(msg.Bytes)
- if airbag == true then
- msg.Airbag = msg.Bytes[key2] - 1
- else
- msg.Airbag = 0
- end
- for i in ipairs(msg.Bytes) do
- msg.Bytes[i] = msg.Bytes[i] - msg.Airbag
- end
- msg.Hex = Crypto.BYTE_HEX(msg.Bytes)
- return msg
- end
- function Crypto.Decrypt(string,key,airbag)
- local Bytes = {}
- Bytes = Crypto.HEX_BYTE(string)
- local keyBytes = Crypto.StringToBytes(key)
- for i in ipairs(Bytes) do
- Bytes[i] = Crypto.Xor(Bytes[i] + airbag,keyBytes[(i-1) % table.maxn(keyBytes)+1])
- end
- return Bytes
- end
- Test = {}
- function Test.FullTest(string,key,airbag)
- print("Original string: " .. string)
- print("Encryption key: " .. key .. "\n")
- local msg = {}
- msg = Crypto.Encrypt(string,key,airbag)
- print("Encrypted String: " .. msg.Hex)
- print("Encryption Airbag: " .. msg.Airbag .. "\n")
- local decrypted = Crypto.BytesToString(Crypto.Decrypt(msg.Hex,key,msg.Airbag))
- print("Decrypted string: " .. decrypted .. "\n")
- end
- function Test.Decrypt(string,key,airbag)
- print(Crypto.BytesToString(Crypto.Decrypt(string,key,airbag)))
- end
- function Test.HexTest(string)
- local hex = Crypto.BYTE_HEX(Crypto.StringToBytes(string))
- print("Encrypted: " .. hex)
- local dec = Crypto.BytesToString(Crypto.HEX_BYTE(hex))
- print("Decrypted: " .. dec)
- end
Add Comment
Please, Sign In to add comment