Advertisement
Guest User

vigenere.py

a guest
Feb 20th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. from cs50 import get_string
  2. from sys import argv
  3.  
  4. # Correct usage :)
  5. if len(argv) == 2:
  6.  
  7.     # Length of key and Variable to track our key
  8.     key_len = 0
  9.     key_index = 0
  10.  
  11.     # Check if argv[1] is alphabetic
  12.     for c in argv[1]:
  13.         # Non-alphabetic
  14.         if c.isalpha() == False:
  15.             print("Key should be alphabetic")
  16.             sys.exit(2)
  17.         # Increment key count
  18.         else:
  19.             key_len+=1
  20.  
  21.  
  22.     # Gets plain text
  23.     plain_text = get_string("plaintext: ")
  24.  
  25.     print("ciphertext: ", end="")
  26.  
  27.     # Iterate through each char of plain_text.
  28.     for c in plain_text:
  29.         if c.isalpha():
  30.             # Uppercase
  31.             if c.isupper():
  32.                 print(chr(((ord(c) - 65) + ord((argv[1][key_index % key_len]).upper()-65))%26+65), end="")
  33.                 key_index+=1
  34.             # Lowercase
  35.             else:
  36.                 print(chr(((ord(c) - 97) + ord((argv[1][key_index % key_len]).upper()-97))%26+97), end="")
  37.                 key_index+=1
  38.  
  39.         # Non-alphabetic
  40.         else:
  41.             print(c, end="")
  42.  
  43.  
  44.  
  45. # Incorrect usage :(
  46. else:
  47.     print("Usage: python vigenere.py key")
  48.     sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement