1414def _encrypt_file (
1515 data : bytes ,
1616 key : bytes ,
17- ) -> None :
17+ ) -> bytes :
1818 return aes_encrypt (data , key )
1919
2020
@@ -32,14 +32,19 @@ def encrypt_key(key: bytes) -> str:
3232 ascii_ls = [ord (x ) for x in key .decode ()]
3333 numbers = generate_rsa_number (2048 )
3434 e , n = numbers ['e' ], numbers ['n' ]
35+ # fill length to be a power of 2
36+ length = len (ascii_ls )
37+ if length & (length - 1 ) != 0 :
38+ length = 1 << length .bit_length ()
39+ ascii_ls = ascii_ls + [0 ] * (length - len (ascii_ls ))
3540 cipher_ls = list ()
3641 # ntt后再用RSA加密
3742 for num in ntt (ascii_ls ):
3843 cipher_ls .append (pow (num , e , n ))
3944 return 'O' .join (map (str , cipher_ls )), numbers ['d' ], numbers ['n' ]
4045
4146
42- def generate_so_file (cipher_key : str , d : int , n : int ):
47+ def generate_so_file (cipher_key : str , d : int , n : int , base_dir : Path = None ):
4348 private_key = f'{ n } O{ d } '
4449 path = Path (os .path .abspath (__file__ )).parent
4550
@@ -57,7 +62,10 @@ def generate_so_file(cipher_key: str, d: int, n: int):
5762 1 ).replace ("__cipher_key = ''" , f"__cipher_key = '{ cipher_key } '" ,
5863 1 ).replace ("from pyencrypt.decrypt import *" , '' )
5964
60- temp_dir = Path (os .getcwd ()) / 'encrypted'
65+ if base_dir is None :
66+ base_dir = Path (os .getcwd ())
67+
68+ temp_dir = base_dir / 'encrypted'
6169 temp_dir .mkdir (exist_ok = True )
6270 loader_file_path = temp_dir / 'loader.py'
6371 loader_file_path .touch (exist_ok = True )
@@ -70,7 +78,6 @@ def generate_so_file(cipher_key: str, d: int, n: int):
7078 loader_origin_file_path .touch (exist_ok = True )
7179 loader_origin_file_path .write_text (f"{ decrypt_source } \n { loader_source } " )
7280
73- setup_file_path = Path (os .path .abspath (__file__ )).parent / 'setup.py'
7481 args = [
7582 'pyminifier' , '--obfuscate-classes' , '--obfuscate-import-methods' ,
7683 '--replacement-length' , '20' , '-o' ,
@@ -81,17 +88,16 @@ def generate_so_file(cipher_key: str, d: int, n: int):
8188 if ret .returncode == 0 :
8289 pass
8390
84- args = [
85- 'python' ,
86- setup_file_path .as_posix (), 'build_ext' , '--build-lib' ,
87- temp_dir .as_posix ()
88- ]
89- ret = subprocess .run (args ,
90- shell = False ,
91- stderr = subprocess .PIPE ,
92- encoding = 'utf-8' )
93- if ret .returncode == 0 :
94- pass
91+ from setuptools import setup
92+ from Cython .Build import cythonize
93+ from Cython .Distutils import build_ext
94+ setup (
95+ ext_modules = cythonize (loader_file_path .as_posix (), language_level = "3" ),
96+ script_args = ['build_ext' , '--build-lib' , temp_dir .as_posix ()],
97+ cmdclass = {'build_ext' : build_ext },
98+ )
99+
100+ return True
95101
96102
97103def encrypt_file (path : Path ,
0 commit comments