Advertisement
v1s

Caesar Cipher encryptor/decryptor [AI GENERATED]

v1s
Jun 30th, 2023 (edited)
1,255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. """
  2. caesar.py: Partly generated with ChatGPT as part of the PyCodex project: https://github.com/fresh466/pycodex
  3. """
  4.  
  5. import sys
  6.  
  7. def cae_enc(plaintext, shift):
  8.     ciphertext = ""
  9.     for char in plaintext:
  10.         if char.isalpha():
  11.             ascii_offset = ord('A') if char.isupper() else ord('a')
  12.             shifted_char = chr((ord(char) - ascii_offset + shift) % 26 + ascii_offset)
  13.             ciphertext += shifted_char
  14.         else:
  15.             ciphertext += char
  16.     return ciphertext
  17.  
  18. def cae_dec(ciphertext, shift):
  19.     return cae_enc(ciphertext, -shift)
  20.  
  21. def main(argc: int, argv: list[str]):
  22.     if argc != 4:
  23.         print("Usage: caesar.py <-e <plaintext>| -d <ciphertext>> <shift>")
  24.         return 1
  25.    
  26.     shift = int(argv[3])
  27.     if argv[1] == "-e":
  28.         plaintext = argv[2]
  29.         enc_text = cae_enc(plaintext, shift)
  30.         print("Encrypted text:", enc_text)
  31.     elif argv[1] == "-d":
  32.         ciphertext = argv[2]
  33.         dec_text = cae_dec(ciphertext, shift)
  34.         print("Decrypted text:", dec_text)
  35.  
  36.     return 0
  37.  
  38.  
  39. if __name__ == "__main__":
  40.     status = main(len(sys.argv), sys.argv)
  41.     sys.exit(status)
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement