Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from cs50 import get_string
- from sys import argv
- # Correct usage :)
- if len(argv) == 2:
- # Length of key and Variable to track our key
- key_len = 0
- key_index = 0
- # Check if argv[1] is alphabetic
- for c in argv[1]:
- # Non-alphabetic
- if c.isalpha() == False:
- print("Key should be alphabetic")
- sys.exit(2)
- # Increment key count
- else:
- key_len+=1
- # Gets plain text
- plain_text = get_string("plaintext: ")
- print("ciphertext: ", end="")
- # Iterate through each char of plain_text.
- for c in plain_text:
- if c.isalpha():
- # Uppercase
- if c.isupper():
- print(chr(((ord(c) - 65) + ord((argv[1][key_index % key_len]).upper()-65))%26+65), end="")
- key_index+=1
- # Lowercase
- else:
- print(chr(((ord(c) - 97) + ord((argv[1][key_index % key_len]).upper()-97))%26+97), end="")
- key_index+=1
- # Non-alphabetic
- else:
- print(c, end="")
- # Incorrect usage :(
- else:
- print("Usage: python vigenere.py key")
- sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement