Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # # -*- coding: utf-8 -*-
- # cypher.py is a simple script that allows
- # for user input and gives a choice of shifting
- # the symbols for encryption and/or decryption.
- __file_name__ = 'cipher.py'
- __author__ = 'Dan Evans'
- __copyright__ = 'Copyright 2017©, Coding With Py'
- __credits__ = 'Dan Evans'
- __dct_url__ = 'http://purl.org/dc/terms/'
- __license__ = 'Creative Commons'
- __rel_url__ = 'http://creativecommons.org/licenses/by-nc/4.0/'
- __version__ = 'CC-by-nc/4.0/'
- __maintainer__ = 'Dan(Python253)Evans'
- __email__ = 'Python253@gmail.com'
- print('\n','','cipher.py is licensed under',
- 'The Creative Commons Attribution-NonCommercial 4.0 International License',
- '\n'*2,'File Name: '+__file_name__,'\n','Author: '+__author__,
- '\n','Copyright: '+__copyright__,'\n','Credits: '+__credits__,
- '\n','DCT URL: ','\n',__dct_url__,'\n','License: '+__license__,
- '\n','Release URL: ','\n',__rel_url__,'\n','Version: '+__version__,
- '\n','Maintainer: '+__maintainer__,'\n','Email: '+__email__,'\n')
- SYM = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
- MAX_KEY_SIZE = len(SYM)
- def gMode():
- while True:
- print('\n','Type "e" to encrypt or... Type "d" to decrypt:','\n')
- mode = input().lower()
- if mode in ['e','E','d','D']:
- return mode
- else:
- print('ERROR!:','\n'+'Invalid Symbol or Outside Perameters','\n')
- def gMessage():
- print('Type a message:')
- return input()
- def gKey():
- key = 0
- while True:
- print('Shift Strength(1-%s)' % (MAX_KEY_SIZE))
- key = int(input())
- if (key >= 1 and key <= MAX_KEY_SIZE):
- return key
- def gTranslated(mode, message, key):
- if mode[0] == 'd':
- key = -key
- translated = ''
- for symbol in message:
- symbolIndex = SYM.find(symbol)
- if symbolIndex == -1:
- # Symbol not within MAX_KEY_SIZE parameters.
- translated += symbol
- else:
- # Encryption or Decryption Magic!
- symbolIndex += key
- if symbolIndex >= len(SYM):
- symbolIndex -= len(SYM)
- elif symbolIndex < 0:
- symbolIndex += len(SYM)
- translated += SYM[symbolIndex]
- return translated
- mode = gMode()
- message = gMessage()
- key = gKey()
- print('\n','Translated:', gTranslated(mode, message, key),'\n')
- #end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement