'''simple docstring'''


import os
import sys

from . import rsa_key_generator as rkg

snake_case__     :			str      			=  128
snake_case__     :			Optional[Any]      			=  256


def 					_lowerCamelCase    (      lowerCamelCase_					:							str	,     lowerCamelCase_					:							int = DEFAULT_BLOCK_SIZE   ):



     """simple docstring"""





     UpperCAmelCase_							:   str   =      message.encode('ascii'   )

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


def 					_lowerCamelCase    (      lowerCamelCase_					:							list[int]	,     lowerCamelCase_					:							int	,     lowerCamelCase_					:							int = DEFAULT_BLOCK_SIZE   ):



     """simple docstring"""





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


def 					_lowerCamelCase    (      lowerCamelCase_					:							str	,     lowerCamelCase_					:							tuple[int, int]	,     lowerCamelCase_					:							int = DEFAULT_BLOCK_SIZE   ):



     """simple docstring"""





     UpperCAmelCase_							:   Union[str, Any]   =      []
     UpperCAmelCase_   ,			UpperCAmelCase_							:   Dict   =      key

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


def 					_lowerCamelCase    (      lowerCamelCase_					:							list[int]	,     lowerCamelCase_					:							int	,     lowerCamelCase_					:							tuple[int, int]	,     lowerCamelCase_					:							int = DEFAULT_BLOCK_SIZE	,     ):



     """simple docstring"""





     UpperCAmelCase_							:   Tuple   =      []
     UpperCAmelCase_   ,			UpperCAmelCase_							:   Dict   =      key
     for block in encrypted_blocks:
          decrypted_blocks.append(pow(lowerCamelCase_	,     lowerCamelCase_	,     lowerCamelCase_   )   )
     return get_text_from_blocks(lowerCamelCase_	,     lowerCamelCase_	,     lowerCamelCase_   )


def 					_lowerCamelCase    (      lowerCamelCase_					:							str   ):



     """simple docstring"""





     with open(lowerCamelCase_   ) as fo:
          UpperCAmelCase_							:   Optional[Any]   =      fo.read()
     UpperCAmelCase_   ,			UpperCAmelCase_   ,			UpperCAmelCase_							:   str   =      content.split(','   )
     return (int(lowerCamelCase_   ), int(lowerCamelCase_   ), int(lowerCamelCase_   ))


def 					_lowerCamelCase    (      lowerCamelCase_					:							str	,     lowerCamelCase_					:							str	,     lowerCamelCase_					:							str	,     lowerCamelCase_					:							int = DEFAULT_BLOCK_SIZE	,     ):



     """simple docstring"""





     UpperCAmelCase_   ,			UpperCAmelCase_   ,			UpperCAmelCase_							:   Optional[int]   =      read_key_file(lowerCamelCase_   )
     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	,     lowerCamelCase_   )   )

     UpperCAmelCase_							:   Union[str, Any]   =      [str(lowerCamelCase_   ) for i in encrypt_message(lowerCamelCase_	,     (n, e)	,     lowerCamelCase_   )]

     UpperCAmelCase_							:   int   =      ','.join(lowerCamelCase_   )
     UpperCAmelCase_							:   Optional[Any]   =      F'''{len(lowerCamelCase_   )}_{block_size}_{encrypted_content}'''
     with open(lowerCamelCase_	,     'w'   ) as fo:
          fo.write(lowerCamelCase_   )
     return encrypted_content


def 					_lowerCamelCase    (      lowerCamelCase_					:							str	,     lowerCamelCase_					:							str   ):



     """simple docstring"""





     UpperCAmelCase_   ,			UpperCAmelCase_   ,			UpperCAmelCase_							:   Optional[Any]   =      read_key_file(lowerCamelCase_   )
     with open(lowerCamelCase_   ) as fo:
          UpperCAmelCase_							:   Dict   =      fo.read()
     UpperCAmelCase_   ,			UpperCAmelCase_   ,			UpperCAmelCase_							:   List[str]   =      content.split('_'   )
     UpperCAmelCase_							:   str   =      int(lowerCamelCase_   )
     UpperCAmelCase_							:   List[str]   =      int(lowerCamelCase_   )

     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	,     lowerCamelCase_   )   )

     UpperCAmelCase_							:   Tuple   =      []
     for block in encrypted_message.split(','   ):
          encrypted_blocks.append(int(lowerCamelCase_   )   )

     return decrypt_message(lowerCamelCase_	,     lowerCamelCase_	,     (n, d)	,     lowerCamelCase_   )


def 					_lowerCamelCase    (      ):



     """simple docstring"""





     UpperCAmelCase_							:   List[Any]   =      'encrypted_file.txt'
     UpperCAmelCase_							:   List[Any]   =      input(R'Encrypt\Decrypt [e\d]: '   )

     if response.lower().startswith('e'   ):
          UpperCAmelCase_							:   Any   =      'encrypt'
     elif response.lower().startswith('d'   ):
          UpperCAmelCase_							:   int   =      'decrypt'

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

          UpperCAmelCase_							:   Any   =      input('\nEnter message: '   )
          UpperCAmelCase_							:   Union[str, Any]   =      'rsa_pubkey.txt'
          print(F'''Encrypting and writing to {filename}...'''   )
          UpperCAmelCase_							:   Any   =      encrypt_and_write_to_file(lowerCamelCase_	,     lowerCamelCase_	,     lowerCamelCase_   )

          print('\nEncrypted text:'   )
          print(lowerCamelCase_   )

     elif mode == "decrypt":
          UpperCAmelCase_							:   List[str]   =      'rsa_privkey.txt'
          print(F'''Reading from {filename} and decrypting...'''   )
          UpperCAmelCase_							:   Any   =      read_from_file_and_decrypt(lowerCamelCase_	,     lowerCamelCase_   )
          print('writing decryption to rsa_decryption.txt...'   )
          with open('rsa_decryption.txt'	,     'w'   ) as dec:
               dec.write(lowerCamelCase_   )

          print('\nDecryption:'   )
          print(lowerCamelCase_   )


if __name__ == "__main__":
 main()







'''simple docstring'''


def 					_lowerCamelCase    (      lowerCamelCase_					:							str   ):



     """simple docstring"""





     UpperCAmelCase_							:   str   =      str(lowerCamelCase_   ).strip()
     if not oct_string:
          raise ValueError('Empty string was passed to the function'   )
     UpperCAmelCase_							:   Tuple   =      oct_string[0] == '-'
     if is_negative:
          UpperCAmelCase_							:   Tuple   =      oct_string[1:]
     if not oct_string.isdigit() or not all(0 <= int(lowerCamelCase_   ) <= 7 for char in oct_string   ):
          raise ValueError('Non-octal value was passed to the function'   )
     UpperCAmelCase_							:   str   =      0
     for char in oct_string:
          UpperCAmelCase_							:   List[Any]   =      8 * decimal_number + int(lowerCamelCase_   )
     if is_negative:
          UpperCAmelCase_							:   List[Any]   =      -decimal_number
     return decimal_number


if __name__ == "__main__":
 from doctest import testmod

 testmod()






