Advertisement
v1s

Vigenère Cipher encryptor/decryptor [AI GENERATED]

v1s
Jun 30th, 2023
1,599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. """
  2. vigenere.py: Partly generated with ChatGPT as part of the PyCodex project: https://github.com/fresh466/pycodex
  3. """
  4.  
  5. import sys
  6.  
  7. def vig_enc(plaintext, key):
  8.     ciphertext = ""
  9.     key_length = len(key)
  10.     key = key.upper()
  11.  
  12.     for i in range(len(plaintext)):
  13.         char = plaintext[i]
  14.         if char.isalpha():
  15.             shift = ord(key[i % key_length]) - ord('A')
  16.             if char.isupper():
  17.                 ciphertext += chr((ord(char) - ord('A') + shift) % 26 + ord('A'))
  18.             else:
  19.                 ciphertext += chr((ord(char) - ord('a') + shift) % 26 + ord('a'))
  20.         else:
  21.             ciphertext += char
  22.  
  23.     return ciphertext
  24.  
  25.  
  26. def vig_dec(ciphertext, key):
  27.     plaintext = ""
  28.     key_length = len(key)
  29.     key = key.upper()
  30.  
  31.     for i in range(len(ciphertext)):
  32.         char = ciphertext[i]
  33.         if char.isalpha():
  34.             shift = ord(key[i % key_length]) - ord('A')
  35.             if char.isupper():
  36.                 plaintext += chr((ord(char) - ord('A') - shift) % 26 + ord('A'))
  37.             else:
  38.                 plaintext += chr((ord(char) - ord('a') - shift) % 26 + ord('a'))
  39.         else:
  40.             plaintext += char
  41.  
  42.     return plaintext
  43.  
  44.  
  45. def main(argc: int, argv: list[str]):
  46.     if argc != 4:
  47.         print("Usage: vigenere.py <-e <plaintext>| -d <ciphertext>> <key>")
  48.         return 1
  49.    
  50.     key = argv[3]
  51.     if argv[1] == "-e":
  52.         plaintext = argv[2]
  53.         enc_text = vig_enc(plaintext, key)
  54.         print("Encrypted text:", enc_text)
  55.     elif argv[1] == "-d":
  56.         ciphertext = argv[2]
  57.         dec_text = vig_dec(ciphertext, key)
  58.         print("Decrypted text:", dec_text)
  59.  
  60.     return 0
  61.  
  62.  
  63. if __name__ == "__main__":
  64.     status = main(len(sys.argv), sys.argv)
  65.     sys.exit(status)
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement