Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Filename: circuit_caesar.py
- # Author: Jeoi Reqi
- # Circuit Python implementation of a Caesar cipher
- SYM = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
- MAX_KEY_SIZE = len(SYM)
- def gMode():
- while True:
- print('Type "e" to encrypt or "d" to decrypt:')
- mode = input().lower()
- if mode in ['e', 'd']:
- return mode
- else:
- print('ERROR: Invalid mode')
- def gMessage():
- print('Type a message:')
- return input()
- def gKey():
- while True:
- print('Shift strength (1-%s):' % (MAX_KEY_SIZE))
- key = int(input())
- if 1 <= key <= MAX_KEY_SIZE:
- return key
- else:
- print('ERROR: Invalid key')
- def gTranslated(mode, message, key):
- if mode == 'd':
- key = -key
- translated = ''
- for symbol in message:
- if symbol.isalpha():
- 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]
- else:
- # Non-letter character, just append to translated message.
- translated += symbol
- return translated
- # Metadata
- __file_name__ = 'circuit_caesar.py'
- __author__ = 'Jeoi Reqi'
- __copyright__ = 'Copyright 2018, Jeoi Reqi'
- __credits__ = 'Jeoi Reqi'
- __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__ = 'Jeoi Reqi'
- __email__ = 'jeoireqi@gmail.com'
- print('\n::%s is licensed under the Creative Commons Attribution-NonCommercial 4.0 International License::\n\n'
- 'File Name: %s\nAuthor: %s\nCopyright: %s\nCredits: %s\nDCT URL: %s\nLicense: %s\nRelease URL: %s\n'
- 'Version: %s\nMaintainer: %s\nEmail: %s\n'
- % (__file_name__, __file_name__, __author__, __copyright__,
- __credits__, __dct_url__, __license__, __rel_url__, __version__,
- __maintainer__, __email__))
- mode = gMode()
- message = gMessage()
- key = gKey()
- print('\nTranslated:', gTranslated(mode, message, key), '\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement