@@ -184,15 +184,15 @@ def process_text(self, text: str) -> str:
184184 'ABCC'
185185 """
186186 chars = [char for char in text .upper () if char in self .key_string ]
187-
187+
188188 # Handle empty input case
189189 if not chars :
190190 return ""
191-
191+
192192 last = chars [- 1 ]
193193 while len (chars ) % self .break_key != 0 :
194194 chars .append (last )
195-
195+
196196 return "" .join (chars )
197197
198198 def encrypt (self , text : str ) -> str :
@@ -222,28 +222,29 @@ def encrypt(self, text: str) -> str:
222222 text = self .process_text (text .upper ())
223223 if not text :
224224 return ""
225-
225+
226226 encrypted = ""
227227
228228 for i in range (0 , len (text ) - self .break_key + 1 , self .break_key ):
229229 # Extract batch of characters
230230 batch = text [i : i + self .break_key ]
231-
231+
232232 # Convert to numerical vector
233233 vec = [self .replace_letters (char ) for char in batch ]
234234 batch_vec = np .array ([vec ]).T
235-
235+
236236 # Matrix multiplication and mod 36
237237 product = self .encrypt_key .dot (batch_vec )
238238 batch_encrypted = self .modulus (product ).T .tolist ()[0 ]
239-
239+
240240 # Convert back to characters
241241 encrypted_batch = "" .join (
242242 self .replace_digits (num ) for num in batch_encrypted
243243 )
244244 encrypted += encrypted_batch
245245
246246 return encrypted
247+
247248 def make_decrypt_key (self ) -> np .ndarray :
248249 """
249250 Compute decryption key matrix from encryption key.
@@ -260,7 +261,7 @@ def make_decrypt_key(self) -> np.ndarray:
260261 >>> cipher.make_decrypt_key()
261262 array([[ 6, 25],
262263 [ 5, 26]])
263-
264+
264265 >>> key3x3 = np.array([[1,2,3],[4,5,6],[7,8,9]])
265266 >>> cipher3 = HillCipher(key3x3)
266267 >>> cipher3.make_decrypt_key() # Determinant 0 should be invalid
@@ -272,7 +273,7 @@ def make_decrypt_key(self) -> np.ndarray:
272273 det = int (round (np .linalg .det (self .encrypt_key )))
273274 if det < 0 :
274275 det = det % len (self .key_string )
275-
276+
276277 det_inv : Optional [int ] = None
277278 for i in range (len (self .key_string )):
278279 if (det * i ) % len (self .key_string ) == 1 :
@@ -315,22 +316,22 @@ def decrypt(self, text: str) -> str:
315316 text = self .process_text (text .upper ())
316317 if not text :
317318 return ""
318-
319+
319320 decrypt_key = self .make_decrypt_key ()
320321 decrypted = ""
321322
322323 for i in range (0 , len (text ) - self .break_key + 1 , self .break_key ):
323324 # Extract batch of characters
324325 batch = text [i : i + self .break_key ]
325-
326+
326327 # Convert to numerical vector
327328 vec = [self .replace_letters (char ) for char in batch ]
328329 batch_vec = np .array ([vec ]).T
329-
330+
330331 # Matrix multiplication and mod 36
331332 product = decrypt_key .dot (batch_vec )
332333 batch_decrypted = self .modulus (product ).T .tolist ()[0 ]
333-
334+
334335 # Convert back to characters
335336 decrypted_batch = "" .join (
336337 self .replace_digits (num ) for num in batch_decrypted
@@ -343,7 +344,7 @@ def decrypt(self, text: str) -> str:
343344def main () -> None :
344345 """
345346 Command-line interface for Hill Cipher operations.
346-
347+
347348 Steps:
348349 1. User inputs encryption key size
349350 2. User inputs encryption key matrix rows
@@ -356,14 +357,14 @@ def main() -> None:
356357
357358 print ("Enter each row of the encryption key with space separated integers" )
358359 for i in range (n ):
359- row = [int (x ) for x in input (f"Row { i + 1 } : " ).split ()]
360+ row = [int (x ) for x in input (f"Row { i + 1 } : " ).split ()]
360361 hill_matrix .append (row )
361362
362363 hc = HillCipher (np .array (hill_matrix ))
363364
364365 print ("\n Would you like to encrypt or decrypt some text?" )
365366 option = input ("1. Encrypt\n 2. Decrypt\n Enter choice (1/2): " )
366-
367+
367368 if option == "1" :
368369 text = input ("\n Enter text to encrypt: " )
369370 print ("\n Encrypted text:" )
@@ -378,20 +379,21 @@ def main() -> None:
378379
379380if __name__ == "__main__" :
380381 import doctest
382+
381383 doctest .testmod ()
382-
384+
383385 print ("\n Running sample tests..." )
384386 key = np .array ([[2 , 5 ], [1 , 6 ]])
385387 cipher = HillCipher (key )
386-
388+
387389 # Test encryption/decryption round trip
388390 plaintext = "HELLO123"
389391 encrypted = cipher .encrypt (plaintext )
390392 decrypted = cipher .decrypt (encrypted )
391-
393+
392394 print (f"\n Original text: { plaintext } " )
393395 print (f"Encrypted text: { encrypted } " )
394396 print (f"Decrypted text: { decrypted } " )
395-
397+
396398 # Run CLI interface
397399 main ()
0 commit comments