

"""simple docstring"""





import os
import sys

from . import rsa_key_generator as rkg

_a					 =				1_28
_a					 =				2_56




def     _A    (    UpperCamelCase_	:			str,     UpperCamelCase_	:			int = DEFAULT_BLOCK_SIZE)      ->		list[int]:
   '''simple docstring'''


   __lowercase								=		message.encode("ascii")

   __lowercase								=		[]
   for block_start in range(0,     len(UpperCamelCase_),     UpperCamelCase_):
      __lowercase								=		0
      for i in range(UpperCamelCase_,     min(block_start + block_size,     len(UpperCamelCase_))):
         block_int += message_bytes[i] * (BYTE_SIZE ** (i % block_size))
      block_ints.append(UpperCamelCase_)
   return block_ints




def     _A    (    UpperCamelCase_	:			list[int],     UpperCamelCase_	:			int,     UpperCamelCase_	:			int = DEFAULT_BLOCK_SIZE)      ->		str:
   '''simple docstring'''


   __lowercase								=		[]
   for block_int in block_ints:
      __lowercase								=		[]
      for i in range(block_size - 1,     -1,     -1):
         if len(UpperCamelCase_) + i < message_length:
            __lowercase								=		block_int // (BYTE_SIZE**i)
            __lowercase								=		block_int % (BYTE_SIZE**i)
            block_message.insert(0,     chr(UpperCamelCase_))
      message.extend(UpperCamelCase_)
   return "".join(UpperCamelCase_)




def     _A    (    UpperCamelCase_	:			str,     UpperCamelCase_	:			tuple[int, int],     UpperCamelCase_	:			int = DEFAULT_BLOCK_SIZE)      ->		list[int]:
   '''simple docstring'''


   __lowercase								=		[]
   __lowercase						,__lowercase								=		key

   for block in get_blocks_from_text(UpperCamelCase_,     UpperCamelCase_):
      encrypted_blocks.append(pow(UpperCamelCase_,     UpperCamelCase_,     UpperCamelCase_))
   return encrypted_blocks




def     _A    (    UpperCamelCase_	:			list[int],     UpperCamelCase_	:			int,     UpperCamelCase_	:			tuple[int, int],     UpperCamelCase_	:			int = DEFAULT_BLOCK_SIZE,     )      ->		str:
   '''simple docstring'''


   __lowercase								=		[]
   __lowercase						,__lowercase								=		key
   for block in encrypted_blocks:
      decrypted_blocks.append(pow(UpperCamelCase_,     UpperCamelCase_,     UpperCamelCase_))
   return get_text_from_blocks(UpperCamelCase_,     UpperCamelCase_,     UpperCamelCase_)




def     _A    (    UpperCamelCase_	:			str)      ->		tuple[int, int, int]:
   '''simple docstring'''


   with open(UpperCamelCase_) as fo:
      __lowercase								=		fo.read()
   __lowercase						,__lowercase						,__lowercase								=		content.split(",")
   return (int(UpperCamelCase_), int(UpperCamelCase_), int(UpperCamelCase_))




def     _A    (    UpperCamelCase_	:			str,     UpperCamelCase_	:			str,     UpperCamelCase_	:			str,     UpperCamelCase_	:			int = DEFAULT_BLOCK_SIZE,     )      ->		str:
   '''simple docstring'''


   __lowercase						,__lowercase						,__lowercase								=		read_key_file(UpperCamelCase_)
   if key_size < block_size * 8:
      sys.exit(
          "ERROR: Block size is {} bits and key size is {} bits. The RSA cipher "
          "requires the block size to be equal to or greater than the key size. "
          "Either decrease the block size or use different keys.".format(
              block_size * 8,     UpperCamelCase_))

   __lowercase								=		[str(UpperCamelCase_) for i in encrypt_message(UpperCamelCase_,     (n, e),     UpperCamelCase_)]

   __lowercase								=		",".join(UpperCamelCase_)
   __lowercase								=		F"""{len(UpperCamelCase_)}_{block_size}_{encrypted_content}"""
   with open(UpperCamelCase_,     "w") as fo:
      fo.write(UpperCamelCase_)
   return encrypted_content




