Omnibus commited on
Commit
f7f5865
·
1 Parent(s): f0f928c

Update crypt.py

Browse files
Files changed (1) hide show
  1. crypt.py +29 -21
crypt.py CHANGED
@@ -332,28 +332,36 @@ def encrypt_trans(data,pub_key):
332
 
333
 
334
  def decrypt_trans(data,key):
335
- secret_code="SECRET PASSWORD"
336
- #priv_key = decode(priv_im)
337
- print(f'priv_key:: {priv_key}')
338
- #key = RSA.import_key(priv_key,passphrase=secret_code)
339
-
340
- public_key = key.publickey().export_key('PEM')
341
- file_out_pub = open("receiver.pem", "wb")
342
- file_out_pub.write(public_key)
343
- file_out_pub.close()
344
-
345
-
346
- hash_1 = calculate_hash(public_key, hash_function="sha256")
347
- hash_2 = calculate_hash(hash_1, hash_function="ripemd160")
348
- address = base58.b58encode(hash_2)
349
- address= str(address)
350
- print (address)
351
- address = address.strip("b")
352
- print (address)
353
- address = address.strip("'") if address.startswith("'") else address.strip('"')
354
- print (address)
 
355
 
356
- return (address)
 
 
 
 
 
 
 
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