-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecrypt.py
31 lines (23 loc) · 896 Bytes
/
decrypt.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
import string
letters = string.ascii_lowercase
while True:
ciphertext = input("\nEnter the message to be decrypted: ").lower()
if ciphertext.isalpha:
break
else:
print("Make sure you only include alphabet letters. Try again.")
while True:
try:
fileName = input("\nEnter the name of the file that contains the key (exclude the .txt): ").strip() + ".txt"
with open(fileName, "r") as f:
key = f.read()
break
except (FileNotFoundError, OSError):
print("This file doesn't exist. Try again.")
plaintext = ""
for i, letter in enumerate(ciphertext):
plaintext += letters[(letters.index(letter) - letters.index(key[i])) % 26]
print("\nThe used portion of the key has been automatically deleted.\nDecrypted message: " + plaintext + "\n")
key = key[len(ciphertext):]
with open(fileName, "w") as f:
f.write(key)