how to encrypt python source code?

13,165

Solution 1

I would suggest you go back into thinking about this, considering:

  • Use the right tool for the job
  • Obfuscation is hard
  • Other means to achieve your goals

Firstly, Python was not designed to be obfuscated. Every aspect of the language is free and accessible to anybody who wants to inspect or modify it. Being a bytecode language makes it difficult to lock down, and Python bytecode is easy to understand. If you want to build something you can't see inside, you will have to use another tool.

Secondly, everything (literally) can be reverse-engineered eventually, so do not assume you'll be able to fully protect any piece of code. You must be able to understand the tradeoff between the importance of hiding a piece of code (for an estimate amount X of resources) versus how useful hiding it actually is (also in terms of effort). Try and realistically evaluate how important your "design and implementation" really is, to justify all this.

Consider having legal requirements. If you expect people will misuse your code, maybe it would be more useful if you could easily discover the ones that do and turn this into a legal issue.

Solution 2

separate confidential functionality in C functions and develop SWIG wrappers. If you are using C++, you can consider boost python.

Solution 3

Anything can be reverse engineered. It is not possible to give a user's machine information without the possibility for the user to examine that information. All you can do is make it take more effort.

Python is particularly bad if you have this requirement, because Python bytecode is much easier to read than fully assembled machine code. Ultimately whatever you do to make it more obfuscated, the user's computer will have to be able to de-obfuscate it to turn it into normal Python bytecode in order for the Python interpreter to exectute it. Therefore a motivated user is going to be able to de-obfuscate whatever you give them into Python bytecode as well.

If you really have rivals who are likely to want to figure out how your programs work, you must assume that any code you release to end users in any form will be fully understood by your rivals. There is no possible way to absolutely guard against this.

The only way you can get around this is to not give your users this code either, if you can run your code on a server under your control, and only give your users a dumb program that makes requests to your server for the real work.

Share:
13,165
hittlle
Author by

hittlle

Updated on June 04, 2022

Comments

  • hittlle
    hittlle almost 2 years

    we have a business-critical program implemented in Python. Our boss don't want others, especially our rivals to know how it is designed and implemented. So I have to find a way to encrypt it. I first thought of pyc and pyo, but soon I found that they are likely to be disassembled. I wanna encrypt our source codes, but i don't know how to do it? Could you guys please help me with this? Any guidance would be highly appreciated.

    • mac
      mac almost 13 years
      Python is not the right tool for the job in this case. Consider offering your software as a service (= web application = you do not to have to distribute your bytecode as it will run on your server)