





import random
import sys

from . import cryptomath_module as cryptomath

_UpperCamelCase       :				Tuple           =			(
    r""" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`"""
    r"""abcdefghijklmnopqrstuvwxyz{|}~"""
)
def  __UpperCamelCase    (						snake_case							,			snake_case							,			snake_case      )    ->		None:


   '''simple docstring'''







   if mode == "encrypt":
      if key_a == 1:
         sys.exit(
             '''The affine cipher becomes weak when key '''
             '''A is set to 1. Choose different key'''      )
      if key_b == 0:
         sys.exit(
             '''The affine cipher becomes weak when key '''
             '''B is set to 0. Choose different key'''      )
   if key_a < 0 or key_b < 0 or key_b > len(snake_case      ) - 1:
      sys.exit(
          '''Key A must be greater than 0 and key B must '''
          F"be between 0 and {len(snake_case      ) - 1}."      )
   if cryptomath.gcd(snake_case							,			len(snake_case      )      ) != 1:
      sys.exit(
          F"Key A {key_a} and the symbol set size {len(snake_case      )} "
          '''are not relatively prime. Choose a different key.'''      )
def  __UpperCamelCase    (						snake_case							,			snake_case      )    ->		str:


   '''simple docstring'''







   __A   ,		__A    =  divmod(snake_case							,			len(snake_case      )      )
   check_keys(snake_case							,			snake_case							,			'''encrypt'''      )
   __A    =  ''''''
   for symbol in message:
      if symbol in SYMBOLS:
         __A    =  SYMBOLS.find(snake_case      )
         cipher_text += SYMBOLS[(sym_index * key_a + key_b) % len(snake_case      )]
      else:
         cipher_text += symbol
   return cipher_text
def  __UpperCamelCase    (						snake_case							,			snake_case      )    ->		str:


   '''simple docstring'''







   __A   ,		__A    =  divmod(snake_case							,			len(snake_case      )      )
   check_keys(snake_case							,			snake_case							,			'''decrypt'''      )
   __A    =  ''''''
   __A    =  cryptomath.find_mod_inverse(snake_case							,			len(snake_case      )      )
   for symbol in message:
      if symbol in SYMBOLS:
         __A    =  SYMBOLS.find(snake_case      )
         plain_text += SYMBOLS[
             (sym_index - key_b) * mod_inverse_of_key_a % len(snake_case      )
         ]
      else:
         plain_text += symbol
   return plain_text
def  __UpperCamelCase    (						)    ->		int:


   '''simple docstring'''







   while True:
      __A    =  random.randint(2							,			len(snake_case      )      )
      __A    =  random.randint(2							,			len(snake_case      )      )
      if cryptomath.gcd(snake_case							,			len(snake_case      )      ) == 1 and key_b % len(snake_case      ) != 0:
         return key_b * len(snake_case      ) + key_b
def  __UpperCamelCase    (						)    ->		None:


   '''simple docstring'''







   __A    =  input('''Enter message: '''      ).strip()
   __A    =  int(input('''Enter key [2000 - 9000]: '''      ).strip()      )
   __A    =  input('''Encrypt/Decrypt [E/D]: '''      ).strip().lower()

   if mode.startswith('''e'''      ):
      __A    =  '''encrypt'''
      __A    =  encrypt_message(snake_case							,			snake_case      )
   elif mode.startswith('''d'''      ):
      __A    =  '''decrypt'''
      __A    =  decrypt_message(snake_case							,			snake_case      )
   print(F"\n{mode.title()}ed text: \n{translated}"      )


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    # main()








from __future__ import annotations
def  __UpperCamelCase    (						snake_case      )    ->		bool:


   '''simple docstring'''







   __A    =  42
   __A    =  list(snake_case      )
   __A   ,		__A    =  _vector[:-1], int(_vector[-1]      )
   __A    =  [int(snake_case      ) for digit in __vector]

   vector.reverse()
   for i, digit in enumerate(snake_case      ):
      if i & 1 == 0:
         __A    =  digit * 2
         if doubled > 9:
            doubled -= 9
         check_digit += doubled
      else:
         check_digit += digit

   return check_digit % 1_0 == 0


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    assert is_luhn("""79927398713""")
    assert not is_luhn("""79927398714""")

