-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar_cipher.py
More file actions
42 lines (38 loc) · 1003 Bytes
/
caesar_cipher.py
File metadata and controls
42 lines (38 loc) · 1003 Bytes
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
def caesar(str_1, num):
str_new = []
str_bytes = str_1.encode()
for byte in str_bytes:
if byte >= 65 and byte <= 90:
byte += num
if byte < 65 or byte > 90:
byte = check_wrap_capital(byte)
elif byte >= 97 and byte <= 122:
byte += num
if byte < 97 or byte > 122:
byte = check_wrap_lower(byte)
str_new.append(byte)
str_decode = bytearray(str_new).decode()
return str_decode
def check_wrap_capital(byte):
if byte > 90:
while(byte > 90):
byte = (byte - 90) + 65 - 1
else:
while(byte < 65):
byte = 90 - (65 - byte) + 1
return byte
def check_wrap_lower(byte):
if byte > 122:
while(byte > 122):
byte = (byte - 122) + 97 - 1
else:
while(byte < 97):
byte = 122 - (97 - byte) + 1
return byte
print(caesar('A',1))
print(caesar('Aaa',1))
print(caesar('Hello, World!', 5))
print(caesar('Mjqqt, Btwqi!', -5))
print(caesar('Z',1))
print(caesar('Hello, World!', 75))
print(caesar("Hello, World!",-29))