I have written a simple Caesar Cipher in Python in my efforts to learn a language that isn't C. It works much like any other Caesar implementation and takes various command line arguments to specify options such as the spacing of the output, whether to encrypt or decrypt the input, and whether or not to try and bruteforce the cipher.
This is my first ever Python program so I'm primarily looking to learn some best practices for the language as well as remove any inefficiencies or redundancies in my code.
Thanks!
import argparse
import re
def caesar(string, shift):
output = ''
for char in string:
if char.isspace():
output += ' '
else:
output += chr(((ord(char) - 65 + shift) % 26) + 65)
return output
def main():
parser = argparse.ArgumentParser(description = "Encrypt or decrpyt a string using the Caesar Cipher")
parser.add_argument("--bruteforce", "--brute", "-b", help = "bruteforce mode", action = "store_true")
parser.add_argument("--encrypt", "--enc", "-e", help = "encryption mode (default)", action = "store_true")
parser.add_argument("--decrpyt", "--dec", "-d", help = "decryption mode", action = "store_true")
parser.add_argument("--no-spacing", "--no-space", "-n", help = "no spacing in output (default)", action = "store_true")
parser.add_argument("--preserve-spacing", "--preserve", "-p", help = "use same spacing as original string", action = "store_true")
parser.add_argument("--shift", "-s", help = "value for the Caesar shift", type = int, choices = range(1, 26))
parser.add_argument("--spacing", "-x", help = "specify the spacing in output", type = int)
args = parser.parse_args()
if args.bruteforce:
bruteforce = True
else:
bruteforce = False
shift = args.shift
if args.decrpyt:
shift = -shift
if args.preserve_spacing:
regex = re.compile('[^A-Z\s]')
else:
regex = re.compile('[^A-Z]')
string = regex.sub('', input().upper())
if args.spacing:
string = ' '.join([string[i:i + args.spacing] for i in range(0, len(string), args.spacing)])
if bruteforce:
for shift in range(1, 26):
print("%d: %s" %(shift, caesar(string, -shift)))
else:
print(caesar(string, shift))
if __name__ == "__main__":
main()