Advertisement
Guest User

There is no spoon

a guest
Dec 14th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import string
  4. import sys
  5.  
  6. abc = string.ascii_lowercase
  7. one_time_pad = list(abc)
  8. help = "No spoon"
  9.  
  10.  
  11. def encrypt(msg, key):
  12.     ciphertext = ''
  13.     for idx, char in enumerate(msg):
  14.         charIdx = abc.index(char)
  15.         keyIdx = one_time_pad.index(key[idx])
  16.  
  17.         cipher = (keyIdx + charIdx) % len(one_time_pad)
  18.         ciphertext += abc[cipher]
  19.  
  20.     return ciphertext
  21.  
  22. def decrypt(ciphertext, key):
  23.     if ciphertext == '' or key == '':
  24.         return ''
  25.  
  26.     charIdx = abc.index(ciphertext[0])
  27.     keyIdx = one_time_pad.index(key[0])
  28.  
  29.     cipher = (charIdx - keyIdx) % len(one_time_pad)
  30.     char = abc[cipher]
  31.  
  32.     return char + decrypt(ciphertext[1:], key[1:])
  33.  
  34. if __name__ == '__main__':
  35.     availableOpt = ["-d", "-e"]
  36.     if len(sys.argv) == 1 or sys.argv[1] not in availableOpt:
  37.         print(help)
  38.         exit(0)
  39.  
  40.     key = input("Key: ")
  41.     msg = input("Message: ")
  42.  
  43.     if sys.argv[1] == availableOpt[1]:
  44.         print(encrypt(msg, key))
  45.     elif sys.argv[1] == availableOpt[0]:
  46.         print(decrypt(msg, key))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement