Skip to content

Commit 9845f8f

Browse files
committed
Project _ Message_Encryption
1 parent 6c04620 commit 9845f8f

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

Projects/Message_Encryption.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import random as ra
2+
import string as str
3+
4+
def options():
5+
while True:
6+
print("========== Welcome To Message Encryption ==========")
7+
print("\t'1' For Encryption")
8+
print("\t'2' For Decode")
9+
try:
10+
opt = int(input("Enter: "))
11+
if opt not in ("1","2",1,2):
12+
print("Choose Number Between '1','2'")
13+
if opt == 1:
14+
encrypt()
15+
elif opt == 2:
16+
decode()
17+
except ValueError:
18+
print("Enter Numbers Only")
19+
20+
def encrypt():
21+
x = input("Enter Your Message :")
22+
23+
if len(x)<=3:
24+
encrypt_key = input("Enter Your Key '1','4','7' :")
25+
26+
with open("User_Key.txt","a+") as f:
27+
f.write(f"{encrypt_key},{x}\n")
28+
print(f"Successfully Encrypted: {x[::-1]}")
29+
30+
encrypt_key = input("Enter Your Key '1','4','7' :")
31+
32+
with open("User_Key.txt","a") as f:
33+
f.write(f"{encrypt_key},{x}\n")
34+
shift = x[1:] + x[0]
35+
prefix = "".join(ra.choice(str.ascii_letters) for _ in range(3))
36+
suffix = "".join(ra.choice(str.ascii_letters) for _ in range(3))
37+
message = prefix + shift + suffix
38+
message = message.strip()
39+
print(f"Successfully Encrypted: {message}")
40+
return encrypt_key
41+
42+
def decode():
43+
with open("User_Key.txt", "r") as f:
44+
user_message = input("Enter Message To Decode : ")
45+
decode_key = input("Enter Your Key : ")
46+
found = False
47+
48+
for line in f:
49+
encrypt_key, x = line.strip().split(",")
50+
51+
if encrypt_key == decode_key:
52+
if len(user_message) <= 3:
53+
print(user_message[::-1])
54+
else:
55+
x = user_message[3:-3]
56+
x = x[-1] + x[:-1]
57+
print(f"Message Decoded : {x}")
58+
59+
found = True
60+
break
61+
62+
if not found:
63+
print("Didn't Match :/")
64+
65+
options()

0 commit comments

Comments
 (0)