-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathciphers.py
More file actions
51 lines (40 loc) · 1.24 KB
/
ciphers.py
File metadata and controls
51 lines (40 loc) · 1.24 KB
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
#Aid array
abc = ['A', 'B', 'C', 'D', 'E', 'F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
msg = []
def getShiftedArray(shift):
temp = list(abc)
final = temp[shift:] + temp[:shift]
return final
def encryptCaesar(shift, text):
del msg[:]
alph = getShiftedArray(shift%26)
for i in range(0, len(text)):
if(text[i] != ' '):
msg.append(alph[ord(str.upper(text[i])) - 65])
continue;
msg.append(" ")
return ''.join(msg)
def decryptCaesar(shift, text):
del msg[:]
alph = getShiftedArray(26 - (shift%26))
for i in range(0, len(text)):
if(text[i] != ' '):
msg.append(alph[ord(str.upper(text[i])) - 65])
continue;
msg.append(" ")
return ''.join(msg)
def encryptVigenere(key, text):
del msg[:]
for i in range(0, len(text)):
shift = (ord(text[i])- 65)
row = getShiftedArray(shift)
msg.append(row[ord(key[i % len(key)]) - 65])
return ''.join(msg)
def decryptVigenere(key, enc_text):
del msg[:]
for i in range(0, len(enc_text)):
shift = (ord(key[i % len(key)]) - 65)
row = getShiftedArray(26 - shift)
msg.append(row[(ord(enc_text[i]) - 65)])
return ''.join(msg)
print(encryptVigenere("FORTIFICATION", "DEFENDTHEEASTWALLOFTHECASTLE"))