xtn-tools-pro 1.0.1.3.0__py3-none-any.whl → 1.0.1.3.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,161 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ # 说明:
5
+ # 加解密、编解码
6
+ # History:
7
+ # Date Author Version Modification
8
+ # --------------------------------------------------------------------------------------------------
9
+ # 2024/5/13 xiatn V00.01.000 新建
10
+ # --------------------------------------------------------------------------------------------------
11
+ import jwt
12
+ import time
13
+ import json
14
+ import pyotp
15
+ import base64
16
+ import hashlib
17
+ import secrets
18
+ from Crypto.PublicKey import RSA
19
+ from Crypto.Cipher import AES, PKCS1_OAEP
20
+ from Crypto.Util.Padding import pad, unpad
21
+ from Crypto.Random import get_random_bytes
22
+
23
+
24
+ class XtnDeEnAir:
25
+ BLOCK_SIZE = AES.block_size
26
+
27
+ @staticmethod
28
+ def de_data(key: bytes, raw: dict) -> str:
29
+ """
30
+ 加密
31
+ """
32
+ cipher = AES.new(key, AES.MODE_ECB)
33
+ raw["time"] = int(time.time())
34
+ raw_s = json.dumps(raw)
35
+ padded_data = pad(raw_s.encode('utf-8'), XtnDeEnAir.BLOCK_SIZE)
36
+ enc = cipher.encrypt(padded_data)
37
+ result = base64.b64encode(enc).decode('utf-8')
38
+ return result
39
+
40
+ @staticmethod
41
+ def en_data(key: bytes, raw: str) -> str:
42
+ """
43
+ 解密
44
+ """
45
+ cipher = AES.new(key, AES.MODE_ECB)
46
+ enc = base64.b64decode(raw)
47
+ decrypted = cipher.decrypt(enc)
48
+ result = unpad(decrypted, XtnDeEnAir.BLOCK_SIZE).decode('utf-8')
49
+ return result
50
+
51
+
52
+ class XtnDeEnPro:
53
+ BLOCK_SIZE = AES.block_size
54
+
55
+ @staticmethod
56
+ def encrypt_dict(key: bytes, raw: dict) -> str:
57
+ """
58
+ 加密
59
+ """
60
+ raw["encrypt_time"] = int(time.time())
61
+ raw_json = json.dumps(data).encode('utf-8')
62
+ padded_data = pad(raw_json, XtnDeEnPro.BLOCK_SIZE)
63
+ iv = get_random_bytes(XtnDeEnPro.BLOCK_SIZE)
64
+ cipher = AES.new(key, AES.MODE_CBC, iv)
65
+ encrypted = cipher.encrypt(padded_data)
66
+ combined = iv + encrypted
67
+ result = base64.b64encode(combined).decode('utf-8')
68
+ # result = combined.hex() # hex
69
+ return result
70
+
71
+ @staticmethod
72
+ def decrypt_dict(key: bytes, raw: str) -> dict:
73
+ """
74
+ 解密
75
+ """
76
+ try:
77
+ combined = base64.b64decode(raw)
78
+ # combined = bytes.fromhex("") # hex
79
+ iv = combined[:XtnDeEnPro.BLOCK_SIZE]
80
+ encrypted = combined[XtnDeEnPro.BLOCK_SIZE:]
81
+ cipher = AES.new(key, AES.MODE_CBC, iv)
82
+ decrypted = unpad(cipher.decrypt(encrypted), XtnDeEnPro.BLOCK_SIZE)
83
+ result = decrypted.decode('utf-8')
84
+ return json.loads(result)
85
+ except (ValueError, KeyError, json.JSONDecodeError) as e:
86
+ raise ValueError(f"解密失败: {e}")
87
+
88
+
89
+ class XtnDeEnProMax:
90
+ BLOCK_SIZE = AES.block_size
91
+
92
+ @staticmethod
93
+ def encrypt_dict(key: bytes, raw: dict) -> str:
94
+ """
95
+ 加密
96
+ """
97
+ session_key = get_random_bytes(16)
98
+
99
+ rsa_key = RSA.import_key(key)
100
+ rsa_cipher = PKCS1_OAEP.new(rsa_key)
101
+ encrypted_session_key = rsa_cipher.encrypt(session_key)
102
+
103
+ iv = get_random_bytes(16)
104
+ aes_cipher = AES.new(session_key, AES.MODE_CBC, iv)
105
+ raw["encrypt_time"] = int(time.time())
106
+ raw_data = json.dumps(raw).encode('utf-8')
107
+ encrypted_data = aes_cipher.encrypt(pad(raw_data, XtnDeEnProMax.BLOCK_SIZE))
108
+
109
+ combined = (
110
+ len(encrypted_session_key).to_bytes(2, 'big') +
111
+ encrypted_session_key +
112
+ iv +
113
+ encrypted_data
114
+ )
115
+
116
+ result_base64 = base64.b64encode(combined).decode()
117
+ # result_hex = combined.hex() # hex
118
+ return result_base64
119
+
120
+ @staticmethod
121
+ def decrypt_dict(key: bytes, raw: str) -> dict:
122
+ """
123
+ 解密
124
+ """
125
+ try:
126
+ combined = base64.b64decode(raw)
127
+ key_len = int.from_bytes(combined[:2], 'big')
128
+ encrypted_session_key = combined[2:2 + key_len]
129
+ iv = combined[2 + key_len:2 + key_len + 16]
130
+ encrypted_data = combined[2 + key_len + 16:]
131
+
132
+ rsa_key = RSA.import_key(key)
133
+ rsa_cipher = PKCS1_OAEP.new(rsa_key)
134
+ session_key = rsa_cipher.decrypt(encrypted_session_key)
135
+
136
+ aes_cipher = AES.new(session_key, AES.MODE_CBC, iv)
137
+ decrypted = unpad(aes_cipher.decrypt(encrypted_data), XtnDeEnProMax.BLOCK_SIZE)
138
+ result = decrypted.decode('utf-8')
139
+ return json.loads(result)
140
+ except (ValueError, KeyError, json.JSONDecodeError) as e:
141
+ raise ValueError(f"解密失败: {e}")
142
+
143
+
144
+ if __name__ == '__main__':
145
+ key = b"d9fe4b29dc7decc8360e72e7dd15a5c8"
146
+ data = {
147
+ "task_type": "update_name",
148
+ "task_client": "tiktok",
149
+ }
150
+ sss = XtnDeEnAir.de_data(key, data)
151
+ print(sss)
152
+ print(XtnDeEnAir.en_data(key, sss))
153
+
154
+ key_pair = RSA.generate(2048)
155
+ private_key = key_pair.export_key()
156
+ public_key = key_pair.publickey().export_key()
157
+ print(private_key)
158
+ print(public_key)
159
+ sss = XtnDeEnProMax.encrypt_dict(public_key, data)
160
+ print(sss)
161
+ print(XtnDeEnProMax.decrypt_dict(private_key, sss))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xtn-tools-pro
3
- Version: 1.0.1.3.0
3
+ Version: 1.0.1.3.1
4
4
  Summary: xtn 开发工具
