-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathKeyedTranspositionCipher.py
38 lines (32 loc) · 1.4 KB
/
KeyedTranspositionCipher.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
class KeyedTranspositionCipher:
def __init__(self, key):
self.key = key
self.decryption_permutation = self.get_decryption_permutation()
def encrypt(self, plaintext: str) -> str:
padding = (- (len(plaintext) % len(self.key))) % len(self.key)
plaintext = plaintext.lower() + 'z' * padding
ciphertext = ''
for index in range(0, len(plaintext), len(self.key)):
block = plaintext[index: index + len(self.key)]
ciphertext += self.encipher(block)
return ciphertext.upper()
def decrypt(self, ciphertext: str) -> str:
plaintext = ''
for index in range(0, len(ciphertext), len(self.key)):
block = ciphertext[index: index + len(self.key)].lower()
plaintext += self.decipher(block)
return plaintext
def decipher(self, block: str) -> str:
return self.crypt(block, key=self.decryption_permutation)
def encipher(self, block: str) -> str:
return self.crypt(block, key=self.key)
def crypt(self, block: str, key):
encrypted = ['a'] * len(self.key)
for index, letter in enumerate(block):
encrypted[key[index]] = letter
return ''.join(encrypted)
def get_decryption_permutation(self):
permutation = [0] * len(self.key)
for index, value in enumerate(self.key):
permutation[value] = index
return permutation