Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide/protect Python code [duplicate]

I am writing code (Python and wxpython for GUI) which will run on Debian OS on Raspberry PI. I want to protect/hide the source code. Is there any way to do it? Probably py2exe, or converting it to a library or something else?

like image 819
Ashish Avatar asked Jan 11 '14 18:01

Ashish


People also ask

How can I protect my python code but still make it available to run?

The best solution to this vulnerability is to encrypt Python source code. Encrypting Python source code is a method of “Python obfuscation,” which has the purpose of storing the original source code in a form that is unreadable to humans.

Can python be obfuscated?

There is no effective way to obfuscate python such that it can not be trivially converted back to human readable. If you have code that valuable, convert it to C, or keep it on a server.

Can python source code be hidden?

To a determined user, you can't. From a practical standpoint, you can do some tricks, such as wrapping it into a binary executable or using obfuscation. Save this answer.


1 Answers

The compiled code (.pyc files) can be used if you wish for others to be able to execute but not to read or modify the source code (.py, .pyw).

Simply:

  1. run your application
  2. then copy all the relevant .pyc files into another folder and you should be able to
  3. run it all from the new location

So long as all the appropriate modules are still able to be loaded, everything will work. This will require the version of python to be the same (can't run .pyc files from python 2.4 with python 2.7 and vice-versa)

The other thing to know is that strings will be preserved. You should open them up in a good text editor (I use vim) and inspect the content if you are worried about what others can see.

py2exe is of course another example, but you lose the ability to have cross-platform code at that point -- and if your application is for the Raspberry Pi -- that won't work.

Since you provided no other information about how you intend to run the code, it's not clear if the source will be a module or intended to be run directly. You should read this post to learn more.

like image 90
xobes Avatar answered Oct 13 '22 15:10

xobes