





import random
import sys

from . import cryptomath_module as cryptomath

_UpperCamelCase   :							List[str]     =	(
    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

import sys
def 				__UpperCamelCase  (		snake_case    ,	snake_case     )  ->						tuple[int, int]:


   '''simple docstring'''







   if abs(snake_case     ) == 1:
      return a, 0
   elif abs(snake_case     ) == 1:
      return 0, b

   __A ,						__A				 =       a, b
   __A ,						__A				 =       1, 0
   __A ,						__A				 =       0, 1

   while remainder != 0:
      __A				 =       old_remainder // remainder
      __A ,						__A				 =       remainder, old_remainder - quotient * remainder
      __A ,						__A				 =       coeff_a, old_coeff_a - quotient * coeff_a
      __A ,						__A				 =       coeff_b, old_coeff_b - quotient * coeff_b

   # sign correction for negative numbers
   if a < 0:
      __A				 =       -old_coeff_a
   if b < 0:
      __A				 =       -old_coeff_b

   return old_coeff_a, old_coeff_b
def 				__UpperCamelCase  (		)  ->						Tuple:


   '''simple docstring'''







   if len(sys.argv     ) < 3:
      print('''2 integer arguments required'''     )
      return 1
   __A				 =       int(sys.argv[1]     )
   __A				 =       int(sys.argv[2]     )
   print(extended_euclidean_algorithm(snake_case    ,	snake_case     )     )
   return 0


if __name__ == "__main__":
    raise SystemExit(main())

