Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Encryption program by PixelToast! Thanks!
- local function zfill(N)
- N=string.format("%X",N)
- Zs=""
- if #N==1 then
- Zs="0"
- end
- return Zs..N
- end
- local function serializeImpl(t)
- local sType = type(t)
- if sType == "table" then
- local lstcnt=0
- for k,v in pairs(t) do
- lstcnt = lstcnt + 1
- end
- local result = "{"
- local aset=1
- for k,v in pairs(t) do
- if k==aset then
- result = result..serializeImpl(v)..","
- aset=aset+1
- else
- result = result..("["..serializeImpl(k).."]="..serializeImpl(v)..",")
- end
- end
- result = result.."}"
- return result
- elseif sType == "string" then
- return string.format("%q",t)
- elseif sType == "number" or sType == "boolean" or sType == "nil" then
- return tostring(t)
- elseif sType == "function" then
- local status,data=pcall(string.dump,t)
- if status then
- return 'func('..string.format("%q",data)..')'
- else
- error()
- end
- else
- error()
- end
- end
- local function split(T,func)
- if func then
- T=func(T)
- end
- local Out={}
- if type(T)=="table" then
- for k,v in pairs(T) do
- Out[split(k)]=split(v)
- end
- else
- Out=T
- end
- return Out
- end
- local function serialize( t )
- t=split(t)
- return serializeImpl( t, tTracking )
- end
- local function unserialize( s )
- local func, e = loadstring( "return "..s, "serialize" )
- local funcs={}
- if not func then
- return e
- end
- setfenv( func, {
- func=function(S)
- local new={}
- funcs[new]=S
- return new
- end,
- })
- return split(func(),function(val)
- if funcs[val] then
- return loadstring(funcs[val])
- else
- return val
- end
- end)
- end
- local function sure(N,n)
- if (l2-n)<1 then N="0" end
- return N
- end
- local function splitnum(S)
- Out=""
- for l1=1,#S,2 do
- l2=(#S-l1)+1
- CNum=tonumber("0x"..sure(string.sub(S,l2-1,l2-1),1) .. sure(string.sub(S,l2,l2),0))
- Out=string.char(CNum)..Out
- end
- return Out
- end
- local function wrap(N)
- return N-(math.floor(N/256)*256)
- end
- function checksum(S,num)
- local sum=0
- for char in string.gmatch(S,".") do
- for l1=1,(num or 1) do
- math.randomseed(string.byte(char)+sum)
- sum=sum+math.random(0,9999)
- end
- end
- math.randomseed(sum)
- return sum
- end
- local function genkey(len)
- checksum(encKey)
- local key={}
- local tKeys={}
- for l1=1,len do
- local num=math.random(1,len)
- while tKeys[num] do
- num=math.random(1,len)
- end
- tKeys[num]=true
- key[l1]={num,math.random(0,255)}
- end
- return key
- end
- function encrypt(data)
- data=serialize(data)
- local chs=checksum(data)
- local key=genkey(#data,encKey)
- local out={}
- local cnt=1
- for char in string.gmatch(data,".") do
- table.insert(out,key[cnt][1],zfill(wrap(string.byte(char)+key[cnt][2])),chars)
- cnt=cnt+1
- end
- return string.sub(serialize({chs,table.concat(out)}),2,-3)
- end
- function decrypt(data)
- local oData=data
- data=unserialize("{"..data.."}")
- if type(data)~="table" then
- return oData
- end
- local chs=data[1]
- data=data[2]
- local key=genkey((#data)/2,encKey)
- local sKey={}
- for k,v in pairs(key) do
- sKey[v[1]]={k,v[2]}
- end
- local str=splitnum(data)
- local cnt=1
- local out={}
- for char in string.gmatch(str,".") do
- table.insert(out,sKey[cnt][1],string.char(wrap(string.byte(char)-sKey[cnt][2])))
- cnt=cnt+1
- end
- out=table.concat(out)
- if checksum(out or "")==chs then
- return unserialize(out)
- end
- return oData,out,chs
- end
- term.clear()
- term.setCursorPos(1,1)
- print("Enter encryption key:")
- write(">")
- encKey = tostring(read())
- function LR()
- local n=1
- while true do
- local x, y=term.getCursorPos()
- term.clearLine()
- if n==1 then write(">LEFT< RIGHT") else write (" LEFT >RIGHT<") end
- term.setCursorPos(x, y)
- a, b=os.pullEventRaw()
- while a~="key" do a, b=os.pullEventRaw() end
- if b==203 and n==2 then n=1 end
- if b==205 and n==1 then n=2 end
- if b==28 then print("") break end
- end
- if n==1 then return true end
- if n==2 then return false end
- return false
- end
- local chatTable = { [1] = "Welcome to TarnChat! (/help for commands)" }
- local chatLine = 1
- local message = ""
- local user = "???"
- local compID = os.getComputerID()
- local toID = ""
- local msgMessage = ""
- local msgSent = ""
- function drawLine()
- term.setCursorPos( 1, 17 )
- term.clearLine()
- for n=1,49 do
- io.write("=")
- end
- term.setCursorPos(1,18)
- term.clearLine()
- write(username)
- write(": ")
- write(message)
- end
- function addChat(str)
- if chatLine < 16 then
- table.insert(chatTable, str)
- chatLine = chatLine + 1
- else
- for n=1,15 do
- chatTable[n] = chatTable[n+1]
- end
- chatTable[16] = str
- end
- end
- function quit()
- os.exit()
- end
- function showChat()
- term.setCursorPos( 1, 1 )
- for n=1,chatLine do
- term.clearLine()
- print(chatTable[n])
- end
- end
- print("Is your modem on the Left or the Right?")
- term.setCursorPos(1,18)
- if LR() == true then
- rednet.open("left")
- else
- rednet.open("right")
- end
- term.clear()
- repeat
- term.setCursorPos(1,1)
- print("Enter a username for yourself.")
- term.setCursorPos(1,18)
- user = read()
- if string.len(user) > 9 then
- term.setCursorPos(1,2)
- print("Name too long! 9 character maximum.")
- term.setCursorPos(1,18)
- elseif string.len(user) > 0 then
- write("Your username is ")
- write(user)
- print("")
- print("Entering Chat...")
- username = user
- end
- until string.len(user) ~=0
- sleep(2.0)
- term.clear()
- drawLine()
- rednet.broadcast(encrypt(""))
- rednet.broadcast(encrypt(username .. " enters the room!"))
- repeat
- term.setCursorPos(1,1)
- write(chatTable[chatLine] or "")
- drawLine()
- showChat()
- term.setCursorPos((3 + string.len(username) + string.len(message)),18)
- term.setCursorBlink( true )
- event, param = os.pullEventRaw()
- if event == "key" then
- if param == 28 then
- if string.sub(message,1,1) == "/" then
- if string.sub(message,1,6) == "/name " then
- if string.len(message) > 6 then
- if string.len(message) < 16 then
- user = username
- username = string.sub(message,7,string.len(message))
- message = "User '" .. user .. "' is now called '" .. username .. "'!"
- rednet.broadcast(encrypt(""))
- rednet.broadcast(encrypt(message))
- addChat(message)
- message = ""
- drawLine()
- else
- addChat("<SYS> Name too long! Maximum 9 characters.")
- message = ""
- end
- end
- elseif string.sub(message,1,4) == "/me " then
- local meMessage = string.sub(message,4,string.len(message))
- message = "*" .. username .. meMessage
- rednet.broadcast(encrypt(""))
- rednet.broadcast(encrypt(message))
- addChat(message)
- message = ""
- drawLine()
- elseif string.sub(message,1,5) == "/list" then
- rednet.broadcast(encrypt(""))
- rednet.broadcast(encrypt(message))
- message = ""
- elseif string.sub(message,1,5) == "/exit" then
- addChat("<SYS> Goodbye!")
- rednet.broadcast(encrypt(""))
- rednet.broadcast(encrypt("User '" .. username .. "' exits the room."))
- sleep(1.0)
- term.clear()
- term.setCursorPos(1,1)
- break
- elseif string.sub(message,1,5) == "/quit" then
- addChat("<SYS> Goodbye!")
- rednet.broadcast(encrypt(""))
- rednet.broadcast(encrypt("User '" .. username .. "' exits the room."))
- sleep(1.0)
- term.clear()
- term.setCursorPos(1,1)
- break
- elseif string.sub(message,1,5) == "/help" then
- addChat("/help - lists programs")
- addChat("/name <name> - changes username")
- addChat("/list - lists connected users, and console IDs")
- addChat("/me - used as emote")
- addChat("/clear - clears the screen")
- addChat("/msg <ID> <message> - sends a private message")
- addChat("/exit - exits the program")
- message = ""
- elseif string.sub(message,1,5) == "/msg " then
- if string.len(message) > 5 then
- if string.sub(message,7,7) == " " then
- toID = string.sub(message,6,6)
- msgMessage = string.sub(message,8,string.len(message))
- addChat("You whisper to #" .. toID .. ": " .. msgMessage)
- message = username .. " whispers to you: " .. msgMessage
- toID = tonumber(toID)
- rednet.send(toID,encrypt(""))
- rednet.send(toID,encrypt(message))
- message = ""
- elseif string.sub(message, 8,8) == " " then
- toID = string.sub(message,6,7)
- msgMessage = string.sub(message,9,string.len(message))
- addChat("You whisper to #" .. toID .. ": " .. msgMessage)
- message = username .. " whispers to you: " .. msgMessage
- toID = tonumber(toID)
- rednet.send(toID,encrypt(""))
- rednet.send(toID,encrypt(message))
- message = ""
- elseif string.sub(message, 9,9) == " " then
- toID = string.sub(message,6,8)
- msgMessage = string.sub(message,10,string.len(message))
- addChat("You whisper to #" .. toID .. ": " .. msgMessage)
- message = username .. " whispers to you: " .. msgMessage
- toID = tonumber(toID)
- rednet.send(toID,encrypt(""))
- rednet.send(toID,encrypt(message))
- message = ""
- elseif string.sub(message,10,10) == " " then
- toID = string.sub(message,6,9)
- msgMessage = string.sub(message,11,string.len(message))
- addChat("You whisper to #" .. toID .. ": " .. msgMessage)
- message = username .. " whispers to you: " .. msgMessage
- toID = tonumber(toID)
- rednet.send(toID,encrypt(""))
- rednet.send(toID,encrypt(message))
- message = ""
- end
- end
- elseif string.sub(message,1,3) == "/m " then
- if string.len(message) > 3 then
- if string.sub(message,5,5) == " " then
- toID = string.sub(message,4,4)
- msgMessage = string.sub(message,6,string.len(message))
- addChat("You whisper to #" .. toID .. ": " .. msgMessage)
- message = username .. " whispers to you: " .. msgMessage
- toID = tonumber(toID)
- rednet.send(toID,encrypt(""))
- rednet.send(toID,encrypt(message))
- message = ""
- elseif string.sub(message, 6,6) == " " then
- toID = string.sub(message,4,5)
- msgMessage = string.sub(message,7,string.len(message))
- addChat("You whisper to #" .. toID .. ": " .. msgMessage)
- message = username .. " whispers to you: " .. msgMessage
- toID = tonumber(toID)
- rednet.send(toID,encrypt(""))
- rednet.send(toID,encrypt(message))
- message = ""
- elseif string.sub(message, 7,7) == " " then
- toID = string.sub(message,4,6)
- msgMessage = string.sub(message,8,string.len(message))
- addChat("You whisper to #" .. toID .. ": " .. msgMessage)
- message = username .. " whispers to you: " .. msgMessage
- toID = tonumber(toID)
- rednet.send(toID,encrypt(""))
- rednet.send(toID,encrypt(message))
- message = ""
- elseif string.sub(message,8,8) == " " then
- toID = string.sub(message,4,7)
- msgMessage = string.sub(message,9,string.len(message))
- addChat("You whisper to #" .. toID .. ": " .. msgMessage)
- message = username .. " whispers to you: " .. msgMessage
- toID = tonumber(toID)
- rednet.send(toID,encrypt(""))
- rednet.send(toID,encrypt(message))
- message = ""
- end
- end
- elseif string.sub(message,1,6) == "/clear" then
- term.clear()
- for n=1,16 do
- addChat(" ")
- end
- chatTable = {}
- chatLine = 1
- message = ""
- else
- message = ""
- end
- else
- if string.len(message) > 0 then
- username = tostring(username)
- message = tostring(message)
- message = "<" .. username .. "> " .. message
- rednet.broadcast(encrypt(""))
- rednet.broadcast(encrypt(message))
- addChat(message)
- message = ""
- drawLine()
- end
- end
- elseif param == 14 then
- message = string.sub(message,1,string.len(message)-1)
- drawLine()
- end
- end
- if event == "char" then
- if string.len(message) < 40 then
- message = message .. param
- end
- drawLine()
- end
- if event == "rednet_message" then
- id, incoming = rednet.receive(0.5)
- incoming = decrypt(incoming)
- if incoming == "/list" then
- rednet.send(id,encrypt(""))
- rednet.send(id,encrypt("User '" .. username .. "' is connected at terminal #" .. compID))
- else
- addChat(incoming)
- end
- end
- until message == "override"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement