fix: typo
This commit is contained in:
parent
c163c57a35
commit
654782592c
1 changed files with 0 additions and 110 deletions
110
cypher.py
110
cypher.py
|
@ -1,110 +0,0 @@
|
|||
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()
|
Loading…
Reference in a new issue