-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsecret_messages.py
executable file
·169 lines (136 loc) · 5.22 KB
/
secret_messages.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import os
import sys
import time
import caesar
import atbash
import key_word
import polybius
def clear_screen():
os.system('cls' if os.name == 'nt' else 'clear')
def hello():
clear_screen()
print("Welcome agent. Press any key to begin.")
input()
# clear screen again
clear_screen()
def goodbye():
clear_screen()
# thank the user
print("Thank you, you were never here, but see you next time.\n")
count = 3
while count:
sys.stdout.write("\r")
sys.stdout.write(
"This program will self destruct in {:2d} seconds...".format(
count))
sys.stdout.flush()
time.sleep(1)
count -= 1
# erase all traces, this is all so secret
clear_screen()
def program_loop():
# available ciphers
available_ciphers = {
'1': 'caesar',
'2': 'polybius square',
'3': 'atbash',
'4': 'keyword'
}
# our program is currently running
program_running = True
input_char = "→ "
while program_running:
# print the available ciphers
print("The available ciphers are:")
for option, cipher in available_ciphers.items():
print("{}: {}".format(option, cipher))
print(
"Choose a cipher by entering its corresponding number below. "
"Enter QUIT to quit.\n")
# Collect user input
user_input = str.lower(input(input_char))
# -todo test user input here
# user has entered quit
if user_input == "quit":
break
# if the users choice is legit...
if user_input in available_ciphers:
# we have valid input,
# we are executing a cipher
executing_cipher = True
clear_screen()
selected_cipher = available_ciphers[user_input]
while executing_cipher:
options = {
"1": "encrypting",
"2": "decrypting"
}
# ask if the user is encrypting or decrypting
print(
"You've selected {}, are you encrypting or "
"decrypting?".format(str.capitalize(selected_cipher)))
print(
"Enter 1 if encrypting, or 2 if decrypting. "
"Enter BACK to return to the main menu.\n")
encrypt_or_decrypt = str.lower(input(input_char))
if encrypt_or_decrypt == "back":
break
# if the user selection is a valid option
elif encrypt_or_decrypt in options:
# okay we're good, grab the desired action as a var so we
# can reference it below
action = options[encrypt_or_decrypt]
# get the message form the user
print("Enter the text you will be {}:".format(action))
users_message = str.upper(input(input_char))
# okay, now we know which cipher, the action to take, and
# the message,
# create an instance of the cipher
# Caesar Cipher
if selected_cipher == "caesar":
# create an instance of caesar
instance = caesar.Caesar()
# atbash
elif selected_cipher == "atbash":
# create an instance of atbash
instance = atbash.Atbash()
# keyword
elif selected_cipher == "keyword":
instance = key_word.Keyword()
# keyword cipher requires a keyword
print(
"The Keyword Cipher requires a keyword."
"Enter it below:")
cipher_keyword = str.upper(input(input_char))
# lets pack the user input & keyword up,
# and pass the to the function together
users_message = [users_message, cipher_keyword]
elif selected_cipher == "polybius square":
# create an instance
instance = polybius.Polybius()
# run the selected action on the created instance
if action == "encrypting":
print(instance.encrypt(users_message))
else:
print(instance.decrypt(users_message))
executing_cipher = False
else:
print(
"\nError. {} is not a valid option, please try "
"again...\n".format(encrypt_or_decrypt))
program_running = False
else:
print(
"\nError. {} is not a valid option. Please "
"try again.\n".format(selected_cipher))
else:
print("Would you like to encrypt another message? [Y,n]\n")
if input(input_char).lower() != "n":
program_loop()
if __name__ == '__main__':
# greet the user
hello()
# run the program
program_loop()
# we were never here
goodbye()