-
Notifications
You must be signed in to change notification settings - Fork 0
/
Caesar Cipher Improvement 3.0.py
137 lines (94 loc) · 4.19 KB
/
Caesar Cipher Improvement 3.0.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
Another way to create an evolution of the Caesar Cipher
"""
"""
The information that a user have to type each time
"""
mode = int(input('Enter 1. For Encryption: \n'
'Enter 2. For Decryption: \n'
))
message = input("Please type your message here: \n")
key = int(input("Please type the number of shifts you would like to make : \n"))
def Encryption_Caesar(message, key):
encrypted_message = ' '
for letter in message:
# Here we check if a letter is uppercase and then we transport it
if letter.isupper():
letter_index = ord(letter) - ord('A')
letter_shifted = (letter_index + key) % 26 + ord('A')
letter_new = chr(letter_shifted)
encrypted_message += letter_new
# Here we check if a letter is lowercase
elif letter.islower():
# check if it's an lowercase character and then we transport it
letter_index = ord(letter) - ord('a')
letter_shifted = (letter_index + key) % 26 + ord('a')
letter_new = chr(letter_shifted)
encrypted_message += letter_new
# Here we check if we have a digit and then we transport it
elif letter.isdigit():
"""
If it's a number then we shift it the key value
For example if we have the number 3 and our key is 5 then the new number will be 8
"""
letter_new = (int(letter) + key) % 10
encrypted_message += str(letter_new)
elif letter.isspace():
encrypted_message += ' '
# Here we check if we have a punctuation and then we transport it
else:
""""
The total number of the punctuation are 32
It shifts the punctuation key values according to ASCII table
If you are confused check the ASCII table
"""
letter_index = ord(letter) - ord('!')
letter_shifted = (letter_index + key) % 32 + ord('!')
letter_new = chr(letter_shifted)
encrypted_message += letter_new
return encrypted_message
def Decryption_Caesar(message, key):
decrypted_message = ' '
for letter in message:
if letter.isupper():
# check if it's an uppercase character and then we transport it
letter_index = ord(letter) - ord('A')
letter_shifted = (letter_index - key) % 26 + ord('A')
letter_new = chr(letter_shifted)
decrypted_message += letter_new
elif letter.islower():
# check if it's an lowercase character and then we transport it
letter_index = ord(letter) - ord('a')
letter_shifted = (letter_index - key) % 26 + ord('a')
letter_new = chr(letter_shifted)
decrypted_message += letter_new
elif letter.isdigit():
"""
Check if it's a number
If it's a number then it shift it the key value
For example if we have the number 3 and our key is 5 then the new number will be 8
"""
letter_new = (int(letter) - key) % 10
decrypted_message += str(letter_new)
elif letter.isspace():
decrypted_message += ' '
else:
"""
Check if it's a punctuation
The total number of the punctuation are 32
If it's a number then it shift it the key value
If you are confused check the ascii table
"""
letter_index = ord(letter) - ord('!')
letter_shifted = (letter_index - key) % 32 + ord('!')
letter_new = chr(letter_shifted)
decrypted_message += letter_new
return decrypted_message
def Caesar(message, key, mode):
if mode == 1:
print('Your encrypted message is the following below:', Encryption_Caesar(message,key))
elif mode ==2:
print('Your encrypted message is the following below:', Decryption_Caesar(message,key))
else:
print('Please type a right choice and run me again')
Caesar(message,key,mode)