-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCeasor-Cipher.py
81 lines (75 loc) · 1.83 KB
/
Ceasor-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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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",
]
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"))
def encrypt(text, shift):
new_word = []
new_sentence = []
text = text.split()
for words in text:
for chosen_letter in words:
i = 0
for letter in alphabet:
if chosen_letter == letter:
new_shift = i + shift
new_shift = new_shift % 26
position = alphabet[new_shift]
new_word.append(position)
i = i + 1
new_word = "".join(new_word)
new_sentence.append(new_word)
new_word = []
new_sentence = " ".join(new_sentence)
print(f"the encoded message is {new_sentence}")
def decrypt(text, shift):
alphabet.reverse()
new_word = []
new_sentence = []
text = text.split()
for words in text:
for chosen_letter in words:
i = 0
for letter in alphabet:
if chosen_letter == letter:
new_shift = i + shift
new_shift = new_shift % 26
position = alphabet[new_shift]
new_word.append(position)
i = i + 1
new_word = "".join(new_word)
new_sentence.append(new_word)
new_word = []
new_sentence = " ".join(new_sentence)
print(f"the decoded message is {new_sentence}")
alphabet.reverse()
if direction == "encode":
encrypt(text, shift)
elif direction == "decode":
decrypt(text, shift)