Skip to content

Commit a260009

Browse files
committed
fix: 🐛 support append sys.path
1 parent f60db00 commit a260009

File tree

5 files changed

+15
-30
lines changed

5 files changed

+15
-30
lines changed

pyencrypt/aes.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -848,21 +848,3 @@ def aes_decrypt(data: bytes, key: str) -> bytes:
848848
plain.append(AESModeOfOperationECB(key).decrypt(x))
849849
return strip_padding(b''.join(plain))
850850

851-
852-
if __name__ == '__main__':
853-
plain = '你好!世界!'
854-
key = 'tB9qW0YGlYIyBfTmuyQbm6AjPQa9gTKwO8j5H4IBf1A='
855-
plain = add_padding(plain.encode())
856-
print('plain', plain)
857-
cipher = AESModeOfOperationECB(key).encrypt(plain)
858-
print('cipher', cipher)
859-
plain = strip_padding(AESModeOfOperationECB(key).decrypt(cipher))
860-
print(plain)
861-
print(plain.decode())
862-
863-
with open('generate.py', 'rb') as f:
864-
data = f.read()
865-
cipher = aes_encrypt(data, key)
866-
print(cipher)
867-
plain = aes_decrypt(cipher, key)
868-
print(plain.decode())

pyencrypt/decrypt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pyencrypt.ntt import intt
55

66

7-
def decrypt_key(cipher_key: str, n: int, d: int) -> str:
7+
def decrypt_key(cipher_key: str, d: int, n: int) -> str:
88
plain_ls = list()
99
for num in map(int, cipher_key.split('O')):
1010
plain_ls.append(pow(num, d, n))

pyencrypt/encrypt.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
def _encrypt_file(
1515
data: bytes,
1616
key: bytes,
17-
) -> None:
17+
) -> bytes:
1818
return aes_encrypt(data, key)
1919

2020

@@ -44,7 +44,7 @@ def encrypt_key(key: bytes) -> str:
4444
return 'O'.join(map(str, cipher_ls)), numbers['d'], numbers['n']
4545

4646

47-
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):
4848
private_key = f'{n}O{d}'
4949
path = Path(os.path.abspath(__file__)).parent
5050

@@ -62,7 +62,10 @@ def generate_so_file(cipher_key: str, d: int, n: int):
6262
1).replace("__cipher_key = ''", f"__cipher_key = '{cipher_key}'",
6363
1).replace("from pyencrypt.decrypt import *", '')
6464

65-
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'
6669
temp_dir.mkdir(exist_ok=True)
6770
loader_file_path = temp_dir / 'loader.py'
6871
loader_file_path.touch(exist_ok=True)
@@ -97,6 +100,7 @@ def generate_so_file(cipher_key: str, d: int, n: int):
97100
encoding='utf-8')
98101
if ret.returncode == 0:
99102
pass
103+
return True
100104

101105

102106
def encrypt_file(path: Path,

pyencrypt/generate.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
44
from Crypto.PublicKey import RSA
55

66

7-
def generate_aes_key(size: int = 32) -> str:
7+
def generate_aes_key(size: int = 32) -> bytes:
88
return base64.b64encode(os.urandom(size))
99

1010

11-
def generate_rsa_number(bits: int):
11+
def generate_rsa_number(bits: int)-> dict:
1212
r = RSA.generate(bits)
1313
return {'p': r.p, 'q': r.q, 'n': r.n, 'e': r.e, 'd': r.d}
1414

15-
16-
if __name__ == '__main__':
17-
print(generate_aes_key(32))
18-
print(generate_rsa_number(2048))

pyencrypt/loader.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def get_data(self, path: _Path) -> bytes:
3333
try:
3434
__n, __d = self.__private_key.split('O', 1)
3535
return decrypt_file(
36-
Path(path), decrypt_key(self.__cipher_key, int(__n), int(__d)))
36+
Path(path), decrypt_key(self.__cipher_key, int(__d), int(__n)))
3737
except Exception:
3838
traceback.print_exc()
3939
return b''
@@ -51,7 +51,10 @@ def find_spec(self,
5151
else:
5252
file_path = Path(path[0]) / f'{fullname.rsplit(".",1)[-1]}.pye'
5353
else:
54-
file_path = Path(f'{fullname}.pye')
54+
for p in sys.path:
55+
file_path = Path(p) / f'{fullname}.pye'
56+
if file_path.exists():
57+
break
5558
file_path = file_path.absolute().as_posix()
5659
if not os.path.exists(file_path):
5760
return None

0 commit comments

Comments
 (0)