44
55import pyfiglet
66
7-
87def encrypt (input_string : str , key : int , alphabet : str | None = None ) -> str :
98 """
109 encrypt
@@ -73,41 +72,42 @@ def encrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
7372 # Use the provided alphabet if given, otherwise default to ascii_letters (a-z + A-Z)
7473 alpha = alphabet or ascii_letters
7574
76- # Create a shifted version of the alphabet by the key.
77- # This rotated alphabet will be used for mapping original characters
78- # to encrypted characters.
75+ # Create a shifted version of the alphabet by the key
76+ # This rotated alphabet will be used for mapping original characters to encrypted characters
7977 shifted = alpha [key % len (alpha ):] + alpha [:key % len (alpha )]
8078
8179 # Create a translation table: original alphabet -> shifted alphabet
8280 table = str .maketrans (alpha , shifted )
83-
81+
8482 # Apply the translation table to the input string
8583 # Characters not in the alphabet remain unchanged
8684 return input_string .translate (table )
8785
88- def encrypt_file (input_path : str , output_path : str , key : int , alphabet : str | None = None ):
8986
87+ def encrypt_file (
88+ input_path : str , output_path : str , key : int , alphabet : str | None = None
89+ ):
9090 """
9191 Encrypts a text file line by line using Caesar Cipher and writes
9292 the encrypted content to the output file.
9393 """
94-
94+
9595 # Use the provided alphabet if given; otherwise default to ascii_letters (a-z + A-Z)
9696 alpha = alphabet or ascii_letters
9797
9898 # Open input file for reading and output file for writing
99- with open (input_path ) as fin , open (output_path , 'w' ) as fout :
99+ with open (input_path , 'r' ) as fin , open (output_path , 'w' ) as fout :
100100
101101 # Read the input file line by line to avoid loading the entire file into memory
102102 for line in fin :
103-
104103 # Encrypt the current line using the encrypt function
105104 encrypted_line = encrypt (line , key , alpha )
106105
107106 # Write the encrypted line to the output file
108107 fout .write (encrypted_line )
109108 print ("File has been successfully been encrypted !!" )
110109
110+
111111def decrypt (input_string : str , key : int , alphabet : str | None = None ) -> str :
112112 """
113113 decrypt
@@ -179,30 +179,31 @@ def decrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
179179
180180 return encrypt (input_string , key , alphabet )
181181
182- def decrypt_file (input_path : str , output_path : str , key : int , alphabet : str | None = None ):
183182
183+ def decrypt_file (
184+ input_path : str , output_path : str , key : int , alphabet : str | None = None
185+ ):
184186 """
185187 Decrypts a text file line by line using Caesar Cipher and writes
186188 the decrypted content to the output file.
187189 """
188-
190+
189191 # Use the provided alphabet if given; otherwise default to ascii_letters (a-z + A-Z)
190192 alpha = alphabet or ascii_letters
191193
192194 # Open input file for reading and output file for writing
193- with open (input_path ) as fin , open (output_path , 'w' ) as fout :
195+ with open (input_path , 'r' ) as fin , open (output_path , 'w' ) as fout :
194196
195197 # Read the input file line by line to avoid loading the entire file into memory
196198 for line in fin :
197-
198199 # Encrypt the current line using the encrypt function
199200 decrypted_line = decrypt (line , key , alpha )
200201
201202 # Write the encrypted line to the output file
202203 fout .write (decrypted_line )
203204
204205 print ("File has been successfully been decrypted !!" )
205-
206+
206207
207208def brute_force (input_string : str , alphabet : str | None = None ) -> dict [int , str ]:
208209 """
@@ -271,9 +272,21 @@ def brute_force(input_string: str, alphabet: str | None = None) -> dict[int, str
271272 banner = pyfiglet .figlet_format ("Caesar Cipher" , font = "big" )
272273 print (banner )
273274 while True :
275+ banner = pyfiglet .figlet_format ("Caesar Ciphar" , font = "big" )
276+ print (banner )
274277 print (f"\n { '-' * 10 } \n Menu\n { '-' * 10 } " )
275278 print ("Please select from the following options: " )
276- print (* ["1.Encrypt" , "2.Encrypt a File" , "3.Decrypt" , "4.Decrypt a File" , "5.BruteForce" , "6.Quit" , ], sep = "\n " )
279+ print (
280+ * [
281+ "1.Encrypt" ,
282+ "2.Encrypt a File" ,
283+ "3.Decrypt" ,
284+ "4.Decrypt a File" ,
285+ "5.BruteForce" ,
286+ "6.Quit" ,
287+ ],
288+ sep = "\n " ,
289+ )
277290
278291 # get user input
279292 choice = input ("\n What would you like to do?: " ).strip () or "4"
@@ -284,28 +297,40 @@ def brute_force(input_string: str, alphabet: str | None = None) -> dict[int, str
284297 elif choice == "1" :
285298 input_string = input ("Please enter the string to be encrypted: " )
286299 key = int (input ("Please enter off-set: " ).strip ())
287- alphabet_input = input ("Enter custom alphabet (press Enter to use default): " ).strip () or None
300+ alphabet_input = (
301+ input ("Enter custom alphabet (press Enter to use default): " ).strip ()
302+ or None
303+ )
288304 print (encrypt (input_string , key , alphabet_input ))
289-
305+
290306 elif choice == "2" :
291307 input_file_path = input ("Please enter path of the input file: " )
292308 output_file_path = input ("Please enter path of the output file: " )
293309 key = int (input ("Please enter off-set: " ).strip ())
294- alphabet_input = input ("Enter custom alphabet (press Enter to use default): " ).strip () or None
310+ alphabet_input = (
311+ input ("Enter custom alphabet (press Enter to use default): " ).strip ()
312+ or None
313+ )
295314 encrypt_file (input_file_path , output_file_path , key , alphabet_input )
296-
315+
297316 elif choice == "3" :
298317 input_string = input ("Please enter the string to be decrypted: " )
299318 key = int (input ("Please enter off-set: " ).strip ())
300- alphabet_input = input ("Enter custom alphabet (press Enter to use default): " ).strip () or None
319+ alphabet_input = (
320+ input ("Enter custom alphabet (press Enter to use default): " ).strip ()
321+ or None
322+ )
301323
302324 print (decrypt (input_string , key , alphabet_input ))
303325
304326 elif choice == "4" :
305327 input_file_path = input ("Please enter path of the input file: " )
306328 output_file_path = input ("Please enter path of the output file: " )
307329 key = int (input ("Please enter off-set: " ).strip ())
308- alphabet_input = input ("Enter custom alphabet (press Enter to use default): " ).strip () or None
330+ alphabet_input = (
331+ input ("Enter custom alphabet (press Enter to use default): " ).strip ()
332+ or None
333+ )
309334 decrypt_file (input_file_path , output_file_path , key , alphabet_input )
310335
311336 elif choice == "5" :
0 commit comments