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

[Help / Question] Is there a way to encrypt a program?

Started by Matrixmage, 31 August 2012 - 04:50 PM
Matrixmage #1
Posted 31 August 2012 - 06:50 PM
Hi, I'm just wondering if there is a way that i can encrypt a program possibly with a null key so I am the only one who can access it?

I want to have a program of mine to be un-readable by a user unless they decrypt it (out of game or in-game are both fine), but still be able to be run from a computer. I run my own server and I want some programs to be forced upon the user and un-editable, I already have got some help with some code to put and keep the program on a given computer.

Thanks for the help!
OmegaVest #2
Posted 31 August 2012 - 07:13 PM
I don't think so. At least, not in this version. I think MysticT was working on something that would do that, and he might have put it into MysticOS, but I don't know. I haven't used it yet.
Matrixmage #3
Posted 31 August 2012 - 07:20 PM
Not really. At least, not in this version. I think MysticT was working on something that would do that, and he might have put it into MysticOS, but I don't know. I haven't used it yet.

ok thanks, if anyone else has a idea i would appreciate the input
ardera #4
Posted 31 August 2012 - 07:56 PM
Im not sure if you can use it for whole programs, but you can convert an file into a function with loadfile(file) and then use string.dump(function) to compile a function in binary code. Then you could write it to an file.

CODE:

args={...}
function printUsage()
  print("Usage: compile <program> <compiledprogram>")
end
if #args<1 or #args>2 then
  printUsage()
  return
end
p1=args[1]
p2=args[2]
if not fs.exists(p1) or fs.isDir(p1) then
  print("File doesn't exist or is a dir")
  return
end
if fs.exists(p2) then
  print("File exists.")
  return
end
local r=fs.open(p1, "r")
body=r.read()
r.close()
r=nil
local w=fs.open(p2, "w")
local str=string.dump(body) --compiles the string
local w.write(str)
w.close()

This should work: 1st arg=Program to compile, 2nd arg=Output File

And you should can run this like an normal program…


An other way is to download the StrUtils API from tomass1997 and encrypt the file with a key, and then you can decrypt the file on an other PC using the decrypt function of the StrUtils API.
Matrixmage #5
Posted 01 September 2012 - 06:29 PM
Im not sure if you can use it for whole programs, but you can convert an file into a function with loadfile(file) and then use string.dump(function) to compile a function in binary code. Then you could write it to an file.

CODE:

args={...}
function printUsage()
  print("Usage: compile <program> <compiledprogram>")
end
if #args<1 or #args>2 then
  printUsage()
  return
end
p1=args[1]
p2=args[2]
if not fs.exists(p1) or fs.isDir(p1) then
  print("File doesn't exist or is a dir")
  return
end
if fs.exists(p2) then
  print("File exists.")
  return
end
local r=fs.open(p1, "r")
body=r.read()
r.close()
r=nil
local w=fs.open(p2, "w")
local str=string.dump(body) --compiles the string
local w.write(str)
w.close()

This should work: 1st arg=Program to compile, 2nd arg=Output File

And you should can run this like an normal program…


An other way is to download the StrUtils API from tomass1997 and encrypt the file with a key, and then you can decrypt the file on an other PC using the decrypt function of the StrUtils API.

I just have one question, if I use this API would it have to be on a server or just my client?
Matrixmage #6
Posted 01 September 2012 - 09:40 PM
is anyone going to answer?
MysticT #7
Posted 01 September 2012 - 10:36 PM
I just have one question, if I use this API would it have to be on a server or just my client?
You just need to run it once to "compile" the program, and then use it like a normal program. So you just need to put it in a computer to compile the program and then distribute the compiled file.
Matrixmage #8
Posted 01 September 2012 - 10:39 PM
I just have one question, if I use this API would it have to be on a server or just my client?
You just need to run it once to "compile" the program, and then use it like a normal program. So you just need to put it in a computer to compile the program and then distribute the compiled file.

I was talking about the StrUtils API that he mentioned, I just need to know if it needs to be on the server and the client or just the client.
MysticT #9
Posted 01 September 2012 - 10:52 PM
Oh, sorry didn't get that :)/>/>
Well, you would need to have it when encrypting and decrypting it, so it has to be in both. But you can't run the program while encrypted, you need to decrypt it to a new file and then run it.
Matrixmage #10
Posted 01 September 2012 - 11:05 PM
Oh, sorry didn't get that :)/>/>
Well, you would need to have it when encrypting and decrypting it, so it has to be in both. But you can't run the program while encrypted, you need to decrypt it to a new file and then run it.

Ok… so only the two people who are encrypting and decrypting need the api?
Also is there any way at all to make a program un-readable but sill run-able?
MysticT #11
Posted 01 September 2012 - 11:09 PM
You can compile it like ardera said. But it can't be decompiled, so you need to keep a copy of the source if you want to modify it.
Matrixmage #12
Posted 02 September 2012 - 02:45 AM
Ok, thanks for all you help!
Matrixmage #13
Posted 02 September 2012 - 03:15 AM
Im not sure if you can use it for whole programs, but you can convert an file into a function with loadfile(file) and then use string.dump(function) to compile a function in binary code. Then you could write it to an file.

CODE:

args={...}
function printUsage()
  print("Usage: compile <program> <compiledprogram>")
end
if #args<1 or #args>2 then
  printUsage()
  return
end
p1=args[1]
p2=args[2]
if not fs.exists(p1) or fs.isDir(p1) then
  print("File doesn't exist or is a dir")
  return
end
if fs.exists(p2) then
  print("File exists.")
  return
end
local r=fs.open(p1, "r")
body=r.read()
r.close()
r=nil
local w=fs.open(p2, "w")
local str=string.dump(body) --compiles the string
local w.write(str)
w.close()

This should work: 1st arg=Program to compile, 2nd arg=Output File

And you should can run this like an normal program…


An other way is to download the StrUtils API from tomass1997 and encrypt the file with a key, and then you can decrypt the file on an other PC using the decrypt function of the StrUtils API.

I ran your code and I got unexpected symbol on line 25…
Can I get some help?
MysticT #14
Posted 02 September 2012 - 03:20 AM
Remove the local from that line. It should be:

w.write(str)
Matrixmage #15
Posted 02 September 2012 - 03:25 AM
Thanks Mystic, now it works!
Matrixmage #16
Posted 02 September 2012 - 03:37 AM
Ok, never mind it doesn't work, if I understand, the first arg is the file to compile, and the second is where to put it, either I don't know how to write where I want it to go or it really doesn't work, I get a attempt to call nil on line 20.