| 7 | |
| 8 | # Start of Encryption Function |
| 9 | def 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 |