






import os
import sys

from . import rsa_key_generator as rkg

a 			=							128
a 			=							256
def    a__    (	A		,					A = DEFAULT_BLOCK_SIZE       ):
   lowercase__    :						Optional[int]       =			message.encode("""ascii"""       )

   lowercase__    :						List[str]       =			[]
   for block_start in range(0		,					len(lowercase__       )		,					lowercase__       ):
      lowercase__    :						List[str]       =			0
      for i in range(lowercase__		,					min(block_start + block_size		,					len(lowercase__       )       )       ):
         block_int += message_bytes[i] * (BYTE_SIZE ** (i % block_size))
      block_ints.append(lowercase__       )
   return block_ints
def    a__    (	A		,					A		,					A = DEFAULT_BLOCK_SIZE       ):
   lowercase__    :						int       =			[]
   for block_int in block_ints:
      lowercase__    :						List[str]       =			[]
      for i in range(block_size - 1		,					-1		,					-1       ):
         if len(lowercase__       ) + i < message_length:
            lowercase__    :						Tuple       =			block_int // (BYTE_SIZE**i)
            lowercase__    :						Optional[int]       =			block_int % (BYTE_SIZE**i)
            block_message.insert(0		,					chr(lowercase__       )       )
      message.extend(lowercase__       )
   return "".join(lowercase__       )
def    a__    (	A		,					A		,					A = DEFAULT_BLOCK_SIZE       ):
   lowercase__    :						List[str]       =			[]
   lowercase__  ,       lowercase__    :						Optional[Any]       =			key

   for block in get_blocks_from_text(lowercase__		,					lowercase__       ):
      encrypted_blocks.append(pow(lowercase__		,					lowercase__		,					lowercase__       )       )
   return encrypted_blocks
def    a__    (	A		,					A		,					A		,					A = DEFAULT_BLOCK_SIZE		,					):
   lowercase__    :						Optional[int]       =			[]
   lowercase__  ,       lowercase__    :						List[str]       =			key
   for block in encrypted_blocks:
      decrypted_blocks.append(pow(lowercase__		,					lowercase__		,					lowercase__       )       )
   return get_text_from_blocks(lowercase__		,					lowercase__		,					lowercase__       )
def    a__    (	A       ):







   with open(lowercase__       ) as fo:
      lowercase__    :						Optional[int]       =			fo.read()
   lowercase__  ,       lowercase__  ,       lowercase__    :						List[str]       =			content.split(""","""       )
   return (int(lowercase__       ), int(lowercase__       ), int(lowercase__       ))
def    a__    (	A		,					A		,					A		,					A = DEFAULT_BLOCK_SIZE		,					):
   lowercase__  ,       lowercase__  ,       lowercase__    :						List[str]       =			read_key_file(lowercase__       )
   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		,					lowercase__       )       )

   lowercase__    :						Optional[int]       =			[str(lowercase__       ) for i in encrypt_message(lowercase__		,					(n, e)		,					lowercase__       )]

   lowercase__    :						int       =			""",""".join(lowercase__       )
   lowercase__    :						Union[str, Any]       =			F'''{len(lowercase__       )}_{block_size}_{encrypted_content}'''
   with open(lowercase__		,					"""w"""       ) as fo:
      fo.write(lowercase__       )
   return encrypted_content
def    a__    (	A		,					A       ):
   lowercase__  ,       lowercase__  ,       lowercase__    :						str       =			read_key_file(lowercase__       )
   with open(lowercase__       ) as fo:
      lowercase__    :						Union[str, Any]       =			fo.read()
   lowercase__  ,       lowercase__  ,       lowercase__    :						Optional[Any]       =			content.split("""_"""       )
   lowercase__    :						List[Any]       =			int(lowercase__       )
   lowercase__    :						Union[str, Any]       =			int(lowercase__       )

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

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

   return decrypt_message(lowercase__		,					lowercase__		,					(n, d)		,					lowercase__       )
def    a__    (	):
   lowercase__    :						List[str]       =			"""encrypted_file.txt"""
   lowercase__    :						Any       =			input(R"""Encrypt\Decrypt [e\d]: """       )

   if response.lower().startswith("""e"""       ):
      lowercase__    :						Tuple       =			"""encrypt"""
   elif response.lower().startswith("""d"""       ):
      lowercase__    :						Any       =			"""decrypt"""

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

      lowercase__    :						Dict       =			input("""\nEnter message: """       )
      lowercase__    :						int       =			"""rsa_pubkey.txt"""
      print(F'''Encrypting and writing to {filename}...'''       )
      lowercase__    :						Optional[int]       =			encrypt_and_write_to_file(lowercase__		,					lowercase__		,					lowercase__       )

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

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

      print("""\nDecryption:"""       )
      print(lowercase__       )


if __name__ == "__main__":
 main()







'''simple docstring'''


import unittest
from dataclasses import dataclass
from typing import List, Union

import numpy as np
import PIL.Image

from diffusers.utils.outputs import BaseOutput







@dataclass
class 						UpperCAmelCase						(		__a):


 '''simple docstring'''




 __magic_name__       :			Union[List[PIL.Image.Image], np.ndarray]







class 						UpperCAmelCase						(		unittest.TestCase):


 '''simple docstring'''




 def    lowercase_	( self)			->   List[str]:



      """simple docstring"""







      a_									=CustomOutput(images=np.random.rand(1				,	3				,	4				,	4))

      # check every way of getting the attribute
      assert isinstance(outputs.images				,	np.ndarray)
      assert outputs.images.shape == (1, 3, 4, 4)
      assert isinstance(outputs["images"]				,	np.ndarray)
      assert outputs["images"].shape == (1, 3, 4, 4)
      assert isinstance(outputs[0]				,	np.ndarray)
      assert outputs[0].shape == (1, 3, 4, 4)

      # test with a non-tensor attribute
      a_									=CustomOutput(images=[PIL.Image.new("RGB"				,	(4, 4))])

      # check every way of getting the attribute
      assert isinstance(outputs.images				,	lowerCAmelCase_)
      assert isinstance(outputs.images[0]				,	PIL.Image.Image)
      assert isinstance(outputs["images"]				,	lowerCAmelCase_)
      assert isinstance(outputs["images"][0]				,	PIL.Image.Image)
      assert isinstance(outputs[0]				,	lowerCAmelCase_)
      assert isinstance(outputs[0][0]				,	PIL.Image.Image)




 def    lowercase_	( self)			->   Optional[int]:



      """simple docstring"""







      a_									=CustomOutput({"images": np.random.rand(1				,	3				,	4				,	4)})

      # check every way of getting the attribute
      assert isinstance(outputs.images				,	np.ndarray)
      assert outputs.images.shape == (1, 3, 4, 4)
      assert isinstance(outputs["images"]				,	np.ndarray)
      assert outputs["images"].shape == (1, 3, 4, 4)
      assert isinstance(outputs[0]				,	np.ndarray)
      assert outputs[0].shape == (1, 3, 4, 4)

      # test with a non-tensor attribute
      a_									=CustomOutput({"images": [PIL.Image.new("RGB"				,	(4, 4))]})

      # check every way of getting the attribute
      assert isinstance(outputs.images				,	lowerCAmelCase_)
      assert isinstance(outputs.images[0]				,	PIL.Image.Image)
      assert isinstance(outputs["images"]				,	lowerCAmelCase_)
      assert isinstance(outputs["images"][0]				,	PIL.Image.Image)
      assert isinstance(outputs[0]				,	lowerCAmelCase_)
      assert isinstance(outputs[0][0]				,	PIL.Image.Image)


