-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVigenere.py
More file actions
54 lines (38 loc) · 1.58 KB
/
Vigenere.py
File metadata and controls
54 lines (38 loc) · 1.58 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
"""
takes a textfile as input and prints out either ciphertext or plaintext depending on selected dictionary module
"""
import string
alpha = string.ascii_lowercase
decipher_dict = {alpha[x]: dict(zip(alpha[x:] + alpha[:x], alpha)) for x in range(len(alpha))}
encipher_dect = {alpha[x]: dict(zip(alpha, alpha[x:] + alpha[:x])) for x in range(len(alpha))}
def cipher_letter(letter, cipher_dict, code_counter, codephrase):
return cipher_dict[codephrase[code_counter]][letter]
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().lower()
codephrase = input("Enter the code phrase (no spaces or punctuation): ").lower()
encoding = ""
while encoding not in ["e", "d"]:
encoding = input("Decipher or Encipher? (enter D or E): ").lower()
if encoding == 'e':
c_dict = encipher_dect
else:
c_dict = decipher_dict
temptext = []
code_counter = -1
for letter in filetext:
if letter in alpha:
code_counter += 1
if code_counter == len(codephrase):
code_counter = 0
temptext.append(cipher_letter(letter, c_dict, code_counter, codephrase))
elif letter in string.printable:
temptext.append(letter)
print("".join(temptext))
if __name__ == "__main__":
main()