-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathencryption_caesar_cipher.py
51 lines (40 loc) · 1.39 KB
/
encryption_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
# CAESAR CIPHER ENCRYPTION
# This program encrypts a given string by the user following the Caesar Cipher encryption rules
# By Alessandro Silvestri, assignement solved for Python Institute
# function for shifting the code point. (in limit and start I'll put the cod point of 'z', 'Z' and 'a', 'A'
def lower_code_shift(letter, shift, limit, start):
code = ord(letter)
for i in range(shift):
if code == limit:
code = start
code += 1
return code
txt = input('insert a string: ')
# I ask a string and a number, I check if the number is between 1 and 25 and if he doesn't put a number
while True:
try:
shift = int(input('insert a shift number between 1 and 25: '))
if shift not in range(1, 26):
print(f'{shift} is not valid')
else:
break
except:
print('Error: a number is required')
# I inizialize the container variables
code = 0
encryptetd_word = ''
# I hiterate the string
for j in txt:
# part of code if the character is lowercase
if j.islower() or not j.isalnum() or j.isnumeric():
if j.isalpha():
encryptetd_word += chr(lower_code_shift(j, shift, 122, 96))
else:
encryptetd_word += j
# part of code if the character is upper
elif j.isupper() or not j.isalnum() or j.isnumeric():
if j.isalpha():
encryptetd_word += chr(lower_code_shift(j, shift, 90, 64))
else:
encryptetd_word += j
print(encryptetd_word)