3838
3939import string
4040
41- import numpy
41+ import numpy as np
4242
4343from maths .greatest_common_divisor import greatest_common_divisor
4444
@@ -49,11 +49,11 @@ class HillCipher:
4949 # i.e. a total of 36 characters
5050
5151 # take x and return x % len(key_string)
52- modulus = numpy .vectorize (lambda x : x % 36 )
52+ modulus = np .vectorize (lambda x : x % 36 )
5353
54- to_int = numpy .vectorize (round )
54+ to_int = np .vectorize (round )
5555
56- def __init__ (self , encrypt_key : numpy .ndarray ) -> None :
56+ def __init__ (self , encrypt_key : np .ndarray ) -> None :
5757 """
5858 encrypt_key is an NxN numpy array
5959 """
@@ -63,7 +63,7 @@ def __init__(self, encrypt_key: numpy.ndarray) -> None:
6363
6464 def replace_letters (self , letter : str ) -> int :
6565 """
66- >>> hill_cipher = HillCipher(numpy .array([[2, 5], [1, 6]]))
66+ >>> hill_cipher = HillCipher(np .array([[2, 5], [1, 6]]))
6767 >>> hill_cipher.replace_letters('T')
6868 19
6969 >>> hill_cipher.replace_letters('0')
@@ -73,7 +73,7 @@ def replace_letters(self, letter: str) -> int:
7373
7474 def replace_digits (self , num : int ) -> str :
7575 """
76- >>> hill_cipher = HillCipher(numpy .array([[2, 5], [1, 6]]))
76+ >>> hill_cipher = HillCipher(np .array([[2, 5], [1, 6]]))
7777 >>> hill_cipher.replace_digits(19)
7878 'T'
7979 >>> hill_cipher.replace_digits(26)
@@ -83,10 +83,10 @@ def replace_digits(self, num: int) -> str:
8383
8484 def check_determinant (self ) -> None :
8585 """
86- >>> hill_cipher = HillCipher(numpy .array([[2, 5], [1, 6]]))
86+ >>> hill_cipher = HillCipher(np .array([[2, 5], [1, 6]]))
8787 >>> hill_cipher.check_determinant()
8888 """
89- det = round (numpy .linalg .det (self .encrypt_key ))
89+ det = round (np .linalg .det (self .encrypt_key ))
9090
9191 if det < 0 :
9292 det = det % len (self .key_string )
@@ -101,7 +101,7 @@ def check_determinant(self) -> None:
101101
102102 def process_text (self , text : str ) -> str :
103103 """
104- >>> hill_cipher = HillCipher(numpy .array([[2, 5], [1, 6]]))
104+ >>> hill_cipher = HillCipher(np .array([[2, 5], [1, 6]]))
105105 >>> hill_cipher.process_text('Testing Hill Cipher')
106106 'TESTINGHILLCIPHERR'
107107 >>> hill_cipher.process_text('hello')
@@ -117,7 +117,7 @@ def process_text(self, text: str) -> str:
117117
118118 def encrypt (self , text : str ) -> str :
119119 """
120- >>> hill_cipher = HillCipher(numpy .array([[2, 5], [1, 6]]))
120+ >>> hill_cipher = HillCipher(np .array([[2, 5], [1, 6]]))
121121 >>> hill_cipher.encrypt('testing hill cipher')
122122 'WHXYJOLM9C6XT085LL'
123123 >>> hill_cipher.encrypt('hello')
@@ -129,7 +129,7 @@ def encrypt(self, text: str) -> str:
129129 for i in range (0 , len (text ) - self .break_key + 1 , self .break_key ):
130130 batch = text [i : i + self .break_key ]
131131 vec = [self .replace_letters (char ) for char in batch ]
132- batch_vec = numpy .array ([vec ]).T
132+ batch_vec = np .array ([vec ]).T
133133 batch_encrypted = self .modulus (self .encrypt_key .dot (batch_vec )).T .tolist ()[
134134 0
135135 ]
@@ -140,14 +140,14 @@ def encrypt(self, text: str) -> str:
140140
141141 return encrypted
142142
143- def make_decrypt_key (self ) -> numpy .ndarray :
143+ def make_decrypt_key (self ) -> np .ndarray :
144144 """
145- >>> hill_cipher = HillCipher(numpy .array([[2, 5], [1, 6]]))
145+ >>> hill_cipher = HillCipher(np .array([[2, 5], [1, 6]]))
146146 >>> hill_cipher.make_decrypt_key()
147147 array([[ 6, 25],
148148 [ 5, 26]])
149149 """
150- det = round (numpy .linalg .det (self .encrypt_key ))
150+ det = round (np .linalg .det (self .encrypt_key ))
151151
152152 if det < 0 :
153153 det = det % len (self .key_string )
@@ -158,16 +158,14 @@ def make_decrypt_key(self) -> numpy.ndarray:
158158 break
159159
160160 inv_key = (
161- det_inv
162- * numpy .linalg .det (self .encrypt_key )
163- * numpy .linalg .inv (self .encrypt_key )
161+ det_inv * np .linalg .det (self .encrypt_key ) * np .linalg .inv (self .encrypt_key )
164162 )
165163
166164 return self .to_int (self .modulus (inv_key ))
167165
168166 def decrypt (self , text : str ) -> str :
169167 """
170- >>> hill_cipher = HillCipher(numpy .array([[2, 5], [1, 6]]))
168+ >>> hill_cipher = HillCipher(np .array([[2, 5], [1, 6]]))
171169 >>> hill_cipher.decrypt('WHXYJOLM9C6XT085LL')
172170 'TESTINGHILLCIPHERR'
173171 >>> hill_cipher.decrypt('85FF00')
@@ -180,7 +178,7 @@ def decrypt(self, text: str) -> str:
180178 for i in range (0 , len (text ) - self .break_key + 1 , self .break_key ):
181179 batch = text [i : i + self .break_key ]
182180 vec = [self .replace_letters (char ) for char in batch ]
183- batch_vec = numpy .array ([vec ]).T
181+ batch_vec = np .array ([vec ]).T
184182 batch_decrypted = self .modulus (decrypt_key .dot (batch_vec )).T .tolist ()[0 ]
185183 decrypted_batch = "" .join (
186184 self .replace_digits (num ) for num in batch_decrypted
@@ -199,7 +197,7 @@ def main() -> None:
199197 row = [int (x ) for x in input ().split ()]
200198 hill_matrix .append (row )
201199
202- hc = HillCipher (numpy .array (hill_matrix ))
200+ hc = HillCipher (np .array (hill_matrix ))
203201
204202 print ("Would you like to encrypt or decrypt some text? (1 or 2)" )
205203 option = input ("\n 1. Encrypt\n 2. Decrypt\n " )
0 commit comments