Advertisement
Python253

Cypher.py

Mar 6th, 2018
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.39 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # # -*- coding: utf-8 -*-
  3. # cypher.py is a simple script that allows
  4. # for user input and gives a choice of shifting
  5. # the symbols for encryption and/or decryption.
  6.  
  7. __file_name__ = 'cipher.py'
  8. __author__ = 'Dan Evans'
  9. __copyright__ = 'Copyright 2017©, Coding With Py'
  10. __credits__ = 'Dan Evans'
  11. __dct_url__ = 'http://purl.org/dc/terms/'
  12. __license__ = 'Creative Commons'
  13. __rel_url__ = 'http://creativecommons.org/licenses/by-nc/4.0/'
  14. __version__ = 'CC-by-nc/4.0/'
  15. __maintainer__ = 'Dan(Python253)Evans'
  16. __email__ = 'Python253@gmail.com'
  17.  
  18. print('\n','','cipher.py is licensed under',
  19.     'The Creative Commons Attribution-NonCommercial 4.0 International License',
  20.     '\n'*2,'File Name: '+__file_name__,'\n','Author: '+__author__,
  21.     '\n','Copyright: '+__copyright__,'\n','Credits: '+__credits__,
  22.     '\n','DCT URL: ','\n',__dct_url__,'\n','License: '+__license__,
  23.     '\n','Release URL: ','\n',__rel_url__,'\n','Version: '+__version__,
  24.     '\n','Maintainer: '+__maintainer__,'\n','Email: '+__email__,'\n')
  25.  
  26. SYM = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  27. MAX_KEY_SIZE = len(SYM)
  28.  
  29. def gMode():
  30.     while True:
  31.         print('\n','Type "e" to encrypt or... Type "d" to decrypt:','\n')
  32.         mode = input().lower()
  33.         if mode in ['e','E','d','D']:
  34.             return mode
  35.         else:
  36.             print('ERROR!:','\n'+'Invalid Symbol or Outside Perameters','\n')
  37.            
  38. def gMessage():
  39.     print('Type a message:')
  40.     return input()
  41.    
  42. def gKey():
  43.     key = 0
  44.     while True:
  45.         print('Shift Strength(1-%s)' % (MAX_KEY_SIZE))
  46.         key = int(input())
  47.         if (key >= 1 and key <= MAX_KEY_SIZE):
  48.             return key
  49.            
  50. def gTranslated(mode, message, key):
  51.     if mode[0] == 'd':
  52.         key = -key
  53.     translated = ''
  54.     for symbol in message:
  55.         symbolIndex = SYM.find(symbol)
  56.         if symbolIndex == -1:
  57.         # Symbol not within MAX_KEY_SIZE parameters.
  58.             translated += symbol
  59.         else:
  60.         # Encryption or Decryption Magic!
  61.             symbolIndex += key
  62.         if symbolIndex >= len(SYM):
  63.             symbolIndex -= len(SYM)
  64.         elif symbolIndex < 0:
  65.             symbolIndex += len(SYM)
  66.         translated += SYM[symbolIndex]
  67.     return translated
  68.    
  69. mode = gMode()
  70. message = gMessage()
  71. key = gKey()
  72.  
  73. print('\n','Translated:', gTranslated(mode, message, key),'\n')
  74.  
  75. #end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement