@@ -183,15 +183,15 @@ def process_text(self, text: str) -> str:
183183 'ABCC'
184184 """
185185 chars = [char for char in text .upper () if char in self .key_string ]
186-
186+
187187 # Handle empty input case
188188 if not chars :
189189 return ""
190-
190+
191191 last = chars [- 1 ]
192192 while len (chars ) % self .break_key != 0 :
193193 chars .append (last )
194-
194+
195195 return "" .join (chars )
196196
197197 def encrypt (self , text : str ) -> str :
@@ -221,21 +221,21 @@ def encrypt(self, text: str) -> str:
221221 text = self .process_text (text .upper ())
222222 if not text :
223223 return ""
224-
224+
225225 encrypted = ""
226226
227227 for i in range (0 , len (text ) - self .break_key + 1 , self .break_key ):
228228 # Extract batch of characters
229229 batch = text [i : i + self .break_key ]
230-
230+
231231 # Convert to numerical vector
232232 vec = [self .replace_letters (char ) for char in batch ]
233233 batch_vec = np .array ([vec ]).T
234-
234+
235235 # Matrix multiplication and mod 36
236236 product = self .encrypt_key .dot (batch_vec )
237237 batch_encrypted = self .modulus (product ).T .tolist ()[0 ]
238-
238+
239239 # Convert back to characters
240240 encrypted_batch = "" .join (
241241 self .replace_digits (num ) for num in batch_encrypted
@@ -244,7 +244,6 @@ def encrypt(self, text: str) -> str:
244244
245245 return encrypted
246246
247-
248247 def make_decrypt_key (self ) -> np .ndarray :
249248 """
250249 Compute decryption key matrix from encryption key.
@@ -261,7 +260,7 @@ def make_decrypt_key(self) -> np.ndarray:
261260 >>> cipher.make_decrypt_key()
262261 array([[ 6, 25],
263262 [ 5, 26]])
264-
263+
265264 >>> key3x3 = np.array([[1,2,3],[4,5,6],[7,8,9]])
266265 >>> cipher3 = HillCipher(key3x3)
267266 >>> cipher3.make_decrypt_key() # Determinant 0 should be invalid
@@ -275,7 +274,7 @@ def make_decrypt_key(self) -> np.ndarray:
275274
276275 if det < 0 :
277276 det = det % len (self .key_string )
278-
277+
279278 det_inv : int | None = None
280279 for i in range (len (self .key_string )):
281280 if (det * i ) % len (self .key_string ) == 1 :
@@ -318,22 +317,22 @@ def decrypt(self, text: str) -> str:
318317 text = self .process_text (text .upper ())
319318 if not text :
320319 return ""
321-
320+
322321 decrypt_key = self .make_decrypt_key ()
323322 decrypted = ""
324323
325324 for i in range (0 , len (text ) - self .break_key + 1 , self .break_key ):
326325 # Extract batch of characters
327326 batch = text [i : i + self .break_key ]
328-
327+
329328 # Convert to numerical vector
330329 vec = [self .replace_letters (char ) for char in batch ]
331330 batch_vec = np .array ([vec ]).T
332-
331+
333332 # Matrix multiplication and mod 36
334333 product = decrypt_key .dot (batch_vec )
335334 batch_decrypted = self .modulus (product ).T .tolist ()[0 ]
336-
335+
337336 # Convert back to characters
338337 decrypted_batch = "" .join (
339338 self .replace_digits (num ) for num in batch_decrypted
@@ -346,7 +345,7 @@ def decrypt(self, text: str) -> str:
346345def main () -> None :
347346 """
348347 Command-line interface for Hill Cipher operations.
349-
348+
350349 Steps:
351350 1. User inputs encryption key size
352351 2. User inputs encryption key matrix rows
@@ -359,14 +358,14 @@ def main() -> None:
359358
360359 print ("Enter each row of the encryption key with space separated integers" )
361360 for i in range (n ):
362- row = [int (x ) for x in input (f"Row { i + 1 } : " ).split ()]
361+ row = [int (x ) for x in input (f"Row { i + 1 } : " ).split ()]
363362 hill_matrix .append (row )
364363
365364 hc = HillCipher (np .array (hill_matrix ))
366365
367366 print ("\n Would you like to encrypt or decrypt some text?" )
368367 option = input ("1. Encrypt\n 2. Decrypt\n Enter choice (1/2): " )
369-
368+
370369 if option == "1" :
371370 text = input ("\n Enter text to encrypt: " )
372371 print ("\n Encrypted text:" )
@@ -381,20 +380,21 @@ def main() -> None:
381380
382381if __name__ == "__main__" :
383382 import doctest
383+
384384 doctest .testmod ()
385-
385+
386386 print ("\n Running sample tests..." )
387387 key = np .array ([[2 , 5 ], [1 , 6 ]])
388388 cipher = HillCipher (key )
389-
389+
390390 # Test encryption/decryption round trip
391391 plaintext = "HELLO123"
392392 encrypted = cipher .encrypt (plaintext )
393393 decrypted = cipher .decrypt (encrypted )
394-
394+
395395 print (f"\n Original text: { plaintext } " )
396396 print (f"Encrypted text: { encrypted } " )
397397 print (f"Decrypted text: { decrypted } " )
398-
398+
399399 # Run CLI interface
400400 main ()
0 commit comments