Advertisement
UF6

2.5 Problem 7

UF6
Jun 27th, 2016
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. import pyperclip
  2.  
  3. alphabet = 'abcdefghijklmnopqrstuvwxyz'
  4.  
  5. def main():
  6.    
  7.     Message = """"though the cold war has passed, decoded soviet cables have intensified the long running debate on
  8.    whether the soviet union employed american citizens as spies"""
  9.     Key = 'venom'
  10.     Mode = 'encrypt'
  11.  
  12.     if Mode == 'encrypt':
  13.         translated = encryptMessage(Key, Message)
  14.     elif Mode == 'decrypt':
  15.         translated = decryptMessage(Key, Message)
  16.  
  17.     print('%sed message:' % (Mode.title()))
  18.     print(translated)
  19.     pyperclip.copy(translated)
  20.     print()
  21.     print('The message has been copied.')
  22.  
  23.  
  24. def encryptMessage(key, message):
  25.     return translateMessage(key, message, 'encrypt')
  26.  
  27.  
  28. def decryptMessage(key, message):
  29.     return translateMessage(key, message, 'decrypt')
  30.  
  31.  
  32. def translateMessage(key, message, mode):
  33.     translated = []
  34.  
  35.     keyIndex = 0
  36.     key = key.upper()
  37.  
  38.     for symbol in message:
  39.         num = alphabet.find(symbol.upper())
  40.         if num != -1:
  41.             if mode == 'encrypt':
  42.                 num += alphabet.find(key[keyIndex])
  43.             elif mode == 'decrypt':
  44.                 num -= alphabet.find(key[keyIndex])
  45.  
  46.             num %= len(alphabet)
  47.  
  48.        
  49.             if symbol.isupper():
  50.                 translated.append(alphabet[num])
  51.             elif symbol.islower():
  52.                 translated.append(alphabet[num].lower())
  53.  
  54.             keyIndex += 1
  55.             if keyIndex == len(key):
  56.                 keyIndex = 0
  57.         else:
  58.             translated.append(symbol)
  59.  
  60.     return ''.join(translated)
  61.  
  62. if __name__ == '__main__':
  63.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement