-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesar-Cipher.py
More file actions
59 lines (46 loc) · 1.76 KB
/
Caesar-Cipher.py
File metadata and controls
59 lines (46 loc) · 1.76 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from copyreg import constructor
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','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 encrypt(text, shift):
cipher_text = ""
for i in text:
position = alphabet.index(i)
new_position = position+shift
new_letter = alphabet[new_position]
cipher_text += new_letter
print(f"The encoded text is {cipher_text}")
def decrypt(text, shift):
plain_text = ""
for i in text:
position = alphabet.index(i)
new_position = position-shift
new_letter = alphabet[new_position]
plain_text += new_letter
print(f"The decoded text is {plain_text}")
def caesar(text, shift, direction):
output_text = ""
if direction == "decode" :
shift *= -1
for i in text:
if i in alphabet:
position = alphabet.index(i)
new_position = position + shift
new_letter = alphabet[new_position]
output_text += new_letter
else:
output_text += i
print(f"The decoded text is {output_text}")
should_end = False
while not should_end:
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
shift = shift % 26
caesar(text, shift, direction)
restart = input("Type 'yes' if you want to go again. Otherwise type 'no'.\n")
if restart == "no":
should_end = True
print("Goodbye")
# if direction=="encode" :
# encrypt(text, shift)
# elif direction=="decode" :
# decrypt(text, shift)