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

Function encrypt

projects/AES256/AES256.py:9–24  ·  view source on GitHub ↗
(plain_text, password)

Source from the content-addressed store, hash-verified

7
8# Start of Encryption Function
9def encrypt(plain_text, password):
10 if not password:
11 raise ValueError("Password cannot be empty.")
12
13 salt = get_random_bytes(AES.block_size)
14 private_key = hashlib.scrypt(
15 password.encode(), salt=salt, n=2**14, r=8, p=1, dklen=32
16 )
17 cipher_config = AES.new(private_key, AES.MODE_GCM)
18 cipher_text, tag = cipher_config.encrypt_and_digest(bytes(plain_text, "utf-8"))
19 return {
20 "cipher_text": b64encode(cipher_text).decode("utf-8"),
21 "salt": b64encode(salt).decode("utf-8"),
22 "nonce": b64encode(cipher_config.nonce).decode("utf-8"),
23 "tag": b64encode(tag).decode("utf-8"),
24 }
25
26
27# Start of Decryption Function

Callers 1

mainFunction · 0.70

Calls 1

newMethod · 0.80

Tested by

no test coverage detected