MCPcopy Create free account
hub / github.com/Mrinank-Bhowmick/python-beginner-projects / decrypt

Function decrypt

projects/AES256/AES256.py:28–44  ·  view source on GitHub ↗
(enc_dict, password)

Source from the content-addressed store, hash-verified

26
27# Start of Decryption Function
28def decrypt(enc_dict, password):
29 if not password:
30 raise ValueError("Password cannot be empty.")
31
32 try:
33 salt = b64decode(enc_dict["salt"])
34 cipher_text = b64decode(enc_dict["cipher_text"])
35 nonce = b64decode(enc_dict["nonce"])
36 tag = b64decode(enc_dict["tag"])
37 private_key = hashlib.scrypt(
38 password.encode(), salt=salt, n=2**14, r=8, p=1, dklen=32
39 )
40 cipher = AES.new(private_key, AES.MODE_GCM, nonce=nonce)
41 decrypted = cipher.decrypt_and_verify(cipher_text, tag)
42 return decrypted.decode("utf-8")
43 except (ValueError, KeyError) as e:
44 raise ValueError("Invalid encrypted message format.") from e
45
46
47def main():

Callers 1

mainFunction · 0.70

Calls 1

newMethod · 0.80

Tested by

no test coverage detected