-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathceaser.py
More file actions
42 lines (34 loc) · 1.44 KB
/
ceaser.py
File metadata and controls
42 lines (34 loc) · 1.44 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
"""
Ceaser cipher decipher/encipher code. main prints all transpositions. Ceaser() takes a text input, dict and returns
ciphertext/plaintext, depending on the specified code dictionary
"""
import string
alpha = string.ascii_lowercase
alltrans_dict = {alpha[x]: dict(zip(alpha[x:] + alpha[:x], alpha)) for x in range(len(alpha))}
def decipher(text, cipherdict):
"""
takes a text input and transforms that by a simple replacement specified by the given dict
:param text: probably the ciphertext, but if you put plaintext here, it will encipher it per the dict param
:param cipherdict: a 1:1 dict lining up a letter with a replacement
:return: the plaintext/ciphertext after replacement per dict param
"""
templist = []
for letter in text:
if letter.lower() in alpha:
templist.append(cipherdict[letter.lower()])
elif letter in string.printable:
templist.append(letter)
return "".join(templist)
def main():
"""
inputs a text file name, transposes all possible ceaser type transpositions and prints out all options.
:return: nothing
"""
filename = input("Enter filename, include path if not in same directory: ")
with open(filename, 'r', encoding = 'utf-8') as f:
filetext = f.read()
print(filetext)
for letter in alpha:
print(decipher(filetext, alltrans_dict[letter]))
if __name__ == "__main__":
main()