Advertisement
Python253

circuit_caesar

Mar 1st, 2024
707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Filename: circuit_caesar.py
  3. # Author: Jeoi Reqi
  4. # Circuit Python implementation of a Caesar cipher
  5.  
  6. SYM = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  7. MAX_KEY_SIZE = len(SYM)
  8.  
  9. def gMode():
  10.     while True:
  11.         print('Type "e" to encrypt or "d" to decrypt:')
  12.         mode = input().lower()
  13.         if mode in ['e', 'd']:
  14.             return mode
  15.         else:
  16.             print('ERROR: Invalid mode')
  17.  
  18. def gMessage():
  19.     print('Type a message:')
  20.     return input()
  21.  
  22. def gKey():
  23.     while True:
  24.         print('Shift strength (1-%s):' % (MAX_KEY_SIZE))
  25.         key = int(input())
  26.         if 1 <= key <= MAX_KEY_SIZE:
  27.             return key
  28.         else:
  29.             print('ERROR: Invalid key')
  30.  
  31. def gTranslated(mode, message, key):
  32.     if mode == 'd':
  33.         key = -key
  34.     translated = ''
  35.     for symbol in message:
  36.         if symbol.isalpha():
  37.             symbolIndex = SYM.find(symbol)
  38.             if symbolIndex == -1:
  39.                 # Symbol not within MAX_KEY_SIZE parameters.
  40.                 translated += symbol
  41.             else:
  42.                 # Encryption or Decryption Magic!
  43.                 symbolIndex += key
  44.                 if symbolIndex >= len(SYM):
  45.                     symbolIndex -= len(SYM)
  46.                 elif symbolIndex < 0:
  47.                     symbolIndex += len(SYM)
  48.                 translated += SYM[symbolIndex]
  49.         else:
  50.             # Non-letter character, just append to translated message.
  51.             translated += symbol
  52.     return translated
  53.  
  54. # Metadata
  55. __file_name__ = 'circuit_caesar.py'
  56. __author__ = 'Jeoi Reqi'
  57. __copyright__ = 'Copyright 2018, Jeoi Reqi'
  58. __credits__ = 'Jeoi Reqi'
  59. __dct_url__ = 'http://purl.org/dc/terms/'
  60. __license__ = 'Creative Commons'
  61. __rel_url__ = 'http://creativecommons.org/licenses/by-nc/4.0/'
  62. __version__ = 'CC-by-nc/4.0/'
  63. __maintainer__ = 'Jeoi Reqi'
  64. __email__ = 'jeoireqi@gmail.com'
  65.  
  66. print('\n::%s is licensed under the Creative Commons Attribution-NonCommercial 4.0 International License::\n\n'
  67.       'File Name: %s\nAuthor: %s\nCopyright: %s\nCredits: %s\nDCT URL: %s\nLicense: %s\nRelease URL: %s\n'
  68.       'Version: %s\nMaintainer: %s\nEmail: %s\n'
  69.       % (__file_name__, __file_name__, __author__, __copyright__,
  70.          __credits__, __dct_url__, __license__, __rel_url__, __version__,
  71.          __maintainer__, __email__))
  72.  
  73. mode = gMode()
  74. message = gMessage()
  75. key = gKey()
  76.  
  77. print('\nTranslated:', gTranslated(mode, message, key), '\n')
  78.  
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement