How to encrypt python source code?

we have a business critical program implemented in Python. Our boss does not want others, especially our rivals, to know how it is designed and implemented. So I have to find a way to encrypt it. I thought of pyc and pyo at first, but soon I discovered that they were likely to be taken apart. I want to encrypt our source codes, but I do not know how to do this? Could you guys help me with this? Any guidance would be greatly appreciated.

+6
source share
3 answers

I would suggest you come back to thinking about this, given:

  • Use the right tool for the job.
  • Hard obfuscation
  • Other means to achieve your goals.

First, Python was not intended to be obfuscated. Every aspect of the language is free and accessible to anyone who wants to check or change it. Being a bytecode language makes locking difficult, and Python bytecode is easy to understand. If you want to create something that you do not see inside, you will have to use another tool.

Secondly, everything (literally) can eventually be reconstructed, so do not assume that you can completely protect any piece of code. Even Skype was broken and analyzed. You must understand the tradeoff between the importance of hiding a piece of code (to estimate the number of X resources) compared to how useful it is to hide it (also in terms of effort). Try and realistically evaluate how important your “design and implementation” is to justify all this.

Consider legal requirements. If you expect people to abuse your code, it might be more helpful if you could easily find the ones that do this and turn this into a legal problem.

+14
source

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

+4
source

Everything that can be reconstructed. Impossible provide information about the user machine without the ability for the user to examine this information. All you can do is make more effort.

Python is especially bad if you have this requirement, because Python bytecode is much easier to read than fully compiled machine code. Ultimately, whatever you do to make it more confusing, the user computer must be able to confuse it in order to turn it into regular Python bytecode so that the Python interpreter can excrement it. Therefore, a motivated user will be able to de-confuse everything you give them in Python bytecode.

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

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

+4
source

Source: https://habr.com/ru/post/891999/


All Articles