5
5
  Author: xtn
6
6
  Author-email: czw011122@gmail.com
@@ -55,6 +55,7 @@ xtn_tools_pro/task_pro/go_fun_v3.py,sha256=GI1OVup-izwDflAaoy_pN6xmCXat59II7v-Mo
55
55
  xtn_tools_pro/utils/__init__.py,sha256=I1_n_NP23F2lBqlF4EOlnOdLYxM8M4pbn63UhJN1hRE,418
56
56
  xtn_tools_pro/utils/audio_video.py,sha256=3Im3tGOW6IbmqDcrE1SJUg6eGyLGEyq11Qu6TCutJJw,1115
57
57
  xtn_tools_pro/utils/crypto.py,sha256=B6NC2yVxtwrbSZnngudTFRm7YRGKKEusydVVYWwxFHs,5231
58
+ xtn_tools_pro/utils/crypto_pro.py,sha256=CJqK-fJryblKsj2uQN7_Sy8gn7HPCeage53xk1DfQLc,5331
58
59
  xtn_tools_pro/utils/file_utils.py,sha256=vsUgds8I8Z2pL9a4oCv1q0JrZsDP29qLyLyGVqj5sX8,3311
59
60
  xtn_tools_pro/utils/helpers.py,sha256=0CTMY7wfSQR_DC9-v1lkKQLpWcB1FL9V_kw_5DOcZ40,4454
60
61
  xtn_tools_pro/utils/log.py,sha256=MPT9q1keNYroaw6U9a-Gc0iT0ceUEVTI-Tz2GEiWLUc,11038
@@ -63,9 +64,9 @@ xtn_tools_pro/utils/set_data.py,sha256=UR5S8xBqc4Vk8zpbrNVsxKhwaBAenaEdC7O-N3s7h
63
64
  xtn_tools_pro/utils/shell.py,sha256=s6sJedxLLQokcI1hF5YSD3qgGUPK0brNcP-D3-yv54I,3790
64
65
  xtn_tools_pro/utils/sql.py,sha256=EAKzbkZP7Q09j15Gm6o0_uq0qgQmcCQT6EAawbpp4v0,6263
65
66
  xtn_tools_pro/utils/time_utils.py,sha256=TUtzG61PeVYXhaQd6pBrXAdlz7tBispNIRQRcGhE2No,4859
66
- xtn_tools_pro-1.0.1.3.0.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
67
- xtn_tools_pro-1.0.1.3.0.dist-info/METADATA,sha256=43e7Vi5-dnn6wCUNT4esYBCOa2Dc68jFNXcuM-siDfY,574
68
- xtn_tools_pro-1.0.1.3.0.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
69
- xtn_tools_pro-1.0.1.3.0.dist-info/entry_points.txt,sha256=t8CtXWOgw7nRDW3XNlZh8MT_P4l8EzsFnyWi5b3ovrc,68
70
- xtn_tools_pro-1.0.1.3.0.dist-info/top_level.txt,sha256=jyB3FLDEr8zE1U7wHczTgIbvUpALhR-ULF7RVEO7O2U,14
71
- xtn_tools_pro-1.0.1.3.0.dist-info/RECORD,,
67
+ xtn_tools_pro-1.0.1.3.1.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
68
+ xtn_tools_pro-1.0.1.3.1.dist-info/METADATA,sha256=Ivrk4YS7pi60sMP7xUq0323sBBR-9Cpfn3u8MkOkML4,574
69
+ xtn_tools_pro-1.0.1.3.1.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
70
+ xtn_tools_pro-1.0.1.3.1.dist-info/entry_points.txt,sha256=t8CtXWOgw7nRDW3XNlZh8MT_P4l8EzsFnyWi5b3ovrc,68
71
+ xtn_tools_pro-1.0.1.3.1.dist-info/top_level.txt,sha256=jyB3FLDEr8zE1U7wHczTgIbvUpALhR-ULF7RVEO7O2U,14
72
+ xtn_tools_pro-1.0.1.3.1.dist-info/RECORD,,