Due to the confidentiality needs of the project , The developed projects need to be encrypted , This paper gives two methods .
encryption py Code , Although compiled as pyc Not much ( It is easy to decompile the source code ), But it still has some encryption effect , If the project is tight , This method can be used for emergency treatment .
python -m compileall -f -q -b "py Folder "
This command will send py Files will generate corresponding pyc file
Then execute the following command , Delete the py file , Only keep pyc file
find . -name "*.py" -type f -print -exec rm -rf { } \;
Finally, execute the run pyc Just file it
Such as : python ****.pyc
Be careful : This method encrypts and uses python The version should be consistent , Otherwise, an error will be reported . I.e. operation python -m compileall -f -q -b "py Folder "
And python ****.pyc
The version should be consistent .
pip3 install Cython sudo apt-get update sudo apt-get install python-devel sudo apt-get install gcc
Fill in the following :
#-* -coding: UTF-8 -* - """ The premise of execution : System installation python-devel and gcc Python install cython Compile a folder : python py2so.py BigoModel Generate results : Catalog build Next After the build is complete : The startup file also needs py/pyc Take on , Must be activated py/pyc Copy to the compilation directory and delete so file """ import sys, os, shutil, time from distutils.core import setup from Cython.Build import cythonize starttime = time.time() setupfile= os.path.join(os.path.abspath('.'), __file__) def getpy(basepath=os.path.abspath('.'), parentpath='', name='', build_dir="build", excepts=(), copyOther=False, delC=False): """ obtain py Path to file :param basepath: The root path :param parentpath: Parent path :param name: file / clip :param excepts: Exclude files :param copy: whether copy Other documents :return: py Iterator for files """ fullpath = os.path.join(basepath, parentpath, name) for fname in os.listdir(fullpath): ffile = os.path.join(fullpath, fname) if os.path.isdir(ffile) and ffile != os.path.join(basepath, build_dir) and not fname.startswith('.'): for f in getpy(basepath, os.path.join(parentpath, name), fname, build_dir, excepts, copyOther, delC): yield f elif os.path.isfile(ffile): # print("\t", basepath, parentpath, name, ffile) ext = os.path.splitext(fname)[1] if ext == ".c": if delC and os.stat(ffile).st_mtime > starttime: os.remove(ffile) elif ffile not in excepts and ext not in('.pyc', '.pyx'): # print("\t\t", basepath, parentpath, name, ffile) if ext in('.py', '.pyx') and not fname.startswith('__'): yield os.path.join(parentpath, name, fname) elif copyOther: dstdir = os.path.join(basepath, build_dir, parentpath, name) if not os.path.isdir(dstdir): os.makedirs(dstdir) shutil.copyfile(ffile, os.path.join(dstdir, fname)) else: pass if __name__ == "__main__": currdir = os.path.abspath('.') parentpath = sys.argv[1] if len(sys.argv)>1 else "." currdir, parentpath = os.path.split(currdir if parentpath == "." else os.path.abspath(parentpath)) build_dir = os.path.join(parentpath, "build") build_tmp_dir = os.path.join(build_dir, "temp") print("start:", currdir, parentpath, build_dir) os.chdir(currdir) try: # obtain py list module_list = list(getpy(basepath=currdir,parentpath=parentpath, build_dir=build_dir, excepts=(setupfile))) print(module_list) setup(ext_modules = cythonize(module_list),script_args=["build_ext", "-b", build_dir, "-t", build_tmp_dir]) module_list = list(getpy(basepath=currdir, parentpath=parentpath, build_dir=build_dir, excepts=(setupfile), copyOther=True)) except Exception as ex: print("error! ", ex) finally: print("cleaning...") module_list = list(getpy(basepath=currdir, parentpath=parentpath, build_dir=build_dir, excepts=(setupfile), delC=True)) if os.path.exists(build_tmp_dir): shutil.rmtree(build_tmp_dir) print("complate! time:", time.time()-starttime, 's')
python py2so.py project_dir_path
At the end of the run , project_dir_path
Will generate a build
Folder , Put the .os All files are moved to the corresponding location of the encrypted folder , Delete py file , If it is a startup file , It is suggested to keep , You can run the program through the startup file .
take py File compiled into pyc:
Python A standard library is provided with the name compileall
The library of , It's easy to compile . Easy and convenient , Raised a little source code crack threshold
Good platform compatibility , py
Where can I run , pyc
Where to run . But there are ready-made decompilers ( python-uncompyle6
), The cost of cracking is low . It is suggested that the project time is tight , Use in case of emergency .
take py Turn it into so file :
Cython Method encryption is to encrypt py
The file to so
file , use so
File replacement py
file , The execution rate will be faster than python faster , But sometimes I encounter some code that doesn't work well , Only test against , The invalid ones will not be encrypted or encrypted in other ways . Good encryption effect , Not easy to decompile , But the project needs to be tested step by step .