I did not bother to name this...

From Abrupt Macaque, 1 Month ago, written in Plain Text, viewed 17 times.
URL http://codebin.org/view/333100a8 Embed
Download Paste or View Raw
  1. from cs50 import get_string
  2. import sys
  3.  
  4. def validate(k):
  5.     if len(sys.argv) != 2:
  6.         return False
  7.     else:
  8.         for ch in k:
  9.             if not ch.isalpha:
  10.                 return False
  11.             else:
  12.                 return True
  13.  
  14. def main():
  15.     k = sys.argv[1]
  16.     if validate(k) == False:
  17.         print("Usage: ./vigenere k")
  18.         return 1;
  19.  
  20.     plaintext = get_string("plaintext: ")
  21.     print("ciphertext: ", end="")
  22.     j = 0
  23.  
  24.     for ch in plaintext:
  25.         if not ch.isalpha:
  26.             print(ch, end="")
  27.             continue
  28.  
  29.         ascii_offset = 65 if ch.isupper() else 97
  30.  
  31.         plaintemp = ord(ch) - ascii_offset
  32.         keytemp = ord(k[j % len(k)].upper()) - 65
  33.         crypt = (plaintemp + keytemp) % 26
  34.         j =+ 1
  35.  
  36.         print(chr(crypt + ascii_offset), end="")
  37.     print()
  38.     return 0
  39.  
  40.  
  41. if __name__ == "__main__":
  42.     main()

Reply to "I did not bother to name this..."

Here you can reply to the paste above