-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage encryption.py
More file actions
65 lines (53 loc) · 1.88 KB
/
Copy pathMessage encryption.py
File metadata and controls
65 lines (53 loc) · 1.88 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
import random as ra
import string as str
def options():
while True:
print("========== Welcome To Message Encryption ==========")
print("\t'1' For Encryption")
print("\t'2' For Decode")
try:
opt = int(input("Enter: "))
if opt not in ("1","2",1,2):
print("Choose Number Between '1','2'")
if opt == 1:
encrypt()
elif opt == 2:
decode()
except ValueError:
print("Enter Numbers Only")
def encrypt():
x = input("Enter Your Message :")
if len(x)<=3:
encrypt_key = input("Enter Your Key '1','4','7' :")
with open("User_Key.txt","a+") as f:
f.write(f"{encrypt_key},{x}\n")
print(f"Successfully Encrypted: {x[::-1]}")
encrypt_key = input("Enter Your Key '1','4','7' :")
with open("User_Key.txt","a") as f:
f.write(f"{encrypt_key},{x}\n")
shift = x[1:] + x[0]
prefix = "".join(ra.choice(str.ascii_letters) for _ in range(3))
suffix = "".join(ra.choice(str.ascii_letters) for _ in range(3))
message = prefix + shift + suffix
message = message.strip()
print(f"Successfully Encrypted: {message}")
return encrypt_key
def decode():
with open("User_Key.txt", "r") as f:
user_message = input("Enter Message To Decode : ")
decode_key = input("Enter Your Key : ")
found = False
for line in f:
encrypt_key, x = line.strip().split(",")
if encrypt_key == decode_key:
if len(user_message) <= 3:
print(user_message[::-1])
else:
x = user_message[3:-3]
x = x[-1] + x[:-1]
print(f"Message Decoded : {x}")
found = True
break
if not found:
print("Didn't Match :/")
options()