Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stopped asking key for brut force in 17. Caesar Cipher #22

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions helper/handles.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,8 @@ def handle_caesar_cipher() -> None:
Handles the Caesar Cipher util.
"""
message = input("Enter a text to cipher/decipher : \t")
shift = int(input("Enter a number of shifts (0 to 25) : \t"))
if shift < 0 or shift > 25:
printer.error("Invalid shift number, please choose a number between 0 and 25..!")
mode = str(input("Enter a mode (encrypt/decrypt/bruteforce) : \t"))
caesar_cipher.CaesarCipher(message, shift, mode)
caesar_cipher.CaesarCipher(message, mode)


def handle_basexx() -> None:
Expand Down
12 changes: 10 additions & 2 deletions utils/caesar_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ class CaesarCipher:
:param shift: The shift to use for the encryption or decryption.
:param mode: The mode to use for the encryption or decryption.
"""
def __init__(self, text: str, shift: int, mode: str):
def __init__(self, text: str, mode: str):
self.text = text
self.shift = shift
self.mode = mode

if self.mode in ("encrypt", "e"):
self.shift = self.get_key()
printer.info(f"Encrypting '{self.text}'...")
encrypted_text = self.caesar_encrypt(self.text, self.shift)
printer.success(f"'{self.text}' in Caesar's code : {encrypted_text}")
elif self.mode in ("decrypt", "d"):
self.shift = self.get_key()
printer.info(f"Decrypting '{self.text}'...")
decrypted_text = self.caesar_decrypt(self.text, self.shift)
printer.success(f"'{self.text}' in plain text : {decrypted_text}")
Expand All @@ -45,6 +46,13 @@ def __init__(self, text: str, shift: int, mode: str):
else:
printer.error("Invalid mode, please choose either 'encrypt' , 'decrypt' or 'bruteforce'..!")

@staticmethod
def get_key():
shift = int(input("Enter a number of shifts (0 to 25) : \t"))
if shift < 0 or shift > 25:
printer.error("Invalid shift number, please choose a number between 0 and 25..!")
return shift

@staticmethod
def caesar_encrypt(text, shift):
encrypted_text = ""
Expand Down
Loading