-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpolyalph.py
36 lines (33 loc) · 1.19 KB
/
polyalph.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
base = ('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')
mode = raw_input("Do you want to encrypt or decrypt")
text = raw_input("Please enter the text")
key = raw_input("Please enter the key word")
keyList = []
keyLength = 0
while keyLength < len(text):
for char in key:
if keyLength < len(text):
keyList.append(str(char))
keyLength = keyLength + 1
ctext = []
cipherCharIndexValue = 0
keyinc = 0
textChar = []
ctextChar = []
if mode == "encrypt":
for textChar in text:
cipherCharIndexValue = base.index(keyList[keyinc]) + base.index(textChar)
while cipherCharIndexValue > 25:
cipherCharIndexValue = cipherCharIndexValue - 26
ctext.append(base[cipherCharIndexValue])
keyinc = keyinc + 1
print (''.join(ctext))
if mode == "decrypt":
for textChar in text:
cipherCharIndexValue = base.index(textChar) - base.index(keyList[keyinc])
while cipherCharIndexValue > 25:
cipherCharIndexValue = cipherCharIndexValue - 26
ctext.append(base[cipherCharIndexValue])
keyinc = keyinc + 1
print (''.join(ctext))