Omnibus commited on
Commit
77566b7
·
1 Parent(s): 1014837

Update crypt.py

Browse files
Files changed (1) hide show
  1. crypt.py +52 -0
crypt.py CHANGED
@@ -296,8 +296,60 @@ def generate_keys():
296
  pub_key = conv_im("address_im.png",data=public_key)
297
 
298
  return public_key,private_key,address_im,address,priv_key,pub_key
 
299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
301
  def encrypt_text(data,pub_im,priv_im,address):
302
  pub_key = decode(pub_im)
303
  data = data.encode("utf-8")
 
296
  pub_key = conv_im("address_im.png",data=public_key)
297
 
298
  return public_key,private_key,address_im,address,priv_key,pub_key
299
+ ########********************************###########################
300
 
301
+ def encrypt_trans(data,pub_key):
302
+ #pub_key = decode(pub_im)
303
+ data = data.encode("utf-8")
304
+ recipient_key = RSA.import_key(pub_key)
305
+ session_key = get_random_bytes(16)
306
+
307
+ # Encrypt the session key with the public RSA key
308
+ cipher_rsa = PKCS1_OAEP.new(recipient_key)
309
+ enc_session_key = cipher_rsa.encrypt(session_key)
310
+
311
+ # Encrypt the data with the AES session key
312
+ cipher_aes = AES.new(session_key, AES.MODE_EAX)
313
+ ciphertext, tag = cipher_aes.encrypt_and_digest(data)
314
 
315
+ file_out = open("encrypted_data.bin", "wb")
316
+
317
+ [ file_out.write(x) for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext) ]
318
+ file_out.close()
319
+ doc_name = "encrypted_data.bin"
320
+ with open(doc_name, "rb") as file:
321
+ file_data =(file.read())
322
+ file.close()
323
+ return file_data
324
+
325
+
326
+ def decrypt_trans(data,priv_im):
327
+ secret_code="SECRET PASSWORD"
328
+ priv_key = decode(priv_im)
329
+ print(f'priv_key:: {priv_key}')
330
+ key = RSA.import_key(priv_key,passphrase=secret_code)
331
+
332
+ public_key = key.publickey().export_key('PEM')
333
+ file_out_pub = open("receiver.pem", "wb")
334
+ file_out_pub.write(public_key)
335
+ file_out_pub.close()
336
+
337
+
338
+ hash_1 = calculate_hash(public_key, hash_function="sha256")
339
+ hash_2 = calculate_hash(hash_1, hash_function="ripemd160")
340
+ address = base58.b58encode(hash_2)
341
+ address= str(address)
342
+ print (address)
343
+ address = address.strip("b")
344
+ print (address)
345
+ address = address.strip("'") if address.startswith("'") else address.strip('"')
346
+ print (address)
347
+
348
+ return (address)
349
+
350
+ ########********************************###########################
351
+
352
+
353
  def encrypt_text(data,pub_im,priv_im,address):
354
  pub_key = decode(pub_im)
355
  data = data.encode("utf-8")