-
Notifications
You must be signed in to change notification settings - Fork 0
/
Caesar cipher.py
45 lines (36 loc) · 1.6 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
37
38
39
40
41
42
43
44
45
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']
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 caesar(start_text, shift_amount, cipher_direction):
end_text = ""
if cipher_direction == "decode":
shift_amount *= -1 # 5 * -1 = -5
for letter in start_text:
position = alphabet.index(letter)
new_position = position + shift_amount
end_text += alphabet[new_position]
print(f"The {cipher_direction}d value is {end_text}")
caesar(start_text=text, shift_amount=shift, cipher_direction=direction)
# def encrypt(plain_text, shift_amount):
# cipher_text = ""
# for letter in plain_text:
# position = alphabet.index(letter)
# new_position = position + shift_amount
# cipher_text += alphabet[new_position]
# print(f"The encoded text is {cipher_text}")
#
# def decrypt(cipher_text, shift_amount):
# plain_text = ""
# for letter in cipher_text:
# position = alphabet.index(letter)
# new_position = position - shift_amount
# plain_text += alphabet[new_position]
# print(f"The decoded text is {plain_text}")
#
# if direction == "encode":
# encrypt(plain_text=text, shift_amount=shift)
# elif direction == "decode":
# decrypt(cipher_text=text, shift_amount=shift)
# else:
# print("invalid")