-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaesarCipher.py
36 lines (33 loc) · 1.21 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
dict = "abcdefghijklmnñopqrstuvwxyz"
def crypt(text, rots):
text = list(text.lower())
for index, letter in enumerate(text):
if letter in dict:
originalIndex = dict.index(letter)
cipheredIndex = (originalIndex+rots) % len(dict)
text[index] = dict[cipheredIndex]
return "".join(text)
def decrypt(text, rots):
text = list(text)
for index, letter in enumerate(text):
if letter in dict:
cipheredIndex = dict.index(letter)
originalIndex = cipheredIndex-rots
text[index] = dict[originalIndex]
return "".join(text)
if __name__ == "__main__":
opt = input("Encrypt or Decrypt? (E/D): ")
if opt.lower() == "e":
try:
text = input("Input text to encrypt: ")
rots = int(input("Number of rotations: "))
except:
print("Something went wrong, please enter data correctly")
print(crypt(text, rots))
elif opt.lower() == "d":
try:
text = input("Input text to decrypt: ")
rots = int(input("Number of rotations: "))
except:
print("Something went wrong, please enter data correctly")
print(decrypt(text, rots))