1

I am writing a python code that I plan to distribute to others.

The problem is, I want to be able to encrypt the code so that they can not read into any code that I have written and therefore can not steal my ideas and make copies of it while still being able to use the program as I intend.

Does anyone have experience in this or at least any suggestions on where to look?

pjs
  • 18,696
  • 4
  • 27
  • 56
ChristopherOjo
  • 237
  • 1
  • 12

1 Answers1

3

As far as my knowledge goes, this is impossible. If you share a python script with other individuals you are entrusting them to not leak any of the source code that you may deem confidential. If you encrypt the source code, then the python's interpreter will not be able to interpret the code (and, therefore, the people you send the encrypted python script will not be able to execute it unless you give them the key--which, in that case, you might as well send it in clear text). No matter what you do, since Python is an interpreted language, you will have to make that script available to them in clear text (regardless of how intricate you try to make the script in order to deter them from viewing it). If you don't mind me asking: what kind of program are you trying to share with them? For example, if you made the application available on the web (and strictly coded the important trade secret algorithms with a server-side language) then they would never come in contact with that source code. Or, although I wouldn't recommend this, you could send them the script coded in a compiled language (and therefore send them the binary)--but beware that there are plenty of really smart people that know how to pick apart binary executables. If you are creating a python script for production that needs to be coded in python, then I would suggest getting a license that makes it illegal for people to copy your code without your consent.

TwoFace
  • 101
  • 4
  • I have heard that some people have acheived an acceptable result by wrapping the python up in a C++ object? I dont fully understand what that means, but it seems to make some sense. Have you heard of this before? – ChristopherOjo Aug 03 '21 at 22:38
  • @ChristopherOjo [Here](https://docs.python.org/3/extending/extending.html) is the official documentation on how to extend Python with C or C++. However, unless you are simply attempting to achieve obscurity, and not 100% prevention of people from being able to view it, I would warn you that extending anything in C++ for Python will just make the code more confusing to read--but will not prevent someone with an extremely dedicated heart from dissecting it. Maybe if you explain a bit more about your program and what it does I can be of more help. – TwoFace Aug 03 '21 at 23:39
  • My code is a trading bot. There are some strategies that I don't want people to dissect and figure out through the code. – ChristopherOjo Aug 04 '21 at 15:06
  • @ChristopherOjo [Here](https://stackoverflow.com/questions/261638/how-do-i-protect-python-code-from-being-read-by-users) is another thread that discusses your issue. My two cents is that, no matter how difficult you make the code to read, someone can reverse engineer a python script (even if some of it is in C++, as they can reverse engineer the binary). You could make it incredibly hard to read the code with various techniques, but that won't make it impossible for someone to reverse engineer it. I would suggest getting a license for it making it illegal for anyone to copy it. – TwoFace Aug 04 '21 at 15:37
  • 1
    @ChristopherOjo Just a thought here: is it possible to make your bot a web service? If so, you could create a website with your python code as the server-side language (with something like python flask): that way the customer wouldn't come in contact with the code. – TwoFace Aug 04 '21 at 15:40
  • Yeah, I thought about that, but in order to deal with security less, I wanted the program to run on the user's computer. That way they can store all their passwords locally and that is not something that I would have to deal with. – ChristopherOjo Aug 04 '21 at 16:08
  • It is possible, I found [`PyArmor`](https://pyarmor.dashingsoft.com/index-zh.html) can do this. – jett chen Feb 26 '22 at 02:38