Skip to content

Commit

Permalink
Merge pull request #22 from AkshDesai04/master
Browse files Browse the repository at this point in the history
Stopped asking key for brut force in 17. Caesar Cipher
  • Loading branch information
vil authored Jun 2, 2024
2 parents 9419273 + d1ca271 commit 713a812
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
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

0 comments on commit 713a812

Please sign in to comment.