From d1ca27170642199c3d6eb6693a4bb20d95ed6149 Mon Sep 17 00:00:00 2001 From: Aksh Desai Date: Sun, 2 Jun 2024 02:06:36 +0530 Subject: [PATCH] Stopped asking key for brutcorse 17.Caesar Cipher --- helper/handles.py | 5 +---- utils/caesar_cipher.py | 12 ++++++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/helper/handles.py b/helper/handles.py index 244ca70..b2466f9 100644 --- a/helper/handles.py +++ b/helper/handles.py @@ -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: diff --git a/utils/caesar_cipher.py b/utils/caesar_cipher.py index 989d654..5daaa9c 100644 --- a/utils/caesar_cipher.py +++ b/utils/caesar_cipher.py @@ -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}") @@ -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 = ""