xtn-tools-pro 1.0.1.3.0__py3-none-any.whl → 1.0.1.3.2__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,167 @@
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 encrypt_dict(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 decrypt_dict(key: bytes, raw: str) -> dict:
42
+ """
43
+ 解密
44
+ """
45
+ try:
46
+ cipher = AES.new(key, AES.MODE_ECB)
47
+ enc = base64.b64decode(raw)
48
+ decrypted = cipher.decrypt(enc)
49
+ result = unpad(decrypted, XtnDeEnAir.BLOCK_SIZE).decode('utf-8')
50
+ return json.loads(result)
51
+ except (ValueError, KeyError, json.JSONDecodeError) as e:
52
+ # raise ValueError(f"解密失败: {e}")
53
+ return {}
54
+
55
+
56
+ class XtnDeEnPro:
57
+ BLOCK_SIZE = AES.block_size
58
+
59
+ @staticmethod
60
+ def encrypt_dict(key: bytes, raw: dict) -> str:
61
+ """
62
+ 加密
63
+ """
64
+ raw["encrypt_time"] = int(time.time())
65
+ raw_json = json.dumps(data).encode('utf-8')
66
+ padded_data = pad(raw_json, XtnDeEnPro.BLOCK_SIZE)
67
+ iv = get_random_bytes(XtnDeEnPro.BLOCK_SIZE)
68
+ cipher = AES.new(key, AES.MODE_CBC, iv)
69
+ encrypted = cipher.encrypt(padded_data)
70
+ combined = iv + encrypted
71
+ result = base64.b64encode(combined).decode('utf-8')
72
+ # result = combined.hex() # hex
73
+ return result
74
+
75
+ @staticmethod
76
+ def decrypt_dict(key: bytes, raw: str) -> dict:
77
+ """
78
+ 解密
79
+ """
80
+ try:
81
+ combined = base64.b64decode(raw)
82
+ # combined = bytes.fromhex("") # hex
83
+ iv = combined[:XtnDeEnPro.BLOCK_SIZE]
84
+ encrypted = combined[XtnDeEnPro.BLOCK_SIZE:]
85
+ cipher = AES.new(key, AES.MODE_CBC, iv)
86
+ decrypted = unpad(cipher.decrypt(encrypted), XtnDeEnPro.BLOCK_SIZE)
87
+ result = decrypted.decode('utf-8')
88
+ return json.loads(result)
89
+ except (ValueError, KeyError, json.JSONDecodeError) as e:
90
+ # raise ValueError(f"解密失败: {e}")
91
+ return {}
92
+
93
+
94
+ class XtnDeEnProMax:
95
+ BLOCK_SIZE = AES.block_size
96
+
97
+ @staticmethod
98
+ def encrypt_dict(key: bytes, raw: dict) -> str:
99
+ """
100
+ 加密
101
+ """
102
+ session_key = get_random_bytes(16)
103
+
104
+ rsa_key = RSA.import_key(key)
105
+ rsa_cipher = PKCS1_OAEP.new(rsa_key)
106
+ encrypted_session_key = rsa_cipher.encrypt(session_key)
107
+
108
+ iv = get_random_bytes(16)
109
+ aes_cipher = AES.new(session_key, AES.MODE_CBC, iv)
110
+ raw["encrypt_time"] = int(time.time())
111
+ raw_data = json.dumps(raw).encode('utf-8')
112
+ encrypted_data = aes_cipher.encrypt(pad(raw_data, XtnDeEnProMax.BLOCK_SIZE))
113
+
114
+ combined = (
115
+ len(encrypted_session_key).to_bytes(2, 'big') +
116
+ encrypted_session_key +
117
+ iv +
118
+ encrypted_data
119
+ )
120
+
121
+ result_base64 = base64.b64encode(combined).decode()
122
+ # result_hex = combined.hex() # hex
123
+ return result_base64
124
+
125
+ @staticmethod
126
+ def decrypt_dict(key: bytes, raw: str) -> dict:
127
+ """
128
+ 解密
129
+ """
130
+ try:
131
+ combined = base64.b64decode(raw)
132
+ key_len = int.from_bytes(combined[:2], 'big')
133
+ encrypted_session_key = combined[2:2 + key_len]
134
+ iv = combined[2 + key_len:2 + key_len + 16]
135
+ encrypted_data = combined[2 + key_len + 16:]
136
+
137
+ rsa_key = RSA.import_key(key)
138
+ rsa_cipher = PKCS1_OAEP.new(rsa_key)
139
+ session_key = rsa_cipher.decrypt(encrypted_session_key)
140
+
141
+ aes_cipher = AES.new(session_key, AES.MODE_CBC, iv)
142
+ decrypted = unpad(aes_cipher.decrypt(encrypted_data), XtnDeEnProMax.BLOCK_SIZE)
143
+ result = decrypted.decode('utf-8')
144
+ return json.loads(result)
145
+ except (ValueError, KeyError, json.JSONDecodeError) as e:
146
+ # raise ValueError(f"解密失败: {e}")
147
+ return {}
148
+
149
+
150
+ if __name__ == '__main__':
151
+ key = b"d9fe4b29dc7decc8360e72e7dd15a5c8"
152
+ data = {
153
+ "task_type": "update_name",
154
+ "task_client": "tiktok",
155
+ }
156
+ sss = XtnDeEnAir.encrypt_dict(key, data)
157
+ print(sss)
158
+ print(XtnDeEnAir.decrypt_dict(key, sss))
159
+
160
+ # key_pair = RSA.generate(2048)
161
+ # private_key = key_pair.export_key()
162
+ # public_key = key_pair.publickey().export_key()
163
+ # print(private_key)
164
+ # print(public_key)
165
+ # sss = XtnDeEnProMax.encrypt_dict(public_key, data)
166
+ # print(sss)
167
+ # 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.2
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=KkTX8ITfawzKEdRw6Oq96ubnGMJrVJag-50JMheXY9c,5608
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.2.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
68
+ xtn_tools_pro-1.0.1.3.2.dist-info/METADATA,sha256=E9GDeG_G6ggOAy8PK0XyZn0qkf2mATpdaXJnR46C38I,574
69
+ xtn_tools_pro-1.0.1.3.2.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
70
+ xtn_tools_pro-1.0.1.3.2.dist-info/entry_points.txt,sha256=t8CtXWOgw7nRDW3XNlZh8MT_P4l8EzsFnyWi5b3ovrc,68
71
+ xtn_tools_pro-1.0.1.3.2.dist-info/top_level.txt,sha256=jyB3FLDEr8zE1U7wHczTgIbvUpALhR-ULF7RVEO7O2U,14
72
+ xtn_tools_pro-1.0.1.3.2.dist-info/RECORD,,