def     _A    (    UpperCamelCase_	:			str,     UpperCamelCase_	:			str)      ->		str:
   '''simple docstring'''


   __lowercase						,__lowercase						,__lowercase								=		read_key_file(UpperCamelCase_)
   with open(UpperCamelCase_) as fo:
      __lowercase								=		fo.read()
   __lowercase						,__lowercase						,__lowercase								=		content.split("_")
   __lowercase								=		int(UpperCamelCase_)
   __lowercase								=		int(UpperCamelCase_)

   if key_size < block_size * 8:
      sys.exit(
          "ERROR: Block size is {} bits and key size is {} bits. The RSA cipher "
          "requires the block size to be equal to or greater than the key size. "
          "Did you specify the correct key file and encrypted file?".format(
              block_size * 8,     UpperCamelCase_))

   __lowercase								=		[]
   for block in encrypted_message.split(","):
      encrypted_blocks.append(int(UpperCamelCase_))

   return decrypt_message(UpperCamelCase_,     UpperCamelCase_,     (n, d),     UpperCamelCase_)




def     _A    (    )      ->		None:
   '''simple docstring'''


   __lowercase								=		"encrypted_file.txt"
   __lowercase								=		input(r"Encrypt\Decrypt [e\d]: ")

   if response.lower().startswith("e"):
      __lowercase								=		"encrypt"
   elif response.lower().startswith("d"):
      __lowercase								=		"decrypt"

   if mode == "encrypt":
      if not os.path.exists("rsa_pubkey.txt"):
         rkg.make_key_files("rsa",     1024)

      __lowercase								=		input("\nEnter message: ")
      __lowercase								=		"rsa_pubkey.txt"
      print(F"""Encrypting and writing to {filename}...""")
      __lowercase								=		encrypt_and_write_to_file(UpperCamelCase_,     UpperCamelCase_,     UpperCamelCase_)

      print("\nEncrypted text:")
      print(UpperCamelCase_)

   elif mode == "decrypt":
      __lowercase								=		"rsa_privkey.txt"
      print(F"""Reading from {filename} and decrypting...""")
      __lowercase								=		read_from_file_and_decrypt(UpperCamelCase_,     UpperCamelCase_)
      print("writing decryption to rsa_decryption.txt...")
      with open("rsa_decryption.txt",     "w") as dec:
         dec.write(UpperCamelCase_)

      print("\nDecryption:")
      print(UpperCamelCase_)


if __name__ == "__main__":
   main()







"""simple docstring"""





def     _A    (    UpperCamelCase_	:			int,     UpperCamelCase_	:			int = 1000)      ->		str:
   '''simple docstring'''


   if (not isinstance(UpperCamelCase_,     UpperCamelCase_)) or (digit_position <= 0):
      raise ValueError("Digit position must be a positive integer")
   elif (not isinstance(UpperCamelCase_,     UpperCamelCase_)) or (precision < 0):
      raise ValueError("Precision must be a nonnegative integer")

   # compute an approximation of (16 ** (n - 1)) * pi whose fractional part is mostly
   # accurate
   __lowercase								=		(
       4 * _subsum(UpperCamelCase_,     1,     UpperCamelCase_)
       - 2 * _subsum(UpperCamelCase_,     4,     UpperCamelCase_)
       - _subsum(UpperCamelCase_,     5,     UpperCamelCase_)
       - _subsum(UpperCamelCase_,     6,     UpperCamelCase_)
   )

   # return the first hex digit of the fractional part of the result
   return hex(int((sum_result % 1) * 16))[2:]




def     _A    (    UpperCamelCase_	:			int,     UpperCamelCase_	:			int,     UpperCamelCase_	:			int)      ->		float:
   '''simple docstring'''


   __lowercase								=		0.0
   for sum_index in range(digit_pos_to_extract + precision):
      __lowercase								=		8 * sum_index + denominator_addend
      if sum_index < digit_pos_to_extract:
         # if the exponential term is an integer and we mod it by the denominator
         # before dividing, only the integer part of the sum will change;
         # the fractional part will not
         __lowercase								=		pow(
             16,     digit_pos_to_extract - 1 - sum_index,     UpperCamelCase_)
      else:
         __lowercase								=		pow(16,     digit_pos_to_extract - 1 - sum_index)
      total += exponential_term / denominator
   return total


if __name__ == "__main__":
   import doctest

   doctest.testmod()




