First i am building a client server program for a banking system. everything i am writing is my own code and ive got it down to decrypting messages on the server.
the setup i have is two test computers one as the target bank server and the other as a client just to send over encrypted data. my encryption algorithm has two parts to modify the text inputed. it has a key which can be any number, and a password. the password. so on each server and client i have hardcoded in several keys and passwords so i can see if the server can decode the encrypted text given that the client randomly chose a pair key and password to encrypt the important data with.
the current way i have it works, but there is an issue with the textutils.unserialize that i am running into.
the example data i am sending over is: {ptl="cConnect",cid=os.getComputerID(),data={},}
i am basically turning a table into a string then encrypting it which still remains a string to send over rednet.
the server takes the rawdata and runs through the keys and passwords to find a table.
i use the textutils.unserialize for every action then test to see if the function returns a table or a string.
i am only seeing if i get a table then i test to see if i have my two keys in the table i originally sent IE: ptl and cid
like i said it works but there is a wierd error i am getting for the serialize function.
the client does what i need it to the error is happening on the serverside so ill post the server code. if you ask for the client code to test yourselves just let me know.
i am also adding in screenshots of the client and server so show that the code was working in decrypting the data but only on one instance was i getting an error.
Spoiler
local contains = function(tbl,val)
for k,v in pairs(tbl) do
if v == val then return true end
end
return false
end
local hash = function(str)
local out = {}
for s in str:gmatch(".") do
if not contains(out,s) then out[#out+1] = s end
end
return table.concat(out)
end
local encryption = function(decrypt,plainTXT,key,password)
local cipher,cipherTXT = "",""
local base = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789=+[]{}-_,." '
if key ~= nil and key > #base then -- keep key within bounds of base length
key = key % #base
end
if password ~= nil then -- place password at front of encryption without duplicates
cipher = hash(password)
end
for i=1,#base do -- create the cipher
local CHAR = string.sub(base,(key+i)%#base,(key+i)%#base)
if password ~= nil then
for I=1,#password do -- find the same letter in password & remove it
local pasChar = string.sub(password,I,I)
if pasChar == CHAR then
CHAR = ""
end
end
end
cipher = cipher..CHAR
end
if type(plainTXT) == "table" then
plainTXT = textutils.serialize(plainTXT)
end
for i=1,#plainTXT do -- loop through text for encryption
local letter = string.sub(plainTXT,i,i)
for I=1,#base do
local baseKey = string.sub(base,I,I)
local hashKey = string.sub(cipher,I,I)
if decrypt == true then -- if decrypting flip flop baseKey & hashKey
if letter == hashKey then
cipherTXT = cipherTXT..baseKey
end
else
if letter == baseKey then -- encrypt baseKey with hashKey
cipherTXT = cipherTXT..hashKey
end
end
end
end
return cipherTXT
end
local keys = {6,117,24,48,372}
local pass = {"squIrrel","ClasSic","BlankeT","challenger"}
local m = peripheral.wrap("top")
m.open(2)
while true do
local ev = { os.pullEvent() }
local bfD = ""
local pkg = {}
if ev[1] == "modem_message" then
local rawData = ev[5]
for i=1,#keys do
for I=1,#pass do
bfD = encryption(true,rawData,keys[i],pass[I])
local isTable = textutils.unserialize(bfD)
if type(isTable) == "table" and isTable["ptl"] and isTable["cid"] then
pkg["data"] = isTable
pkg["key"] = keys[i]
pkg["pwd"] = pass[I]
end
end
end
end
print(pkg["data"])
print("Decrypted w/: "..pkg["key"].." |"..pkg["pwd"])
end