decrypt.py (4011B)
1 from cryptography.hazmat.backends import default_backend 2 from cryptography.hazmat.primitives.asymmetric import rsa 3 from cryptography.hazmat.primitives import serialization 4 from cryptography.hazmat.primitives import hashes 5 from cryptography.hazmat.primitives.asymmetric import padding 6 from cryptography.fernet import Fernet 7 from prompts import success 8 9 10 def rsa_dec(file, priv, passkey): 11 """Decrypt the passed file with a private key 12 13 Keyword arguments: 14 file -- the filepath to file to decrypt 15 priv -- the filepath to the private key 16 passkey -- the password to the private key (OPTIONAL) 17 """ 18 with open(priv, "rb") as priv_src, \ 19 open(file, "rb") as read_file: 20 private_key = serialization.load_pem_private_key( 21 priv_src.read(), 22 password=bytes(passkey, "utf-8") if passkey != "" else None, 23 backend=default_backend() 24 ) 25 content = read_file.read()[:-1] 26 original_message = private_key.decrypt( 27 content, 28 padding.OAEP( 29 mgf=padding.MGF1(algorithm=hashes.SHA256()), 30 algorithm=hashes.SHA256(), 31 label=None 32 ) 33 ) 34 with open(file, "wb") as write_file: 35 write_file.write(original_message) 36 37 def mixed_dec(file, priv, sym, passkey): 38 """Decrypt the passed file with an encrypted symmetric key 39 40 Keyword arguments: 41 file -- the filepath to file to decrypt 42 priv -- the filepath to the private key 43 sym -- the filepath to the symmetric key 44 passkey -- the password to the private key (OPTIONAL) 45 """ 46 with open(priv, "rb") as priv_src, \ 47 open(sym, "rb") as sym_src, \ 48 open(file, "rb") as read_file: 49 private_key = serialization.load_pem_private_key( 50 priv_src.read(), 51 password=bytes(passkey, "utf-8") if passkey != "" else None, 52 backend=default_backend() 53 ) 54 sym_data = sym_src.read() 55 try: 56 sym_dec = private_key.decrypt( 57 sym_data, 58 padding.OAEP( 59 mgf=padding.MGF1(algorithm=hashes.SHA256()), 60 algorithm=hashes.SHA256(), 61 label=None 62 ) 63 ) 64 symmetric_key = Fernet(sym_dec) 65 except: 66 symmetric_key = Fernet(sym_data) 67 content = read_file.read()[:-1] 68 original_message = symmetric_key.decrypt(content) 69 with open(file, "wb") as write_file: 70 write_file.write(original_message) 71 72 73 def dec_manager(files, key_dir, passkey): 74 """Decrypt all of the passed files based on their respective tags 75 76 Keyword arguments: 77 files -- a tuple of filepaths to decrypt 78 key_dir -- the directory where the keys are located 79 passkey -- the password to the private key (OPTIONAL) 80 """ 81 priv = key_dir + "/private_key.pem" 82 sym = key_dir + "/symmetric_key.key" 83 rsa = True 84 for fl in files: 85 with open(fl, "rb") as read_file: 86 tag = read_file.read()[-1] 87 if tag == 48: 88 rsa_dec(fl, priv, passkey) 89 else: 90 mixed_dec(fl, priv, sym, passkey) 91 rsa = False 92 if not rsa: 93 with open(priv, "rb") as priv_src, \ 94 open(sym, "rb") as sym_src: 95 private_key = serialization.load_pem_private_key( 96 priv_src.read(), 97 password=bytes(passkey, "utf-8") if passkey != "" else None, 98 backend=default_backend() 99 ) 100 sym_data = sym_src.read() 101 try: 102 sym_dec = private_key.decrypt( 103 sym_data, 104 padding.OAEP( 105 mgf=padding.MGF1(algorithm=hashes.SHA256()), 106 algorithm=hashes.SHA256(), 107 label=None 108 ) 109 ) 110 except: 111 return None 112 with open(sym, "wb") as sym_src: 113 sym_src.write(sym_dec) 114 success("dec")