-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
93 lines (82 loc) · 2.88 KB
/
Copy pathmain.py
File metadata and controls
93 lines (82 loc) · 2.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
char_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~', ' ']
def change_length(string, length):
output = []
char_index = 0
for _ in range(length):
if char_index >= len(string):
char_index = 0
output.append(string[char_index])
char_index += 1
return "".join(output)
def get_indexes(text):
output = []
for char in range(len(text)):
output.append(char_list.index(text[char]))
return output
def encrypt(text, key):
output = []
char_list_len = len(char_list)
text_len = len(text)
key_len = len(key)
text_indexes = get_indexes(text)
key_indexes = get_indexes(change_length(key, text_len))
for x in range(text_len):
char_index = text_indexes[x] + key_indexes[x] + key_len + 1
while char_index >= char_list_len:
char_index -= char_list_len
output.append(char_list[char_index])
return "".join(output)
def decrypt(text, key):
output = []
char_list_len = len(char_list)
text_len = len(text)
key_len = len(key)
text_indexes = get_indexes(text)
key_indexes = get_indexes(change_length(key, text_len))
for x in range(text_len):
char_index = text_indexes[x] - key_indexes[x] - key_len - 1
while char_index < 0:
char_index += char_list_len
output.append(char_list[char_index])
return "".join(output)
def main():
is_running = True
print("-----------------")
print("Author: M.Kamran ")
while is_running:
print("-----------------")
print("1. Encrypt")
print("2. Decrypt")
print("3. Exit")
print("-----------------")
option = input("Enter your option (1-3): ")
if option == "1":
text = ""
while text == "":
text = input("Enter text for encryption: ")
key = ""
while key == "":
key = input("Enter the password: ")
encrypted_text = encrypt(text, key)
print("-----------------")
print(f"Encrypted text (exclude outer ''): '{encrypted_text}'")
elif option == "2":
text = ""
while text == "":
text = input("Enter encrypted text: ")
key = ""
while key == "":
key = input("Enter the password: ")
decrypted_text = decrypt(text, key)
print("-----------------")
print(f"Decrypted text (exclude outer ''): '{decrypted_text}'")
elif option == "3":
is_running = False
else:
print("-----------------")
print("Invalid choice")
print("-----------------")
print("Thanks for using!")
print("-----------------")
if __name__ == "__main__":
main()