Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cs50
- import sys
- if len(sys.argv) == 1 or len(sys.argv) > 2:
- print("Usage: python vigenere.py <key>")
- exit(1)
- key = sys.argv[1]
- for i in range(len(key)):
- if key[i].isalpha() is not True:
- print("Keyword must only contain letters A-Z or a-z")
- exit(2)
- print("plaintext: ", end="")
- plaintext = cs50.get_string()
- plaintext_size = len(plaintext)
- key_size = len(key)
- ciphertext = []
- j = 0
- for i in range(plaintext_size):
- if j >= key_size:
- j = 0
- if plaintext[i].isalpha():
- k = ord(key[j].upper()) % 65
- c = (ord(plaintext[i]) + k)
- if c >= 0 or c <= 25:
- c %= 26
- ciphertext.append(chr(c))
- j += 1
- else:
- c = ord(plaintext[i])
- ciphertext.append(chr(c))
- print("ciphertext: ", end="")
- print("".join(ciphertext))
- exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement