




from typing import TYPE_CHECKING

from ..utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available


__lowerCAmelCase						:							Tuple			     =						{
    'configuration_utils': ['GenerationConfig'],
    'streamers': ['TextIteratorStreamer', 'TextStreamer'],
}

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__lowerCAmelCase						:							List[Any]			     =						[
		    'Constraint',
		    'ConstraintListState',
		    'DisjunctiveConstraint',
		    'PhrasalConstraint',
		]
		__lowerCAmelCase						:							int			     =						[
		    'BeamHypotheses',
		    'BeamScorer',
		    'BeamSearchScorer',
		    'ConstrainedBeamSearchScorer',
		]
		__lowerCAmelCase						:							List[str]			     =						[
		    'EpsilonLogitsWarper',
		    'EtaLogitsWarper',
		    'ForcedBOSTokenLogitsProcessor',
		    'ForcedEOSTokenLogitsProcessor',
		    'HammingDiversityLogitsProcessor',
		    'InfNanRemoveLogitsProcessor',
		    'LogitsProcessor',
		    'LogitsProcessorList',
		    'LogitsWarper',
		    'MinLengthLogitsProcessor',
		    'MinNewTokensLengthLogitsProcessor',
		    'NoBadWordsLogitsProcessor',
		    'NoRepeatNGramLogitsProcessor',
		    'PrefixConstrainedLogitsProcessor',
		    'RepetitionPenaltyLogitsProcessor',
		    'SequenceBiasLogitsProcessor',
		    'EncoderRepetitionPenaltyLogitsProcessor',
		    'TemperatureLogitsWarper',
		    'TopKLogitsWarper',
		    'TopPLogitsWarper',
		    'TypicalLogitsWarper',
		    'EncoderNoRepeatNGramLogitsProcessor',
		    'ExponentialDecayLengthPenalty',
		    'LogitNormalization',
		]
		__lowerCAmelCase						:							int			     =						[
		    'MaxNewTokensCriteria',
		    'MaxLengthCriteria',
		    'MaxTimeCriteria',
		    'StoppingCriteria',
		    'StoppingCriteriaList',
		    'validate_stopping_criteria',
		]
		__lowerCAmelCase						:							Dict			     =						[
		    'GenerationMixin',
		    'top_k_top_p_filtering',
		    'GreedySearchEncoderDecoderOutput',
		    'GreedySearchDecoderOnlyOutput',
		    'SampleEncoderDecoderOutput',
		    'SampleDecoderOnlyOutput',
		    'BeamSearchEncoderDecoderOutput',
		    'BeamSearchDecoderOnlyOutput',
		    'BeamSampleEncoderDecoderOutput',
		    'BeamSampleDecoderOnlyOutput',
		    'ContrastiveSearchEncoderDecoderOutput',
		    'ContrastiveSearchDecoderOnlyOutput',
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__lowerCAmelCase						:							List[str]			     =						[
		    'TFForcedBOSTokenLogitsProcessor',
		    'TFForcedEOSTokenLogitsProcessor',
		    'TFLogitsProcessor',
		    'TFLogitsProcessorList',
		    'TFLogitsWarper',
		    'TFMinLengthLogitsProcessor',
		    'TFNoBadWordsLogitsProcessor',
		    'TFNoRepeatNGramLogitsProcessor',
		    'TFRepetitionPenaltyLogitsProcessor',
		    'TFTemperatureLogitsWarper',
		    'TFTopKLogitsWarper',
		    'TFTopPLogitsWarper',
		    'TFForceTokensLogitsProcessor',
		    'TFSuppressTokensAtBeginLogitsProcessor',
		    'TFSuppressTokensLogitsProcessor',
		]
		__lowerCAmelCase						:							Dict			     =						[
		    'TFGenerationMixin',
		    'tf_top_k_top_p_filtering',
		    'TFGreedySearchDecoderOnlyOutput',
		    'TFGreedySearchEncoderDecoderOutput',
		    'TFSampleEncoderDecoderOutput',
		    'TFSampleDecoderOnlyOutput',
		    'TFBeamSearchEncoderDecoderOutput',
		    'TFBeamSearchDecoderOnlyOutput',
		    'TFBeamSampleEncoderDecoderOutput',
		    'TFBeamSampleDecoderOnlyOutput',
		    'TFContrastiveSearchEncoderDecoderOutput',
		    'TFContrastiveSearchDecoderOnlyOutput',
		]

try:
		if not is_flax_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__lowerCAmelCase						:							Optional[Any]			     =						[
		    'FlaxForcedBOSTokenLogitsProcessor',
		    'FlaxForcedEOSTokenLogitsProcessor',
		    'FlaxLogitsProcessor',
		    'FlaxLogitsProcessorList',
		    'FlaxLogitsWarper',
		    'FlaxMinLengthLogitsProcessor',
		    'FlaxTemperatureLogitsWarper',
		    'FlaxTopKLogitsWarper',
		    'FlaxTopPLogitsWarper',
		]
		__lowerCAmelCase						:							Any			     =						[
		    'FlaxGenerationMixin',
		    'FlaxGreedySearchOutput',
		    'FlaxSampleOutput',
		    'FlaxBeamSearchOutput',
		]

if TYPE_CHECKING:
		from .configuration_utils import GenerationConfig
		from .streamers import TextIteratorStreamer, TextStreamer

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .beam_constraints import Constraint, ConstraintListState, DisjunctiveConstraint, PhrasalConstraint
				from .beam_search import BeamHypotheses, BeamScorer, BeamSearchScorer, ConstrainedBeamSearchScorer
				from .logits_process import (
				    EncoderNoRepeatNGramLogitsProcessor,
				    EncoderRepetitionPenaltyLogitsProcessor,
				    EpsilonLogitsWarper,
				    EtaLogitsWarper,
				    ExponentialDecayLengthPenalty,
				    ForcedBOSTokenLogitsProcessor,
				    ForcedEOSTokenLogitsProcessor,
				    HammingDiversityLogitsProcessor,
				    InfNanRemoveLogitsProcessor,
				    LogitNormalization,
				    LogitsProcessor,
				    LogitsProcessorList,
				    LogitsWarper,
				    MinLengthLogitsProcessor,
				    MinNewTokensLengthLogitsProcessor,
				    NoBadWordsLogitsProcessor,
				    NoRepeatNGramLogitsProcessor,
				    PrefixConstrainedLogitsProcessor,
				    RepetitionPenaltyLogitsProcessor,
				    SequenceBiasLogitsProcessor,
				    TemperatureLogitsWarper,
				    TopKLogitsWarper,
				    TopPLogitsWarper,
				    TypicalLogitsWarper,
				)
				from .stopping_criteria import (
				    MaxLengthCriteria,
				    MaxNewTokensCriteria,
				    MaxTimeCriteria,
				    StoppingCriteria,
				    StoppingCriteriaList,
				    validate_stopping_criteria,
				)
				from .utils import (
				    BeamSampleDecoderOnlyOutput,
				    BeamSampleEncoderDecoderOutput,
				    BeamSearchDecoderOnlyOutput,
				    BeamSearchEncoderDecoderOutput,
				    ContrastiveSearchDecoderOnlyOutput,
				    ContrastiveSearchEncoderDecoderOutput,
				    GenerationMixin,
				    GreedySearchDecoderOnlyOutput,
				    GreedySearchEncoderDecoderOutput,
				    SampleDecoderOnlyOutput,
				    SampleEncoderDecoderOutput,
				    top_k_top_p_filtering,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tf_logits_process import (
				    TFForcedBOSTokenLogitsProcessor,
				    TFForcedEOSTokenLogitsProcessor,
				    TFForceTokensLogitsProcessor,
				    TFLogitsProcessor,
				    TFLogitsProcessorList,
				    TFLogitsWarper,
				    TFMinLengthLogitsProcessor,
				    TFNoBadWordsLogitsProcessor,
				    TFNoRepeatNGramLogitsProcessor,
				    TFRepetitionPenaltyLogitsProcessor,
				    TFSuppressTokensAtBeginLogitsProcessor,
				    TFSuppressTokensLogitsProcessor,
				    TFTemperatureLogitsWarper,
				    TFTopKLogitsWarper,
				    TFTopPLogitsWarper,
				)
				from .tf_utils import (
				    TFBeamSampleDecoderOnlyOutput,
				    TFBeamSampleEncoderDecoderOutput,
				    TFBeamSearchDecoderOnlyOutput,
				    TFBeamSearchEncoderDecoderOutput,
				    TFContrastiveSearchDecoderOnlyOutput,
				    TFContrastiveSearchEncoderDecoderOutput,
				    TFGenerationMixin,
				    TFGreedySearchDecoderOnlyOutput,
				    TFGreedySearchEncoderDecoderOutput,
				    TFSampleDecoderOnlyOutput,
				    TFSampleEncoderDecoderOutput,
				    tf_top_k_top_p_filtering,
				)

		try:
				if not is_flax_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .flax_logits_process import (
				    FlaxForcedBOSTokenLogitsProcessor,
				    FlaxForcedEOSTokenLogitsProcessor,
				    FlaxLogitsProcessor,
				    FlaxLogitsProcessorList,
				    FlaxLogitsWarper,
				    FlaxMinLengthLogitsProcessor,
				    FlaxTemperatureLogitsWarper,
				    FlaxTopKLogitsWarper,
				    FlaxTopPLogitsWarper,
				)
				from .flax_utils import FlaxBeamSearchOutput, FlaxGenerationMixin, FlaxGreedySearchOutput, FlaxSampleOutput
else:
		import sys

		__lowerCAmelCase						:							Union[str, Any]			     =						_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)












import os
import sys

from . import rsa_key_generator as rkg

UpperCAmelCase__    :							str       		=     128
UpperCAmelCase__    :							int       		=     256


def 		_lowerCamelCase					(       UpperCamelCase       ,  UpperCamelCase = DEFAULT_BLOCK_SIZE				)      ->		list[int]:





  """simple docstring"""





  _lowercase:			List[str]      =     message.encode('ascii'				)

  _lowercase:			str      =     []
  for block_start in range(0       ,  len(UpperCamelCase				)       ,  UpperCamelCase				):
    _lowercase:			Optional[int]      =     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 		_lowerCamelCase					(       UpperCamelCase       ,  UpperCamelCase       ,  UpperCamelCase = DEFAULT_BLOCK_SIZE				)      ->		str:





  """simple docstring"""





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


def 		_lowerCamelCase					(       UpperCamelCase       ,  UpperCamelCase       ,  UpperCamelCase = DEFAULT_BLOCK_SIZE				)      ->		list[int]:





  """simple docstring"""





  _lowercase:			Optional[int]      =     []
  _lowercase  ,       _lowercase:			str      =     key

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


def 		_lowerCamelCase					(       UpperCamelCase       ,  UpperCamelCase       ,  UpperCamelCase       ,  UpperCamelCase = DEFAULT_BLOCK_SIZE       ,  )      ->		str:





  """simple docstring"""





  _lowercase:			Optional[int]      =     []
  _lowercase  ,       _lowercase:			str      =     key
  for block in encrypted_blocks:
    decrypted_blocks.append(pow(UpperCamelCase       ,  UpperCamelCase       ,  UpperCamelCase				)				)
  return get_text_from_blocks(UpperCamelCase       ,  UpperCamelCase       ,  UpperCamelCase				)


def 		_lowerCamelCase					(       UpperCamelCase				)      ->		tuple[int, int, int]:





  """simple docstring"""





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


def 		_lowerCamelCase					(       UpperCamelCase       ,  UpperCamelCase       ,  UpperCamelCase       ,  UpperCamelCase = DEFAULT_BLOCK_SIZE       ,  )      ->		str:





  """simple docstring"""





  _lowercase  ,       _lowercase  ,       _lowercase:			int      =     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:			int      =     [str(UpperCamelCase				) for i in encrypt_message(UpperCamelCase       ,  (n, e)       ,  UpperCamelCase				)]

  _lowercase:			List[Any]      =     ','.join(UpperCamelCase				)
  _lowercase:			Tuple      =     F'{len(UpperCamelCase				)}_{block_size}_{encrypted_content}'
  with open(UpperCamelCase       ,  'w'				) as fo:
    fo.write(UpperCamelCase				)
  return encrypted_content


def 		_lowerCamelCase					(       UpperCamelCase       ,  UpperCamelCase				)      ->		str:





  """simple docstring"""





  _lowercase  ,       _lowercase  ,       _lowercase:			Tuple      =     read_key_file(UpperCamelCase				)
  with open(UpperCamelCase				) as fo:
    _lowercase:			Optional[Any]      =     fo.read()
  _lowercase  ,       _lowercase  ,       _lowercase:			Union[str, Any]      =     content.split('_'				)
  _lowercase:			List[Any]      =     int(UpperCamelCase				)
  _lowercase:			Dict      =     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:			Union[str, Any]      =     []
  for block in encrypted_message.split(','				):
    encrypted_blocks.append(int(UpperCamelCase				)				)

  return decrypt_message(UpperCamelCase       ,  UpperCamelCase       ,  (n, d)       ,  UpperCamelCase				)


def 		_lowerCamelCase					(       )      ->		None:





  """simple docstring"""





  _lowercase:			Tuple      =     'encrypted_file.txt'
  _lowercase:			Optional[Any]      =     input(r'Encrypt\Decrypt [e\d]: '				)

  if response.lower().startswith('e'				):
    _lowercase:			Optional[Any]      =     'encrypt'
  elif response.lower().startswith('d'				):
    _lowercase:			Union[str, Any]      =     'decrypt'

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

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

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

  elif mode == "decrypt":
    _lowercase:			Optional[Any]      =     'rsa_privkey.txt'
    print(F'Reading from {filename} and decrypting...'				)
    _lowercase:			int      =     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()
