-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaesar-complete.py
75 lines (63 loc) · 1.75 KB
/
Caesar-complete.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
import string
alphabet = list(string.ascii_lowercase)
decoded = list()
encrypted = list()
print("Caesar Cipher Decrypter-----------------------------------\n\n")
def init():
global boolin
boolin = str(input("Would you like to (e)ncrpyt a message?\nOr, (d)ecrypt one?\n\n"))
print('\n')
init()
#print(boolin)
#Make Shifted List
def alphacs(shift):
diff = int(26 - shift)
#diff=difference
global sl
sl = alphabet [diff:26]
for x in range(diff):
sl.append(alphabet [x])
#sl = Shifted list
def encrypt(message,shift):
for i in range(len(message)):
char = message[i]
if char in alphabet:
pos = alphabet.index(char)
enchar = sl[pos]
#enchar = Encrypted Character
encrypted.append(enchar)
else:
otr = char
#otr= Other
encrypted.append(otr)
def decrypt(code,shift):
for i in range(len(code)):
char = code[i]
if char in alphabet:
pos = sl.index(char)
dechar = alphabet[pos]
#dechar = Decoded Character
decoded.append(dechar)
else:
otr = char
#otr= Other
decoded.append(otr)
#Encrypt
if boolin in ("e", "encrypt"):
message = str(input("Message to Encrypt\n\n"))
shift = int(input("Shift?\n\n"))
alphacs(shift)
encrypt(message,shift)
fencrypted = "".join(encrypted)
print("Encrypted Message:" + fencrypted)
#Decrypt
elif boolin in ("d", "decrypt"):
code = str(input("Encrypted Message\n\n"))
shift = int(input("Shift?\n\n"))
alphacs(shift)
decrypt(code,shift)
fdecoded = "".join(decoded)
print("Decrypted Code:" + fdecoded)
else:
print("invalid selection\n")
init()