Advertisement
DragonOsman

vigenere.py

Feb 22nd, 2017
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. import cs50
  2. import sys
  3.  
  4. if len(sys.argv) == 1 or len(sys.argv) > 2:
  5.     print("Usage: python vigenere.py <key>")
  6.     exit(1)
  7.  
  8. key = sys.argv[1]
  9. for i in range(len(key)):
  10.     if key[i].isalpha() is not True:
  11.         print("Keyword must only contain letters A-Z or a-z")
  12.         exit(2)
  13.  
  14. print("plaintext: ", end="")
  15. plaintext = cs50.get_string()
  16. plaintext_size = len(plaintext)
  17. key_size = len(key)
  18.  
  19. ciphertext = []
  20. j = 0
  21. for i in range(plaintext_size):
  22.     if j >= key_size:
  23.         j = 0
  24.     if plaintext[i].isalpha():
  25.         k = ord(key[j].upper()) % 65
  26.        
  27.         c = (ord(plaintext[i]) + k)
  28.         if c >= 0 or c <= 25:
  29.             c %= 26
  30.         ciphertext.append(chr(c))
  31.         j += 1
  32.            
  33.     else:
  34.         c = ord(plaintext[i])
  35.         ciphertext.append(chr(c))
  36.  
  37. print("ciphertext: ", end="")
  38. print("".join(ciphertext))
  39. exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement