-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaesarCipher.py
73 lines (66 loc) · 2.38 KB
/
CaesarCipher.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
def init_fun(): # Initializes the program
startup = input("Encode or decode with key or decode without key? (ed/dwok)\n> ").lower()
if startup == "dwok": # "dwok" means decode without key.
caesarCipher_hiddenkey_decode()
elif startup == "ed": # "ed" means encode/decode.
caesarCipher()
else:
print("It looks like you didn't input ed or dwok. Try again please.")
startup_()
def isInt_fun(x):
try:
int(x)
return True
except ValueError:
return False
def caesarCipher(): # Encodes or decodes with a key
text = input("\nInput your message.\n> ")
key = int(input("\nInput your integer key.\n> "))
mode = input("\nEncode or decode?\n> ")
alphabet = "abcdefghijlmnopqrstuvwxyz"
newText = "" # text = list(text) is unnecessary here
if mode == "decode": # Reverses the direction of the inputted shift
key = -ke
print("\nDecoding...\n")
else:
print("\nEncoding...\n")
for char in range(len(text)):
if text[char].isupper():
alphabet = alphabet.upper()
elif text[char].islower():
alphabet = alphabet.lower()
if text[char].isalpha() == False: # Exception for non-letters
if isInt_fun(text[char]):
newLetter = str((int(text[char]) + key)%10)
else:
newLetter = text[char]
else:
newLetter = alphabet[((alphabet.index(text[char])+key)%25)]
newText += newLetter
print("Result:", newText)
def caesarCipher_hiddenkey_decode(): # Decodes one step at a time with user input
text = input("\nInput your message to decode.\n> ")
alphabet = "abcdefghijlmnopqrstuvwxyz"
key = 1
not_done = ""
while not_done == "":
newText = ""
print("\nDecoding...\n")
for char in range(len(text)):
if text[char].isupper():
alphabet = alphabet.upper()
elif text[char].islower():
alphabet = alphabet.lower()
if text[char].isalpha() == False:
if isInt_fun(text[char]):
newLetter = str((int(text[char]) - key)%10)
else:
newLetter = text[char]
else:
newLetter = alphabet[((alphabet.index(text[char])-key)%25)]
newText += newLetter
key += 1 # Iterate through all possible key values one step at a time.
print("Result: " + newText)
not_done = input("\nInput something if done.\n> ") # User can tell the script when to stop cycling.
print("\nDone!\n\nThe final result is: " + newText)
init_fun() # Begins the script