commit 03b4771c43064fc1bb1c9cf75ac3e73eab3c30e8 Author: Raine Date: Fri Oct 6 17:52:09 2023 +0200 init: initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2211df6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.txt diff --git a/README.md b/README.md new file mode 100644 index 0000000..21d2b43 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Logical Alternating Shift Cypher + +Made to talk with a friend without eavedroppers. + +## Usage +The tool uses a numerical key to shift the message's characters. +Only alphabetical characters are shifted. diff --git a/cypher.py b/cypher.py new file mode 100644 index 0000000..ecaeb7a --- /dev/null +++ b/cypher.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()