This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
PrinzJuliano's profile picture

Need help with a program

Started by PrinzJuliano, 05 January 2013 - 05:10 AM
PrinzJuliano #1
Posted 05 January 2013 - 06:10 AM
Hello guys,
i got a problem: The following code is a sting/file encryption and decryption program


local tArgs = { ... }
local param1 = tonumber(tArgs[1])
local param2 = tArgs[2]
local param3 = tArgs[3]
local content = tArgs[4]
if not param1 or not param2 or not param3 or not content then
print("")
print("Usage: zipper.lua <code> <-enc, -dec> <-f , -s>  <filename , string>");
return;
end
function encrypt_string(code, str)
local chars="1234567890!@#$%^&amp;*()qwertyuiopasdfghjklzxcvbnm QWERTYUIOPASDFGHJKLZXCVBNM,<.>/?;:'[{]}\|`~"
local newstr = ""
for i=1,999 do
  if string.sub(str, i, i) == "" then
   break;
  else
   com=string.sub(str,i,i);
  end
  for j=1,90 do
   cur=string.sub(chars,j,j)
   if com == cur then
	new=j+code;
	while new > 90 do
	 new = new - 90;
	end
	newstr = ""..newstr..""..string.sub(chars,new,new).."";
   end
  end
end
return newstr;
end

function encrypt_file(codec, filename22 )
origfile = io.open(filename, "r")
fCont = origfile:read("*a")
origfile:close()
encfstr = encrypt_string(codec, fCont)
encfilename = ""
print("+---------[File Content]---------+")
print(encfstr)
print("+---------[End of file!]---------+")
encfilename = filename..".zip";
encfile = io.open(encfilename, "w")
encfile:write(encfstr);
encfile:close()
return encfstr, encfilename;
end
function decrypt_string(code, str)
local chars="1234567890!@#$%^&amp;*()qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM ,<.>/?;:'[{]}\|`~";
local newstr = ""
for i=1,999 do
		if string.sub(str,i,i) == "" then
			break
		else
   com=string.sub(str,i,i)
  end
		for x=1, 90 do
   cur=string.sub(chars,x,x)
   if com == cur then
	new=x-code
	while new < 0 do
	 new = new + 90
	end
	newstr=""..newstr..""..string.sub(chars,new,new)..""
   end
  end
	end
	return newstr
end
function decrypt_file(codec, filename)
origfile = io.open(filename, "r")
fContent = origfile:read("*a")
origfile.close()
decstr = decrypt_string(codec, fContent)
decfilename = ""
print("+---------[File Content]---------+")
print(decstr)
print("+---------[End of file!]---------+")
decfilename = filename..".source";
decfile = io.open(decfilename, "w")
decfile:write(decstr)
decfile:close()
return decstr, decfilename
end
if param2 == "-enc" and param3 == "-f" then
encrypt_file(param1, content)
elseif param2 == "-enc" and param3 == "-s" then
fail = encrypt_string(param1, content)
print("Original: "..content.." | Packed: "..fail)
elseif param2 == "-dec" and param3 == "-f" then
decrypt_file(param1, ""..content.."")
elseif param2 == "-dec" and param3 == "-s" then
fail = decrypt_string(param1, content)
print("Packed: "..content.." | Unpacked: "..fail)
else
print("Unkown parameters: "..param2.." "..param3)
end


But if I want to encrypt a file, it returns a number like [44, 45, 46].
Please help me, i want to make a safer rednet

Thanks for your help! ;)/>
remiX #2
Posted 05 January 2013 - 06:28 AM
What happens if you remove the part when trying to save the file as a .zip?


encfilename = filename..".zip";
encfile = io.open(encfilename, "w")
encfile:write(encfstr);
encfile:close()
PrinzJuliano #3
Posted 05 January 2013 - 07:30 AM
ok this is how the file is looking:

local tArgs = { ... }
local param1 = tonumber(tArgs[1])
local param2 = tArgs[2]
local param3 = tArgs[3]
local content = tArgs[4]
if not param1 or not param2 or not param3 or not content then
print("")
print("Usage: zipper.lua <code> <-enc, -dec> <-f , -s>  <filename , string>");
return;
end
function encrypt_string(code, str)
local chars="1234567890!@#$%^&amp;*()qwertyuiopasdfghjklzxcvbnm QWERTYUIOPASDFGHJKLZXCVBNM,<.>/?;:'[{]}\|`~"
local newstr = ""
for i=1,999 do
  if string.sub(str, i, i) == "" then
   break;
  else
   com=string.sub(str,i,i);
  end
  for j=1,90 do
   cur=string.sub(chars,j,j)
   if com == cur then
	new = j+code;
	while new > 90 do
	 new = new - 90;
	end
	newstr = ""..newstr..""..string.sub(chars,new,new).."";
   end
  end
end
return newstr;
end

function encrypt_file(codec, filename22 )
origfile = io.open(filename22, "r")
fCont = origfile:read("*a")
origfile:close()
encfstr = encrypt_string(codec, fCont)
encfilename = ""
print("+---------[File Content]---------+")
print(encfstr)
print("+---------[End of file!]---------+")

encfilename = filename22..".zip";
--[[
encfile = io.open(encfilename, "w")
encfile:write(encfstr);
encfile:close()
]]
return encfstr, encfilename;
end
function decrypt_string(code, str)
local chars="1234567890!@#$%^&amp;*()qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM ,<.>/?;:'[{]}\|`~";
local newstr = ""
for i=1,999 do
		if string.sub(str,i,i) == "" then
			break
		else
   com=string.sub(str,i,i)
  end
		for x=1, 90 do
   cur=string.sub(chars,x,x)
   if com == cur then
	new=x-code
	while new < 0 do
	 new = new + 90
	end
	newstr=""..newstr..""..string.sub(chars,new,new)..""
   end
  end
	end
	return newstr
end
function decrypt_file(codec, filename)
origfile = io.open(filename, "r")
fContent = origfile:read("*a")
origfile.close()
decstr = decrypt_string(codec, fContent)
decfilename = ""
print("+---------[File Content]---------+")
print(decstr)
print("+---------[End of file!]---------+")

decfilename = filename..".source";
--[[
decfile = io.open(decfilename, "w")
decfile:write(decstr)
decfile:close()
]]
return decstr, decfilename
end
if param2 == "-enc" and param3 == "-f" then
lol1, lol2 = encrypt_file(param1, content)
elseif param2 == "-enc" and param3 == "-s" then
fail = encrypt_string(param1, content)
print("Original: "..content.." | Packed: "..fail)
elseif param2 == "-dec" and param3 == "-f" then
lol3, lol4 = decrypt_file(param1, ""..content.."")
elseif param2 == "-dec" and param3 == "-s" then
fail = decrypt_string(param1, content)
print("Packed: "..content.." | Unpacked: "..fail)
else
print("Unkown parameters: "..param2.." "..param3)
end

– file hello
this is a message
this is a message 2
the whole world consists of 2 messages

– Console
zipper.lua 64 -enc -f hello

output:

+———[File Content]———+
bla bla the encrypted code
+———[End of file!]———+

It worked but i want it saved in the right order
remiX #4
Posted 05 January 2013 - 08:01 AM
Looks like it lua doesn't like to write into zip files, would never have guessed ;D

What do you mean you want it to be saved in the right order? What order is that?
PrinzJuliano #5
Posted 06 January 2013 - 11:46 PM
well like
1st line Source into first line Encoded
2nd line Source into 2nd line Encoded

Just read one line, encode it and write this line at the same position as in the original file