-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.py
32 lines (22 loc) · 851 Bytes
/
encrypt.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
import string
letters = string.ascii_lowercase
message = input("\nEnter the message to be encrypted: ").lower()
plaintext = ""
for letter in message:
if letter.isalpha():
plaintext += letter
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.")
ciphertext = ""
for i, letter in enumerate(plaintext):
ciphertext += letters[(letters.index(letter) + letters.index(key[i])) % 26]
print("\nThe used portion of the key has been automatically deleted.\nEncrypted message: " + ciphertext + "\n")
key = key[len(plaintext):]
with open(fileName, "w") as f:
f.write(key)