Spaces:
Runtime error
Runtime error
Update crypt.py
Browse files
crypt.py
CHANGED
@@ -332,28 +332,36 @@ def encrypt_trans(data,pub_key):
|
|
332 |
|
333 |
|
334 |
def decrypt_trans(data,key):
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
#
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
print (
|
353 |
-
|
354 |
-
print (
|
|
|
355 |
|
356 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
357 |
|
358 |
########********************************###########################
|
359 |
|
|
|
332 |
|
333 |
|
334 |
def decrypt_trans(data,key):
|
335 |
+
enc_in = data
|
336 |
+
priv_key = key
|
337 |
+
#enc_in = decode(im)
|
338 |
+
#secret_code="SECRET PASSWORD"
|
339 |
+
##private_key = RSA.import_key(open("private.pem").read())
|
340 |
+
#priv_key = decode(in2)
|
341 |
+
#print(f'priv_key:: {priv_key}')
|
342 |
+
#private_key = RSA.import_key(priv_key,passphrase=secret_code)
|
343 |
+
print(f'private_key:: {private_key}')
|
344 |
+
enc_session_key = enc_in[:private_key.size_in_bytes()]
|
345 |
+
end1 = private_key.size_in_bytes()+16
|
346 |
+
nonce = enc_in[private_key.size_in_bytes():end1]
|
347 |
+
start1=end1+1
|
348 |
+
end2 = private_key.size_in_bytes()+32
|
349 |
+
start2=end2+1
|
350 |
+
tag = enc_in[end1:end2]
|
351 |
+
ciphertext = enc_in[end2:]
|
352 |
+
print (f'enc_session_key::{enc_session_key}')
|
353 |
+
print (f'nonce::{nonce}')
|
354 |
+
print (f'tag::{tag}')
|
355 |
+
print (f'ciphertext::{ciphertext}')
|
356 |
|
357 |
+
# Decrypt the session key with the private RSA key
|
358 |
+
cipher_rsa = PKCS1_OAEP.new(private_key)
|
359 |
+
session_key = cipher_rsa.decrypt(enc_session_key)
|
360 |
+
|
361 |
+
# Decrypt the data with the AES session key
|
362 |
+
cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
|
363 |
+
data = cipher_aes.decrypt_and_verify(ciphertext, tag)
|
364 |
+
return(data.decode("utf-8"))
|
365 |
|
366 |
########********************************###########################
|
367 |
|