Advertisement
Guest User

...

a guest
Jan 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. from sys import argv
  2.  
  3. def main():
  4.     # check for argument
  5.     if len(argv) != 2:
  6.         print("Usage: python3.6", argv[0], "keyword")
  7.         return 1
  8.  
  9.     keyword = argv[1]
  10.  
  11.     # check if it's alphabetical
  12.     if not isAlphaWord(keyword):
  13.         print("Only alphabetical keywords are allowed")
  14.         return 1
  15.  
  16.     plaintext = input("plaintext:  ")
  17.     ciphertext = ""
  18.  
  19.     j = 0
  20.  
  21.     for c in plaintext:
  22.         asciiIndex = ord(c)
  23.         if asciiIndex >= 65 and asciiIndex <= 90:
  24.             startIndex = 65
  25.         elif asciiIndex >= 97 and asciiIndex <= 122:
  26.             startIndex = 97
  27.         else:
  28.             s = c
  29.             ciphertext += s
  30.             continue
  31.  
  32.         k = getCharAlphaIndex(keyword[j])
  33.         j += 1
  34.         j = j % len(keyword)
  35.  
  36.         alphaIndex = asciiIndex - startIndex
  37.         s = chr((alphaIndex + k) % 26 + startIndex)
  38.         ciphertext += s
  39.  
  40.         print("ciphertext:" + {ciphertext})
  41.        
  42.         return 0
  43.        
  44.  
  45. def isAlphaWord(word):
  46.     for c in word:
  47.         asciiIndex = ord(c)
  48.         if asciiIndex >= 65 and asciiIndex <= 90:
  49.             continue
  50.         elif asciiIndex >= 97 and asciiIndex <= 122:
  51.             continue
  52.         else:
  53.             return False
  54.     return True
  55.  
  56. def getCharAlphaIndex(ch):
  57.     chAsciiIndex = ord(ch)
  58.     if chAsciiIndex >= 65 and chAsciiIndex <= 90:
  59.         chStartIndex = 65
  60.     elif chAsciiIndex >= 97 and chAsciiIndex <= 122:
  61.         chStartIndex = 97
  62.  
  63.     chAlphaIndex = chAsciiIndex - chStartIndex
  64.     return chAlphaIndex
  65.  
  66.  
  67. if __name__ == "_main_":
  68.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement