-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchesar_cipher_1.py
40 lines (33 loc) · 1.3 KB
/
chesar_cipher_1.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
37
38
39
40
alphabet = 'abcdefghijklmnopqrstuvwxyz'
direction = input("Type 'encode' to encrypt, Type 'decode' to decrypt: ")
text = input("Type your message: ").lower()
shift = int(input("Type the shift number: "))
def encrypt(plain_text, shift_amount):
cipher_text = ""
for letter in plain_text:
if letter in alphabet:
position = alphabet.index(letter) # index is a build-in python function that returns the index of the first
# encountered character
new_position = (position + shift_amount) % 26
new_letter = alphabet[new_position]
cipher_text += new_letter
else:
cipher_text += letter
print(f"The encrypted text is: {cipher_text}")
def decrypt(cipher_text, shift_amount):
plain_text = ""
for letter in cipher_text:
if letter in alphabet:
position = alphabet.index(letter)
new_position = (position - shift_amount) % 26
new_letter = alphabet[new_position]
plain_text += new_letter
else:
plain_text += letter
print(f"The decoded text is: {plain_text}")
if direction == "encode":
encrypt(text, shift)
elif direction == "decode":
decrypt(text, shift)
else:
print("Wrong selection!! run again..")