-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubCipher.py
More file actions
36 lines (30 loc) · 1.35 KB
/
subCipher.py
File metadata and controls
36 lines (30 loc) · 1.35 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
import utils
PERMS = None
def decrypt(cipherText, key):
return ''.join(list(map(key.get, cipherText)))
def genKey(key, keyGenType, itr):
if keyGenType == 'manual':
cipherWord = input("Enter an incorrect word from the plain text: ").upper()
plainWord = input("Enter the correct word: ").upper()
if len(cipherWord) != len(plainWord):
print("Length of words must match! Key failed to update.")
return key
elif not (cipherWord.isalpha() and plainWord.isalpha()):
print("Numbers can not be entered! Key failed to update.")
else:
cipherWordArray = []
cipherWordArray[:0] = cipherWord
plainWordArray = []
plainWordArray[:0] = plainWord
inverseKey = dict(zip(key.values(), key.keys()))
for i in range(0, len(cipherWordArray)):
if cipherWordArray[i] != plainWordArray[i]:
key[inverseKey[cipherWordArray[i]]] = plainWordArray[i]
key[inverseKey[plainWordArray[i]]] = cipherWordArray[i]
print("Key updated! Try substitution cipher again.")
elif keyGenType == 'auto':
global PERMS
if PERMS is None or itr == 0:
PERMS = utils.permutations(key.values(), n=20, unique=True)
key = dict(zip(key.keys(), PERMS[itr]))
return key