forked from Mrinank-Bhowmick/python-beginner-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAES256.py
More file actions
88 lines (74 loc) · 2.81 KB
/
AES256.py
File metadata and controls
88 lines (74 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Imports
import hashlib
from base64 import b64encode, b64decode
import os
from Cryptodome.Cipher import AES
from Cryptodome.Random import get_random_bytes
import platform
# Clear the console screen
if platform.system() == "Windows":
os.system("cls")
else:
os.system("clear")
# Start of Encryption Function
def encrypt(plain_text, password):
if not password:
raise ValueError("Password cannot be empty.")
salt = get_random_bytes(AES.block_size)
private_key = hashlib.scrypt(
password.encode(), salt=salt, n=2**14, r=8, p=1, dklen=32
)
cipher_config = AES.new(private_key, AES.MODE_GCM)
cipher_text, tag = cipher_config.encrypt_and_digest(bytes(plain_text, "utf-8"))
return {
"cipher_text": b64encode(cipher_text).decode("utf-8"),
"salt": b64encode(salt).decode("utf-8"),
"nonce": b64encode(cipher_config.nonce).decode("utf-8"),
"tag": b64encode(tag).decode("utf-8"),
}
# Start of Decryption Function
def decrypt(enc_dict, password):
if not password:
raise ValueError("Password cannot be empty.")
try:
salt = b64decode(enc_dict["salt"])
cipher_text = b64decode(enc_dict["cipher_text"])
nonce = b64decode(enc_dict["nonce"])
tag = b64decode(enc_dict["tag"])
private_key = hashlib.scrypt(
password.encode(), salt=salt, n=2**14, r=8, p=1, dklen=32
)
cipher = AES.new(private_key, AES.MODE_GCM, nonce=nonce)
decrypted = cipher.decrypt_and_verify(cipher_text, tag)
return decrypted.decode("utf-8")
except (ValueError, KeyError) as e:
raise ValueError("Invalid encrypted message format.") from e
def main():
print("\t\tAES 256 Encryption and Decryption Algorithm")
print("\t\t-------------------------------------------\n\n")
x = input("Enter 1 to encrypt and 2 to decrypt: ")
if x == "1":
password = input("Enter the Password: ")
secret_mssg = input("\nEnter the Secret Message: ")
# First, let us encrypt the secret message
encrypted = encrypt(secret_mssg, password)
print("\n\nEncrypted:")
print("---------------\n")
for k, v in encrypted.items():
print(f"{k}: {v}")
elif x == "2":
try:
encrypted = {}
encrypted["cipher_text"] = input("Enter the cipher text: ")
encrypted["salt"] = input("Enter the salt: ")
encrypted["nonce"] = input("Enter the nonce: ")
encrypted["tag"] = input("Enter the tag: ")
password = input("Enter the password: ")
decrypted = decrypt(encrypted, password)
print("\n\nDecrypted:")
print("-----------------\n")
print(decrypted)
except ValueError as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()