-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaesar_cipher.py
36 lines (34 loc) · 1.42 KB
/
Caesar_cipher.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
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
def encryption(plain_text,shift_key):
cipher_text = ""
for char in plain_text:
if char in alphabet:
position = alphabet.index(char)
new_position = (position+shift_key)%26
cipher_text += alphabet[new_position]
else:
cipher_text += char
print(f"Here's is the text after encryption: {cipher_text}")
def decryption(cipher_text,shift_key):
plain_text = ""
for char in cipher_text:
if char in alphabet:
position = alphabet.index(char)
new_position = (position-shift_key)%26
plain_text += alphabet[new_position]
else:
plain_text += char
print(f"Here's is the text after decryption: {plain_text}")
end_program = False
while not end_program:
what_to_do = input("Type 'encrypt' for encryption, type 'decrypt' for decryption:\n")
text = input("Type your message:\n").lower()
shift = int(input("Enter shift key:\n"))
if what_to_do=="encrypt":
encryption(plain_text=text, shift_key=shift)
elif what_to_do=="decrypt":
decryption(cipher_text=text, shift_key=shift)
play_again = input("Type 'yes' to continue, type 'no' to exit.\n")
if play_again == 'no':
end_program = True
print("Have a nice day! Bye..")