From 483b4ba7c0a03c22f68fdf9a2e89805510eb2039 Mon Sep 17 00:00:00 2001 From: Officialahmed Date: Thu, 5 Oct 2023 07:32:59 +0400 Subject: [PATCH] FIXED: Caesar decoder possible result --- Caesar Cipher Encoder & Decoder.py | 112 ++++++++++++---------------- 1 file changed, 47 insertions(+), 65 deletions(-) diff --git a/Caesar Cipher Encoder & Decoder.py b/Caesar Cipher Encoder & Decoder.py index 7bd078e2849..63097b39e17 100644 --- a/Caesar Cipher Encoder & Decoder.py +++ b/Caesar Cipher Encoder & Decoder.py @@ -1,85 +1,67 @@ -#PROJECT1 -#CAESAR CIPHER DECODER +# PROJECT1 +# CAESAR CIPHER ENCODER/DECODER -#Author: InTruder -#Cloned from: https://github.com/InTruder-Sec/caesar-cipher +# Author: InTruder +# Cloned from: https://github.com/InTruder-Sec/caesar-cipher +# Improved by: OfficialAhmed (https://github.com/OfficialAhmed) + +def get_int() -> int: + """ + Get integer, otherwise redo + """ + + try: + key = int(input("Enter number of characters you want to shift: ")) + except: + print("Enter an integer") + key = get_int() + + return key def main(): + print("[>] CAESAR CIPHER DECODER!!! \n") print("[1] Encrypt\n[2] Decrypt") - try: - func = int(input("Choose one of the above(example for encode enter 1): ")) - except: - print("\n[>] Invalid input") - exit() - if func == 2: - decode() - else: - if func == 1: + match input("Choose one of the above(example for encode enter 1): "): + + case "1": encode() - else: - print("\n[>] Invalid input") - exit() + + case "2": + decode() + + case _: + print("\n[>] Invalid input. Choose 1 or 2") + main() + def encode(): - text = input("Enter text to encode: ") - key = input("Enter number of characters you want to shift: ") + encoded_cipher = "" - try: - key = int(key) - except: - print("Only intigers between 0 to 25 are allowed. Try again :)") - exit() - if key > 25: - print("Only intigers between 0 to 25 are allowed. Try again :)") - exit() - else: - key = key - text = text.upper() + text = input("Enter text to encode: ") + key = get_int() + for char in text: - ascii = ord(char) - if ascii > 90: - new_ascii = ascii - else: - if ascii < 65: - new_ascii = ascii - else: - new_ascii = ascii + key - if new_ascii > 90: - new_ascii = new_ascii - 26 - else: - new_ascii = new_ascii - encoded = chr(new_ascii) - encoded_cipher = encoded_cipher + encoded - print("Encoded text: " + encoded_cipher) + + ascii = ord(char) + key + encoded_cipher += chr(ascii) + print(f"Encoded text: {encoded_cipher}") def decode(): + + decoded_cipher = "" cipher = input("\n[>] Enter your cipher text: ") - print("Posiblities of cipher text are: \n") - cipher = cipher.lower() - for i in range(1, 26): - decoded = "" - decoded_cipher = "" - for char in cipher: - ascii = ord(char) - if ascii < 97: - new_ascii = ascii - else: - if ascii > 122: - new_ascii = ascii - else: - new_ascii = ascii - int(i) - if new_ascii < 97: - new_ascii = new_ascii + 26 - else: - new_ascii = new_ascii - decoded = chr(new_ascii) - decoded_cipher = decoded_cipher + decoded - print("\n" + decoded_cipher) + key = get_int() + + for character in cipher: + ascii = ord(character) - key + decoded_cipher += chr(ascii) + + print(decoded_cipher) if __name__ == '__main__':