-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesar_cipher.py
60 lines (50 loc) · 2.06 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#This program takes in input from user and encodes the message using a Caesar Cipher.
from enum import IntEnum
class Action(IntEnum):
Encrypt = 0
Decrypt = 1
def get_user_choice():
choices = [f"{action.name}[{action.value}]" for action in Action]
choices_str=",".join(choices)
selection=int(input(f"Enter a choice to either encrypt or decrypt a caeser cipher message ({choices_str}): "))
action=Action(selection)
return action
def caesar_cipher(message,encryption_key):
result = ""
for i, letter in enumerate(message):
if (letter.isupper()):
result += chr((ord(letter) + int(encryption_key)-65) % 26 + 65)
else:
result += chr((ord(letter) + int(encryption_key) - 97) % 26 + 97)
return result
def main():
while True:
try:
user_choice = get_user_choice()
except ValueError as e:
range_str = f"[0, {len(Action) - 1}]"
print(f"Invalid selection. Enter a value in range {range_str}")
continue
if user_choice==Action.Encrypt:
print("Enter a message to encrypt.")
message= input()
print("Enter a shift key.")
encryption_key=input()
print ("Plain message : " + message)
print ("Shift pattern : " + encryption_key)
encrypted_message=caesar_cipher(message,encryption_key)
print( "Cipher: " + encrypted_message)
else:
print("Decrypt a caesar cipher message by entering the encrypted message and the shift key")
print("Enter a encrypted message: ")
inputed_encrypted_message=input()
print("Enter the Caesar Cipher shift key: ")
inputed_encryption_key=input()
decrypted_message=caesar_cipher(inputed_encrypted_message, (26-(int(inputed_encryption_key))))
print(decrypted_message)
keep_going=input("Do yo want to encrypt or decrpty another message? (y/n): ")
if keep_going.lower() !="y":
break
# __name__
if __name__=="__main__":
main()