From 3af0f7dc7298b0fa5903a414d015c737c64a98b3 Mon Sep 17 00:00:00 2001 From: Raine Date: Fri, 6 Oct 2023 18:08:07 +0200 Subject: [PATCH] fix: typo --- cipher.py | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 cipher.py diff --git a/cipher.py b/cipher.py new file mode 100644 index 0000000..ecaeb7a --- /dev/null +++ b/cipher.py @@ -0,0 +1,110 @@ +import argparse + +def encode_shift_cipher(text, key): + result = "" + alphabet = 'abcdefghijklmnopqrstuvwxyz' + shift_direction = 1 + shift_count = 0 + + for char in text: + if char.isalpha(): + shift = int(key[shift_count]) * shift_direction + index = alphabet.index(char.lower()) + + # Calculate the new index and wrap around if it's less than 0 + new_index = (index + shift) % 26 + if new_index < 0: + new_index += 26 # Wrap around to 'z' + + result += alphabet[new_index] + + shift_direction *= -1 + shift_count = (shift_count + 1) % len(key) + else: + result += char + + return result + +def decode_shift_cipher(text, key): + result = "" + alphabet = 'abcdefghijklmnopqrstuvwxyz' + shift_direction = 1 + shift_count = 0 + + for char in text: + if char.isalpha(): + shift = int(key[shift_count]) * shift_direction + index = alphabet.index(char.lower()) + + # Calculate the new index and wrap around if it's less than 0 + new_index = (index - shift) % 26 + if new_index < 0: + new_index += 26 # Wrap around to 'z' + + result += alphabet[new_index] + + shift_direction *= -1 + shift_count = (shift_count + 1) % len(key) + else: + result += char + + return result + + +def main(): + parser = argparse.ArgumentParser(description="Shift Cipher Encoder/Decoder") + parser.add_argument("-e", "--encode", action="store_true", help="Encode the input") + parser.add_argument("-d", "--decode", action="store_true", help="Decode the input") + parser.add_argument("-k", "--key", required=True, help="Encryption/Decryption key (e.g., '1234')") + parser.add_argument("-i", "--input", help="Input file to process") + parser.add_argument("-o", "--output", help="Output file (optional)") + parser.add_argument("-s", "--string", help="Input string to process") + + args = parser.parse_args() + + if not args.encode and not args.decode: + print("Please specify whether to encode or decode using -e or -d.") + return + + if args.encode and args.decode: + print("Please choose either -e (encode) or -d (decode), not both.") + return + + if args.input and args.string: + print("Please choose either -i (input file) or -s (input string), not both.") + return + + if args.input: + # Read input from a file + try: + with open(args.input, "r") as file: + input_text = file.read() + except FileNotFoundError: + print(f"Error: Input file '{args.input}' not found.") + return + elif args.string: + # Use the input string + input_text = args.string + else: + # Read input from the command line + input_text = input("Enter the text to encode/decode: ") + + key = args.key + + if args.encode: + output_text = encode_shift_cipher(input_text, key) + else: + output_text = decode_shift_cipher(input_text, key) + + if args.output: + # Write the result to an output file + with open(args.output, "w") as file: + file.write(output_text) + print(f"Output written to '{args.output}'") + else: + # Print the result to the console + print("Result:") + print(output_text) + +if __name__ == "__main__": + main()