DE-Lib 0.0.20__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.
- DE_Lib/Cloud/__init__.py +0 -0
- DE_Lib/DataBase/Azure.py +44 -0
- DE_Lib/DataBase/Cache.py +74 -0
- DE_Lib/DataBase/Firebird.py +45 -0
- DE_Lib/DataBase/Informix.py +37 -0
- DE_Lib/DataBase/Metadata.py +62 -0
- DE_Lib/DataBase/MsSql.py +39 -0
- DE_Lib/DataBase/MySql.py +42 -0
- DE_Lib/DataBase/Oracle.py +111 -0
- DE_Lib/DataBase/Postgres.py +39 -0
- DE_Lib/DataBase/RedShift.py +42 -0
- DE_Lib/DataBase/SQCipher.py +42 -0
- DE_Lib/DataBase/SQLite.py +48 -0
- DE_Lib/DataBase/__init__.py +0 -0
- DE_Lib/Files/Avro.py +23 -0
- DE_Lib/Files/Csv.py +64 -0
- DE_Lib/Files/JSon.py +64 -0
- DE_Lib/Files/Parquet.py +31 -0
- DE_Lib/Files/Txt.py +64 -0
- DE_Lib/Files/Xlsx.py +55 -0
- DE_Lib/Files/__init__.py +0 -0
- DE_Lib/Log/DE_LogEventos.py +533 -0
- DE_Lib/Log/Level.py +77 -0
- DE_Lib/Log/Log.py +470 -0
- DE_Lib/Log/__init__.py +0 -0
- DE_Lib/Utils/Cipher/Aes.py +65 -0
- DE_Lib/Utils/Cipher/Argon.py +37 -0
- DE_Lib/Utils/Cipher/Base64.py +48 -0
- DE_Lib/Utils/Cipher/Cipher.py +300 -0
- DE_Lib/Utils/Cipher/Fernet.py +81 -0
- DE_Lib/Utils/Cipher/Gcm.py +78 -0
- DE_Lib/Utils/Cipher/Pbkdf2.py +43 -0
- DE_Lib/Utils/Cipher/Rsa.py +140 -0
- DE_Lib/Utils/Cipher/__init__.py +0 -0
- DE_Lib/Utils/Colors.py +203 -0
- DE_Lib/Utils/DateUtils.py +215 -0
- DE_Lib/Utils/Generic.py +249 -0
- DE_Lib/Utils/SQL.py +34 -0
- DE_Lib/Utils/System.py +50 -0
- DE_Lib/Utils/WebHook.py +18 -0
- DE_Lib/Utils/__init__.py +0 -0
- DE_Lib/__init__.py +0 -0
- de_lib-0.0.20.dist-info/LICENCE +21 -0
- de_lib-0.0.20.dist-info/METADATA +68 -0
- de_lib-0.0.20.dist-info/RECORD +47 -0
- de_lib-0.0.20.dist-info/WHEEL +5 -0
- de_lib-0.0.20.dist-info/top_level.txt +1 -0
@@ -0,0 +1,300 @@
|
|
1
|
+
import os
|
2
|
+
#from operator import truediv
|
3
|
+
# -----------------------------------
|
4
|
+
from Crypto.Cipher import AES, PKCS1_OAEP
|
5
|
+
from Crypto.PublicKey import RSA
|
6
|
+
# -----------------------------------
|
7
|
+
from Crypto.Util.Padding import pad, unpad
|
8
|
+
from Crypto.Random import get_random_bytes
|
9
|
+
import base64
|
10
|
+
import rsa
|
11
|
+
from argon2 import PasswordHasher
|
12
|
+
|
13
|
+
KEYS_RSA = rsa.newkeys(512)
|
14
|
+
|
15
|
+
class RSA_Cipher:
|
16
|
+
"""
|
17
|
+
Esta classe faz criptografia de pequenos textos
|
18
|
+
"""
|
19
|
+
def __init__(self):
|
20
|
+
self.__private_key = None
|
21
|
+
self.__public_key = None
|
22
|
+
self.__path_keys = ""
|
23
|
+
|
24
|
+
def setInit(self, path, word:str=""):
|
25
|
+
msg, result = None, True
|
26
|
+
try:
|
27
|
+
self.setPathKeys(path)
|
28
|
+
self.setWord(word)
|
29
|
+
self.__private_key = self.getFileKey(os.path.join(self.PATH_KEYS, "private.pem"))
|
30
|
+
self.__public_key = self.getFileKey(os.path.join(self.PATH_KEYS, "public.pem"))
|
31
|
+
except Exception as error:
|
32
|
+
msg = error
|
33
|
+
result = msg
|
34
|
+
finally:
|
35
|
+
return result
|
36
|
+
|
37
|
+
def CIPHER(self, word, action: str = "E"):
|
38
|
+
msg, result = None, True
|
39
|
+
try:
|
40
|
+
if action == "E":
|
41
|
+
result = self.encrypt(word, self.PUBLIC_KEY)
|
42
|
+
else:
|
43
|
+
result = self.decrypt(word, self.PRIVATE_KEY)
|
44
|
+
except Exception as error:
|
45
|
+
msg = error
|
46
|
+
result = msg
|
47
|
+
finally:
|
48
|
+
return result
|
49
|
+
|
50
|
+
def encrypt(self, word, key):
|
51
|
+
msg, result = None, True
|
52
|
+
try:
|
53
|
+
__cifra = PKCS1_OAEP.new(key)
|
54
|
+
result = __cifra.encrypt(word.encode())
|
55
|
+
result = result.hex()
|
56
|
+
except Exception as error:
|
57
|
+
msg = error
|
58
|
+
result = msg
|
59
|
+
finally:
|
60
|
+
return result
|
61
|
+
|
62
|
+
def decrypt(self, word, key):
|
63
|
+
msg, result = None, True
|
64
|
+
try:
|
65
|
+
__decifra = PKCS1_OAEP.new(key)
|
66
|
+
result = __decifra.decrypt(bytes.fromhex(word))
|
67
|
+
except Exception as error:
|
68
|
+
msg = error
|
69
|
+
result = msg
|
70
|
+
finally:
|
71
|
+
return result
|
72
|
+
|
73
|
+
def getChavePublica(self):
|
74
|
+
msg, result = None, True
|
75
|
+
try:
|
76
|
+
self.__public_key = self.PRIVATE_KEY.publickey()
|
77
|
+
except Exception as error:
|
78
|
+
msg = error
|
79
|
+
result = msg
|
80
|
+
finally:
|
81
|
+
return result
|
82
|
+
|
83
|
+
def getChavePrivada(self, value_bytes: int = 2048):
|
84
|
+
msg, result = None, True
|
85
|
+
try:
|
86
|
+
self.__private_key = RSA.generate(value_bytes)
|
87
|
+
except Exception as error:
|
88
|
+
msg = error
|
89
|
+
result = msg
|
90
|
+
finally:
|
91
|
+
return result
|
92
|
+
|
93
|
+
def setFileKey(self, key: hex, filename: str = "private.pem"):
|
94
|
+
msg, result = None, True
|
95
|
+
try:
|
96
|
+
with open(filename, "wb") as f:
|
97
|
+
f.write(key.export_key())
|
98
|
+
except Exception as error:
|
99
|
+
msg = error
|
100
|
+
result = msg
|
101
|
+
finally:
|
102
|
+
return result
|
103
|
+
|
104
|
+
def getFileKey(self, filename):
|
105
|
+
msg, result = None, True
|
106
|
+
try:
|
107
|
+
x = os.getcwd()
|
108
|
+
with open(filename, "r") as f:
|
109
|
+
result = RSA.import_key(f.read())
|
110
|
+
except Exception as error:
|
111
|
+
msg = error
|
112
|
+
result = msg
|
113
|
+
finally:
|
114
|
+
return result
|
115
|
+
|
116
|
+
def setPathKeys(self, path:str):
|
117
|
+
msg, result = None, True
|
118
|
+
try:
|
119
|
+
self.__path_keys = path
|
120
|
+
except Exception as error:
|
121
|
+
msg = error
|
122
|
+
result = msg
|
123
|
+
finally:
|
124
|
+
return result
|
125
|
+
|
126
|
+
def setWord(self, word):
|
127
|
+
msg, result = None, True
|
128
|
+
try:
|
129
|
+
self.__word = word
|
130
|
+
except Exception as error:
|
131
|
+
msg = error
|
132
|
+
result = msg
|
133
|
+
finally:
|
134
|
+
return result
|
135
|
+
|
136
|
+
@property
|
137
|
+
def PRIVATE_KEY(self):
|
138
|
+
return self.__private_key
|
139
|
+
|
140
|
+
@property
|
141
|
+
def PUBLIC_KEY(self):
|
142
|
+
return self.__public_key
|
143
|
+
|
144
|
+
@property
|
145
|
+
def PATH_KEYS(self):
|
146
|
+
return self.__path_keys
|
147
|
+
|
148
|
+
@property
|
149
|
+
def WORD(self):
|
150
|
+
return self.__word
|
151
|
+
|
152
|
+
class AES_Cipher:
|
153
|
+
"""
|
154
|
+
Esta classe faz criptografia de textos longos
|
155
|
+
"""
|
156
|
+
def __init__(self):
|
157
|
+
...
|
158
|
+
|
159
|
+
def encrypt(self, plaintext: str, rsa_public_key):
|
160
|
+
msg, result = None, True
|
161
|
+
try:
|
162
|
+
# Gerar uma chave AES aleatória (16 bytes)
|
163
|
+
aes_key = get_random_bytes(16)
|
164
|
+
|
165
|
+
# Criar cifra AES em modo CBC
|
166
|
+
cipher_aes = AES.new(aes_key, AES.MODE_CBC)
|
167
|
+
iv = cipher_aes.iv # Vetor de Inicialização
|
168
|
+
|
169
|
+
# Adicionar padding ao texto para que seja múltiplo de 16
|
170
|
+
ciphertext = cipher_aes.encrypt(pad(plaintext.encode(), AES.block_size))
|
171
|
+
|
172
|
+
# Criptografar a chave AES com RSA
|
173
|
+
cipher_rsa = PKCS1_OAEP.new(rsa_public_key)
|
174
|
+
encrypted_aes_key = cipher_rsa.encrypt(aes_key)
|
175
|
+
result = base64.b64encode(encrypted_aes_key + iv + ciphertext).decode()
|
176
|
+
except Exception as error:
|
177
|
+
msg = error
|
178
|
+
result = msg
|
179
|
+
finally:
|
180
|
+
return result
|
181
|
+
|
182
|
+
|
183
|
+
def decrypt(self, encrypted_data: str, rsa_private_key):
|
184
|
+
msg, result = None, True
|
185
|
+
try:
|
186
|
+
# Converter de base64 para bytes
|
187
|
+
encrypted_data = base64.b64decode(encrypted_data)
|
188
|
+
|
189
|
+
# Extrair partes (chave AES criptografada + IV + texto criptografado)
|
190
|
+
key_size = rsa_private_key.size_in_bytes()
|
191
|
+
encrypted_aes_key = encrypted_data[:key_size]
|
192
|
+
iv = encrypted_data[key_size:key_size + 16]
|
193
|
+
ciphertext = encrypted_data[key_size + 16:]
|
194
|
+
|
195
|
+
# Descriptografar a chave AES com RSA
|
196
|
+
cipher_rsa = PKCS1_OAEP.new(rsa_private_key)
|
197
|
+
aes_key = cipher_rsa.decrypt(encrypted_aes_key)
|
198
|
+
|
199
|
+
# Descriptografar o texto com AES
|
200
|
+
cipher_aes = AES.new(aes_key, AES.MODE_CBC, iv)
|
201
|
+
plaintext = unpad(cipher_aes.decrypt(ciphertext), AES.block_size)
|
202
|
+
|
203
|
+
result = plaintext.decode()
|
204
|
+
|
205
|
+
except Exception as error:
|
206
|
+
msg = error
|
207
|
+
result = msg
|
208
|
+
finally:
|
209
|
+
return result
|
210
|
+
|
211
|
+
class B64_Cipher:
|
212
|
+
def __init__(self):
|
213
|
+
...
|
214
|
+
|
215
|
+
@staticmethod
|
216
|
+
def base64_encrypt(word: str, encode_pattern: str = "utf-8"):
|
217
|
+
encoded = (base64.b64encode(word.encode(encode_pattern)))
|
218
|
+
encoded_ascii = encoded.decode(encode_pattern)
|
219
|
+
return encoded_ascii
|
220
|
+
|
221
|
+
@staticmethod
|
222
|
+
def base64_decrypt(word: str, encode_pattern: str = "utf-8"):
|
223
|
+
try:
|
224
|
+
word = word.encode(encode_pattern)
|
225
|
+
decoded = base64.b64decode(word).decode(encode_pattern)
|
226
|
+
# decoded_ascii = decoded.decode()
|
227
|
+
except Exception as error:
|
228
|
+
decoded = error
|
229
|
+
finally:
|
230
|
+
return decoded
|
231
|
+
|
232
|
+
@staticmethod
|
233
|
+
def token_get() -> str:
|
234
|
+
# key = Fernet.generate_key()
|
235
|
+
# cipher_suite = Fernet(key)
|
236
|
+
cipher_suite = True
|
237
|
+
# return key.decode("ascii")
|
238
|
+
return cipher_suite
|
239
|
+
|
240
|
+
@staticmethod
|
241
|
+
def CRYPTOGRAPHY(word: str, token: str = None, action: str = "E"):
|
242
|
+
msg, result = None, None
|
243
|
+
try:
|
244
|
+
if action == "E":
|
245
|
+
if isinstance(word, str):
|
246
|
+
word = word.encode()
|
247
|
+
result = token.encrypt(word).decode()
|
248
|
+
else:
|
249
|
+
if isinstance(word, str):
|
250
|
+
word = word.encode()
|
251
|
+
result = token.decrypt(word).decode()
|
252
|
+
except Exception as error:
|
253
|
+
msg = error.args[0]
|
254
|
+
result = msg
|
255
|
+
finally:
|
256
|
+
return result
|
257
|
+
|
258
|
+
class ARGON:
|
259
|
+
# ----------------------------------
|
260
|
+
def __init__(self):
|
261
|
+
msg, result = None, True
|
262
|
+
try:
|
263
|
+
self.__ph = PasswordHasher()
|
264
|
+
except Exception as error:
|
265
|
+
msg = error
|
266
|
+
result = msg
|
267
|
+
finally:
|
268
|
+
return result
|
269
|
+
|
270
|
+
@staticmethod
|
271
|
+
# ----------------------------------
|
272
|
+
def hash(self, value):
|
273
|
+
msg, result = None, True
|
274
|
+
try:
|
275
|
+
result = self.HASH.hash(value)
|
276
|
+
except Exception as error:
|
277
|
+
msg = error
|
278
|
+
result = msg
|
279
|
+
finally:
|
280
|
+
return result
|
281
|
+
|
282
|
+
@staticmethod
|
283
|
+
# ----------------------------------
|
284
|
+
def unHash(self, value, key):
|
285
|
+
msg, result = None, True
|
286
|
+
try:
|
287
|
+
result = self.HASH.verify(key, value)
|
288
|
+
except Exception as error:
|
289
|
+
msg = error
|
290
|
+
result = msg
|
291
|
+
finally:
|
292
|
+
return result
|
293
|
+
|
294
|
+
@property
|
295
|
+
def HASH(self):
|
296
|
+
return self.__ph
|
297
|
+
|
298
|
+
|
299
|
+
|
300
|
+
|
@@ -0,0 +1,81 @@
|
|
1
|
+
from cryptography.fernet import Fernet
|
2
|
+
import os
|
3
|
+
import json
|
4
|
+
|
5
|
+
class FERNET:
|
6
|
+
def __init__(self):
|
7
|
+
self.__token = None
|
8
|
+
self.__cipher = None
|
9
|
+
|
10
|
+
# -----------------------------------
|
11
|
+
def encrypt(self, word:str, token: str):
|
12
|
+
msg, result = None, None
|
13
|
+
try:
|
14
|
+
#x = Fernet(key)
|
15
|
+
result = self.CIPHER.encrypt(word.encode())
|
16
|
+
except Exception as error:
|
17
|
+
msg = error
|
18
|
+
result = msg
|
19
|
+
finally:
|
20
|
+
return result
|
21
|
+
|
22
|
+
# ----------------------------------
|
23
|
+
def decrypt(self, word):
|
24
|
+
msg, result = None, True
|
25
|
+
try:
|
26
|
+
result = self.CIPHER.decrypt(word).decode()
|
27
|
+
except Exception as error:
|
28
|
+
msg = error
|
29
|
+
result = msg
|
30
|
+
finally:
|
31
|
+
return result
|
32
|
+
|
33
|
+
|
34
|
+
# ----------------------------------
|
35
|
+
def setBuildToken(self):
|
36
|
+
# normalmente sera gerado apenas uma unica vez
|
37
|
+
# armazenar a chave. Caso seja gerado uma outra
|
38
|
+
# todas as criptografias geraradas anteriormente
|
39
|
+
# serao perdidas.
|
40
|
+
msg, result = None, True
|
41
|
+
try:
|
42
|
+
self.__token = Fernet.generate_key()
|
43
|
+
result = self.TOKEN
|
44
|
+
except Exception as error:
|
45
|
+
msg = error
|
46
|
+
result = msg
|
47
|
+
finally:
|
48
|
+
return result
|
49
|
+
|
50
|
+
# ----------------------------------
|
51
|
+
def setToken(self, token):
|
52
|
+
msg, result = None, True
|
53
|
+
try:
|
54
|
+
self.__token = token
|
55
|
+
except Exception as error:
|
56
|
+
msg = error
|
57
|
+
result = msg
|
58
|
+
finally:
|
59
|
+
return result
|
60
|
+
|
61
|
+
|
62
|
+
# ----------------------------------
|
63
|
+
def __setCipher(self, key):
|
64
|
+
msg, result = None, True
|
65
|
+
try:
|
66
|
+
self.__cipher = Fernet(key)
|
67
|
+
except Exception as error:
|
68
|
+
msg = error
|
69
|
+
result = msg
|
70
|
+
finally:
|
71
|
+
return result
|
72
|
+
|
73
|
+
|
74
|
+
@property
|
75
|
+
def TOKEN(self) -> str:
|
76
|
+
return self.__token
|
77
|
+
|
78
|
+
@property
|
79
|
+
def CIPHER(self):
|
80
|
+
return Fernet(self.TOKEN)
|
81
|
+
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# criptografia funcionou normalmente
|
2
|
+
# tem que utulizar a biblioteca "pip install cryptography==41.0.7"
|
3
|
+
|
4
|
+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
5
|
+
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
|
6
|
+
from cryptography.hazmat.primitives import hashes
|
7
|
+
import os
|
8
|
+
import base64
|
9
|
+
|
10
|
+
from docutils.io import error_string
|
11
|
+
|
12
|
+
|
13
|
+
class GCM:
|
14
|
+
def __init__(self):
|
15
|
+
...
|
16
|
+
|
17
|
+
def __buildKey(self, token: str, salt: bytes) -> bytes:
|
18
|
+
msg, result = None, None
|
19
|
+
try:
|
20
|
+
result = PBKDF2HMAC(algorithm=hashes.SHA256(),
|
21
|
+
length=32,
|
22
|
+
salt=salt,
|
23
|
+
iterations=100000
|
24
|
+
)
|
25
|
+
result = result.derive(token.encode())
|
26
|
+
except Exception as error:
|
27
|
+
msg = error
|
28
|
+
result = msg
|
29
|
+
finally:
|
30
|
+
return result
|
31
|
+
|
32
|
+
# ----------------------------------
|
33
|
+
def Encrypt(self, token:str, word:str):
|
34
|
+
msg, result = None, True
|
35
|
+
try:
|
36
|
+
salt = os.urandom(16)
|
37
|
+
key = self.__buildKey(token=token, salt=salt)
|
38
|
+
|
39
|
+
iv = os.urandom(12)
|
40
|
+
cipher = Cipher(algorithms.AES(key), modes.GCM(iv))
|
41
|
+
encryptor = cipher.encryptor()
|
42
|
+
|
43
|
+
text_bytes = word.encode()
|
44
|
+
text_cryptography = encryptor.update(text_bytes) + encryptor.finalize()
|
45
|
+
result = base64.b64encode(salt + iv + encryptor.tag + text_cryptography).decode()
|
46
|
+
except Exception as error:
|
47
|
+
msg = error
|
48
|
+
result = msg
|
49
|
+
finally:
|
50
|
+
return result
|
51
|
+
|
52
|
+
# ----------------------------------
|
53
|
+
def Decrypt(self, word:str, token: str):
|
54
|
+
msg, result = None, True
|
55
|
+
try:
|
56
|
+
data = base64.b64decode(word)
|
57
|
+
|
58
|
+
|
59
|
+
salt = data[:16]
|
60
|
+
iv = data[16:28]
|
61
|
+
tag = data[28:44]
|
62
|
+
__text = data[44:]
|
63
|
+
|
64
|
+
key = self.__buildKey(token, salt)
|
65
|
+
cipher = Cipher(algorithms.AES(key), modes.GCM(iv, tag))
|
66
|
+
decryptor = cipher.decryptor()
|
67
|
+
result = decryptor.update(__text) + decryptor.finalize()
|
68
|
+
|
69
|
+
except Exception as error:
|
70
|
+
msg = error
|
71
|
+
result = msg
|
72
|
+
finally:
|
73
|
+
return result.decode("utf-8")
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
@@ -0,0 +1,43 @@
|
|
1
|
+
import hashlib
|
2
|
+
import os
|
3
|
+
|
4
|
+
class PBKDF2:
|
5
|
+
# ----------------------------------
|
6
|
+
def __init__(self):
|
7
|
+
msg, result = None, True
|
8
|
+
try:
|
9
|
+
self.__saltAleatorio = 16
|
10
|
+
except Exception as error:
|
11
|
+
msg = error
|
12
|
+
result = msg
|
13
|
+
|
14
|
+
# ----------------------------------
|
15
|
+
def hash(self, value, salt=None):
|
16
|
+
msg, result = None, True
|
17
|
+
try:
|
18
|
+
if salt is None:
|
19
|
+
salt = os.urandom(self.SALT)
|
20
|
+
hash_value = hashlib.pbkdf2_hmac("sha256", value.encode(), salt, 100000)
|
21
|
+
result = salt + hash_value
|
22
|
+
except Exception as error:
|
23
|
+
msg = error
|
24
|
+
result = msg
|
25
|
+
finally:
|
26
|
+
return result
|
27
|
+
|
28
|
+
# ----------------------------------
|
29
|
+
def validHash(self, value, key):
|
30
|
+
msg, result = None, True
|
31
|
+
try:
|
32
|
+
salt = key[:self.SALT]
|
33
|
+
new_hash = self.hash(value, salt)
|
34
|
+
result = (new_hash == key)
|
35
|
+
except Exception as error:
|
36
|
+
msg = error
|
37
|
+
result = msg
|
38
|
+
finally:
|
39
|
+
return result
|
40
|
+
|
41
|
+
@property
|
42
|
+
def SALT(self):
|
43
|
+
return self.__saltAleatorio
|
@@ -0,0 +1,140 @@
|
|
1
|
+
from Crypto.PublicKey import RSA
|
2
|
+
from Crypto.Cipher import PKCS1_OAEP
|
3
|
+
import os
|
4
|
+
|
5
|
+
class RSA_Cipher:
|
6
|
+
"""
|
7
|
+
Esta classe faz criptografia de pequenos textos
|
8
|
+
"""
|
9
|
+
def __init__(self):
|
10
|
+
self.__private_key = None
|
11
|
+
self.__public_key = None
|
12
|
+
self.__path_keys = ""
|
13
|
+
|
14
|
+
def setInit(self, path, word:str=""):
|
15
|
+
msg, result = None, True
|
16
|
+
try:
|
17
|
+
self.setPathKeys(path)
|
18
|
+
self.setWord(word)
|
19
|
+
self.__private_key = self.getFileKey(os.path.join(self.PATH_KEYS, "private.pem"))
|
20
|
+
self.__public_key = self.getFileKey(os.path.join(self.PATH_KEYS, "public.pem"))
|
21
|
+
except Exception as error:
|
22
|
+
msg = error
|
23
|
+
result = msg
|
24
|
+
finally:
|
25
|
+
return result
|
26
|
+
|
27
|
+
def CIPHER(self, word, action: str = "E"):
|
28
|
+
msg, result = None, True
|
29
|
+
try:
|
30
|
+
if action == "E":
|
31
|
+
result = self.encrypt(word, self.PUBLIC_KEY)
|
32
|
+
else:
|
33
|
+
result = self.decrypt(word, self.PRIVATE_KEY)
|
34
|
+
except Exception as error:
|
35
|
+
msg = error
|
36
|
+
result = msg
|
37
|
+
finally:
|
38
|
+
return result
|
39
|
+
|
40
|
+
def encrypt(self, word, key):
|
41
|
+
msg, result = None, True
|
42
|
+
try:
|
43
|
+
__cifra = PKCS1_OAEP.new(key)
|
44
|
+
result = __cifra.encrypt(word.encode())
|
45
|
+
result = result.hex()
|
46
|
+
except Exception as error:
|
47
|
+
msg = error
|
48
|
+
result = msg
|
49
|
+
finally:
|
50
|
+
return result
|
51
|
+
|
52
|
+
def decrypt(self, word, key):
|
53
|
+
msg, result = None, True
|
54
|
+
try:
|
55
|
+
__decifra = PKCS1_OAEP.new(key)
|
56
|
+
result = __decifra.decrypt(bytes.fromhex(word))
|
57
|
+
except Exception as error:
|
58
|
+
msg = error
|
59
|
+
result = msg
|
60
|
+
finally:
|
61
|
+
return result
|
62
|
+
|
63
|
+
def getChavePublica(self):
|
64
|
+
msg, result = None, True
|
65
|
+
try:
|
66
|
+
self.__public_key = self.PRIVATE_KEY.publickey()
|
67
|
+
except Exception as error:
|
68
|
+
msg = error
|
69
|
+
result = msg
|
70
|
+
finally:
|
71
|
+
return result
|
72
|
+
|
73
|
+
def getChavePrivada(self, value_bytes=2048):
|
74
|
+
msg, result = None, True
|
75
|
+
try:
|
76
|
+
self.__private_key = RSA.generate(2048)
|
77
|
+
except Exception as error:
|
78
|
+
msg = error
|
79
|
+
result = msg
|
80
|
+
finally:
|
81
|
+
return result
|
82
|
+
|
83
|
+
def setFileKey(self, key: hex, filename: str = "private.pem"):
|
84
|
+
msg, result = None, True
|
85
|
+
try:
|
86
|
+
with open(filename, "wb") as f:
|
87
|
+
f.write(key.export_key())
|
88
|
+
except Exception as error:
|
89
|
+
msg = error
|
90
|
+
result = msg
|
91
|
+
finally:
|
92
|
+
return result
|
93
|
+
|
94
|
+
def getFileKey(self, filename):
|
95
|
+
msg, result = None, True
|
96
|
+
try:
|
97
|
+
x = os.getcwd()
|
98
|
+
with open(filename, "r") as f:
|
99
|
+
result = RSA.import_key(f.read())
|
100
|
+
except Exception as error:
|
101
|
+
msg = error
|
102
|
+
result = msg
|
103
|
+
finally:
|
104
|
+
return result
|
105
|
+
|
106
|
+
def setPathKeys(self, path:str):
|
107
|
+
msg, result = None, True
|
108
|
+
try:
|
109
|
+
self.__path_keys = path
|
110
|
+
except Exception as error:
|
111
|
+
msg = error
|
112
|
+
result = msg
|
113
|
+
finally:
|
114
|
+
return result
|
115
|
+
|
116
|
+
def setWord(self, word):
|
117
|
+
msg, result = None, True
|
118
|
+
try:
|
119
|
+
self.__word = word
|
120
|
+
except Exception as error:
|
121
|
+
msg = error
|
122
|
+
result = msg
|
123
|
+
finally:
|
124
|
+
return result
|
125
|
+
|
126
|
+
@property
|
127
|
+
def PRIVATE_KEY(self):
|
128
|
+
return self.__private_key
|
129
|
+
|
130
|
+
@property
|
131
|
+
def PUBLIC_KEY(self):
|
132
|
+
return self.__public_key
|
133
|
+
|
134
|
+
@property
|
135
|
+
def PATH_KEYS(self):
|
136
|
+
return self.__path_keys
|
137
|
+
|
138
|
+
@property
|
139
|
+
def WORD(self):
|
140
|
+
return self.__word
|
File without changes
|