Skip to content

Commit 71ee3da

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 3d92601 commit 71ee3da

1 file changed

Lines changed: 45 additions & 21 deletions

File tree

ciphers/caesar_cipher.py

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import pyfiglet
55

6+
67
def encrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
78
"""
89
encrypt
@@ -73,38 +74,39 @@ def encrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
7374

7475
# Create a shifted version of the alphabet by the key
7576
# This rotated alphabet will be used for mapping original characters to encrypted characters
76-
shifted = alpha[key % len(alpha):] + alpha[:key % len(alpha)]
77+
shifted = alpha[key % len(alpha) :] + alpha[: key % len(alpha)]
7778

7879
# Create a translation table: original alphabet -> shifted alphabet
7980
table = str.maketrans(alpha, shifted)
80-
81+
8182
# Apply the translation table to the input string
8283
# Characters not in the alphabet remain unchanged
8384
return input_string.translate(table)
8485

85-
def encrypt_file(input_path: str, output_path: str, key: int, alphabet: str | None = None):
8686

87+
def encrypt_file(
88+
input_path: str, output_path: str, key: int, alphabet: str | None = None
89+
):
8790
"""
8891
Encrypts a text file line by line using Caesar Cipher and writes
8992
the encrypted content to the output file.
9093
"""
91-
94+
9295
# Use the provided alphabet if given; otherwise default to ascii_letters (a-z + A-Z)
9396
alpha = alphabet or ascii_letters
9497

9598
# Open input file for reading and output file for writing
96-
with open(input_path, 'r') as fin, open(output_path, 'w') as fout:
97-
99+
with open(input_path, "r") as fin, open(output_path, "w") as fout:
98100
# Read the input file line by line to avoid loading the entire file into memory
99101
for line in fin:
100-
101102
# Encrypt the current line using the encrypt function
102103
encrypted_line = encrypt(line, key, alpha)
103104

104105
# Write the encrypted line to the output file
105106
fout.write(encrypted_line)
106107
print("File has been successfully been encrypted !!")
107108

109+
108110
def decrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
109111
"""
110112
decrypt
@@ -176,30 +178,30 @@ def decrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
176178

177179
return encrypt(input_string, key, alphabet)
178180

179-
def decrypt_file(input_path: str, output_path: str, key: int, alphabet: str | None = None):
180181

182+
def decrypt_file(
183+
input_path: str, output_path: str, key: int, alphabet: str | None = None
184+
):
181185
"""
182186
Decrypts a text file line by line using Caesar Cipher and writes
183187
the decrypted content to the output file.
184188
"""
185-
189+
186190
# Use the provided alphabet if given; otherwise default to ascii_letters (a-z + A-Z)
187191
alpha = alphabet or ascii_letters
188192

189193
# Open input file for reading and output file for writing
190-
with open(input_path, 'r') as fin, open(output_path, 'w') as fout:
191-
194+
with open(input_path, "r") as fin, open(output_path, "w") as fout:
192195
# Read the input file line by line to avoid loading the entire file into memory
193196
for line in fin:
194-
195197
# Encrypt the current line using the encrypt function
196198
decrypted_line = decrypt(line, key, alpha)
197199

198200
# Write the encrypted line to the output file
199201
fout.write(decrypted_line)
200202

201203
print("File has been successfully been decrypted !!")
202-
204+
203205

204206
def brute_force(input_string: str, alphabet: str | None = None) -> dict[int, str]:
205207
"""
@@ -266,12 +268,22 @@ def brute_force(input_string: str, alphabet: str | None = None) -> dict[int, str
266268

267269
if __name__ == "__main__":
268270
while True:
269-
os.system('cls' if os.name == 'nt' else 'clear')
271+
os.system("cls" if os.name == "nt" else "clear")
270272
banner = pyfiglet.figlet_format("Caesar Ciphar", font="big")
271273
print(banner)
272274
print(f"\n{'-' * 10}\n Menu\n{'-' * 10}")
273275
print("Please select from the following options: ")
274-
print(*["1.Encrypt", "2.Encrypt a File", "3.Decrypt", "4.Decrypt a File", "5.BruteForce", "6.Quit", ], sep="\n")
276+
print(
277+
*[
278+
"1.Encrypt",
279+
"2.Encrypt a File",
280+
"3.Decrypt",
281+
"4.Decrypt a File",
282+
"5.BruteForce",
283+
"6.Quit",
284+
],
285+
sep="\n",
286+
)
275287

276288
# get user input
277289
choice = input("\nWhat would you like to do?: ").strip() or "4"
@@ -282,28 +294,40 @@ def brute_force(input_string: str, alphabet: str | None = None) -> dict[int, str
282294
elif choice == "1":
283295
input_string = input("Please enter the string to be encrypted: ")
284296
key = int(input("Please enter off-set: ").strip())
285-
alphabet_input = input("Enter custom alphabet (press Enter to use default): ").strip() or None
297+
alphabet_input = (
298+
input("Enter custom alphabet (press Enter to use default): ").strip()
299+
or None
300+
)
286301
print(encrypt(input_string, key, alphabet_input))
287-
302+
288303
elif choice == "2":
289304
input_file_path = input("Please enter path of the input file: ")
290305
output_file_path = input("Please enter path of the output file: ")
291306
key = int(input("Please enter off-set: ").strip())
292-
alphabet_input = input("Enter custom alphabet (press Enter to use default): ").strip() or None
307+
alphabet_input = (
308+
input("Enter custom alphabet (press Enter to use default): ").strip()
309+
or None
310+
)
293311
encrypt_file(input_file_path, output_file_path, key, alphabet_input)
294-
312+
295313
elif choice == "3":
296314
input_string = input("Please enter the string to be decrypted: ")
297315
key = int(input("Please enter off-set: ").strip())
298-
alphabet_input = input("Enter custom alphabet (press Enter to use default): ").strip() or None
316+
alphabet_input = (
317+
input("Enter custom alphabet (press Enter to use default): ").strip()
318+
or None
319+
)
299320

300321
print(decrypt(input_string, key, alphabet_input))
301322

302323
elif choice == "4":
303324
input_file_path = input("Please enter path of the input file: ")
304325
output_file_path = input("Please enter path of the output file: ")
305326
key = int(input("Please enter off-set: ").strip())
306-
alphabet_input = input("Enter custom alphabet (press Enter to use default): ").strip() or None
327+
alphabet_input = (
328+
input("Enter custom alphabet (press Enter to use default): ").strip()
329+
or None
330+
)
307331
decrypt_file(input_file_path, output_file_path, key, alphabet_input)
308332

309333
elif choice == "5":

0 commit comments

Comments
 (0)