How to encrypt .py files using my own encryption algorithm

56 views
Skip to first unread message

Gal Shtengel

unread,
Sep 9, 2020, 6:10:59 AM9/9/20
to PyInstaller
Hi pyinstaller team!

I'm facing an issue i can't find a solution for if you could help me please.

Background:
I've written an Python program and in order to protect my source code iv'e encrypted every .py file using AES. (except for the main.py and decrypt.py)

decrypt.py is an import hook that decrypt the encrypted files when main.py imports them.
decrypt.py is special in a sense that the key is stored on my server and he can retrieve the key by using username,password authentication.

im using main.spec file where runtime_hooks=['decrypt.py'] (so my import hook will be loaded when main.py is running)

what i want is to achieve is this:
I want to encrypt each file PyInstaller collects (only my source code not everything) so that the folder with all my scripts will contain encrypted scripts

bwoodsend

unread,
Sep 9, 2020, 9:40:26 AM9/9/20
to PyInstaller

I’m afraid by using raw .py files you’re going against what PyInstaller does. It compiles you .pys to .pycs then bundles those into a PYZ archive so that there is a) no folder containing your scripts and b) even if there was, the scripts would be pre-compiled.

That being said, it should be possible (although not really recommended) to do this. You can tell PyInstaller to add your encrypted Python scripts as data files by adding them to the datas=[] in the spec file (please do not debug in onefile mode). Exactly what you put in there depends on how you’re importing your encrypted code - whether files are loaded using Python’s import mechanisms or read from file, and where these scripts are located relative to main.py.

Reply all
Reply to author
Forward
0 new messages