





'''simple docstring'''







import os
import sys

from . import rsa_key_generator as rkg

snake_case_  :							int            =	1_28
snake_case_  :							str            =	2_56





def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      str,      SCREAMING_SNAKE_CASE__   :      int = DEFAULT_BLOCK_SIZE )			->						list[int]:
						UpperCAmelCase_      :   str      =	message.encode('''ascii''' )

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





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





def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      str,      SCREAMING_SNAKE_CASE__   :      tuple[int, int],      SCREAMING_SNAKE_CASE__   :      int = DEFAULT_BLOCK_SIZE )			->						list[int]:
						UpperCAmelCase_      :   List[str]      =	[]
						UpperCAmelCase_    ,							UpperCAmelCase_      :   Optional[Any]      =	key

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





def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      list[int],      SCREAMING_SNAKE_CASE__   :      int,      SCREAMING_SNAKE_CASE__   :      tuple[int, int],      SCREAMING_SNAKE_CASE__   :      int = DEFAULT_BLOCK_SIZE,      )			->						str:
						UpperCAmelCase_      :   Union[str, Any]      =	[]
						UpperCAmelCase_    ,							UpperCAmelCase_      :   List[Any]      =	key
						for block in encrypted_blocks:
												decrypted_blocks.append(pow(SCREAMING_SNAKE_CASE__,      SCREAMING_SNAKE_CASE__,      SCREAMING_SNAKE_CASE__ ) )
						return get_text_from_blocks(SCREAMING_SNAKE_CASE__,      SCREAMING_SNAKE_CASE__,      SCREAMING_SNAKE_CASE__ )





def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      str )			->						tuple[int, int, int]:
						with open(SCREAMING_SNAKE_CASE__ ) as fo:
												UpperCAmelCase_      :   Tuple      =	fo.read()
						UpperCAmelCase_    ,							UpperCAmelCase_    ,							UpperCAmelCase_      :   List[Any]      =	content.split(''',''' )
						return (int(SCREAMING_SNAKE_CASE__ ), int(SCREAMING_SNAKE_CASE__ ), int(SCREAMING_SNAKE_CASE__ ))





def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      str,      SCREAMING_SNAKE_CASE__   :      str,      SCREAMING_SNAKE_CASE__   :      str,      SCREAMING_SNAKE_CASE__   :      int = DEFAULT_BLOCK_SIZE,      )			->						str:
						UpperCAmelCase_    ,							UpperCAmelCase_    ,							UpperCAmelCase_      :   str      =	read_key_file(SCREAMING_SNAKE_CASE__ )
						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,      SCREAMING_SNAKE_CASE__ ) )

						UpperCAmelCase_      :   Tuple      =	[str(SCREAMING_SNAKE_CASE__ ) for i in encrypt_message(SCREAMING_SNAKE_CASE__,      (n, e),      SCREAMING_SNAKE_CASE__ )]

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





def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      str,      SCREAMING_SNAKE_CASE__   :      str )			->						str:
						UpperCAmelCase_    ,							UpperCAmelCase_    ,							UpperCAmelCase_      :   str      =	read_key_file(SCREAMING_SNAKE_CASE__ )
						with open(SCREAMING_SNAKE_CASE__ ) as fo:
												UpperCAmelCase_      :   Union[str, Any]      =	fo.read()
						UpperCAmelCase_    ,							UpperCAmelCase_    ,							UpperCAmelCase_      :   Union[str, Any]      =	content.split('''_''' )
						UpperCAmelCase_      :   int      =	int(SCREAMING_SNAKE_CASE__ )
						UpperCAmelCase_      :   Union[str, Any]      =	int(SCREAMING_SNAKE_CASE__ )

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

						UpperCAmelCase_      :   List[Any]      =	[]
						for block in encrypted_message.split(''',''' ):
												encrypted_blocks.append(int(SCREAMING_SNAKE_CASE__ ) )

						return decrypt_message(SCREAMING_SNAKE_CASE__,      SCREAMING_SNAKE_CASE__,      (n, d),      SCREAMING_SNAKE_CASE__ )





def 							lowerCamelCase_   (	)			->						None:
						UpperCAmelCase_      :   int      =	'''encrypted_file.txt'''
						UpperCAmelCase_      :   str      =	input(R'''Encrypt\Decrypt [e\d]: ''' )

						if response.lower().startswith('''e''' ):
												UpperCAmelCase_      :   List[Any]      =	'''encrypt'''
						elif response.lower().startswith('''d''' ):
												UpperCAmelCase_      :   Dict      =	'''decrypt'''

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

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

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

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

												print('''\nDecryption:''' )
												print(SCREAMING_SNAKE_CASE__ )


if __name__ == "__main__":
					main()







'''simple docstring'''







import unittest

from cer import CER


__SCREAMING_SNAKE_CASE		:  List[str]        =      CER()
class  SCREAMING_SNAKE_CASE__       (  unittest.TestCase   ):
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     Optional[Any]								=	["""White House"""]
					__a       :     Tuple								=	["""white house"""]
					# S = 2, D = 0, I = 0, N = 11, CER = 2 / 11
					__a       :     Any								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.1_8_1_8_1_8_1_8_1_8			) < 1E-6			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     Optional[int]								=	["""were wolf"""]
					__a       :     Tuple								=	["""werewolf"""]
					# S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
					__a       :     Any								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.1_1_1_1_1_1_1			) < 1E-6			)

					__a       :     str								=	["""werewolf"""]
					__a       :     List[Any]								=	["""weae     wolf"""]
					# S = 1, D = 1, I = 0, N = 8, CER = 0.25
					__a       :     Tuple								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.2_5			) < 1E-6			)

					# consecutive whitespaces case 1
					__a       :     int								=	["""were wolf"""]
					__a       :     Dict								=	["""were               wolf"""]
					# S = 0, D = 0, I = 0, N = 9, CER = 0
					__a       :     str								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.0			) < 1E-6			)

					# consecutive whitespaces case 2
					__a       :     List[str]								=	["""were   wolf"""]
					__a       :     Optional[int]								=	["""were               wolf"""]
					# S = 0, D = 0, I = 0, N = 9, CER = 0
					__a       :     Optional[Any]								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.0			) < 1E-6			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     Dict								=	["""werewolf"""]
					__a       :     str								=	["""weaewolf"""]
					# S = 1, D = 0, I = 0, N = 8, CER = 0.125
					__a       :     Optional[int]								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.1_2_5			) < 1E-6			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     List[str]								=	["""werewolf"""]
					__a       :     Tuple								=	["""wereawolf"""]
					# S = 0, D = 1, I = 0, N = 8, CER = 0.125
					__a       :     Optional[Any]								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.1_2_5			) < 1E-6			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     List[str]								=	["""werewolf"""]
					__a       :     Optional[Any]								=	["""wereolf"""]
					# S = 0, D = 0, I = 1, N = 8, CER = 0.125
					__a       :     int								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.1_2_5			) < 1E-6			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     Dict								=	["""werewolf"""]
					__a       :     Tuple								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertEqual(__UpperCamelCase  ,    0.0			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     List[str]								=	["""werewolf""", """I am your father"""]
					__a       :     int								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertEqual(__UpperCamelCase  ,    0.0			)

					__a       :     Dict								=	["""werewolf""", """I am your father""", """doge"""]
					__a       :     Optional[Any]								=	["""werxwolf""", """I       am your father""", """doge"""]
					# S = 1, D = 0, I = 0, N = 28, CER = 1 / 28
					__a       :     Union[str, Any]								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.0_3_5_7_1_4_2_8			) < 1E-6			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     List[str]								=	["""My hovercraft""", """is full of eels"""]
					__a       :     Union[str, Any]								=	["""My hovercraft is full""", """ of eels"""]
					# S = 0, D = 0, I = 2, N = 28, CER = 2 / 28
					# whitespace at the front of " of eels" will be strip during preporcessing
					# so need to insert 2 whitespaces
					__a       :     str								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase  ,    concatenate_texts=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.0_7_1_4_2_8			) < 1E-6			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     int								=	["""我能吞下玻璃而不伤身体"""]
					__a       :     Optional[Any]								=	[""" 能吞虾玻璃而 不霜身体啦"""]
					# S = 3, D = 2, I = 0, N = 11, CER = 5 / 11
					__a       :     List[Any]								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.4_5_4_5_4_5_4_5_4_5			) < 1E-6			)

					__a       :     Union[str, Any]								=	["""我能吞下玻璃""", """而不伤身体"""]
					__a       :     Optional[Any]								=	["""我    能 吞 下 玻 璃""", """而不伤身体"""]
					# S = 0, D = 5, I = 0, N = 11, CER = 5 / 11
					__a       :     List[str]								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.4_5_4_5_4_5_4_5_4_5_4_5			) < 1E-6			)

					__a       :     Union[str, Any]								=	["""我能吞下玻璃而不伤身体"""]
					__a       :     Dict								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertFalse(__UpperCamelCase  ,    0.0			)

	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     List[Any]								=	[""""""]
					__a       :     Dict								=	["""Hypothesis"""]
					with self.assertRaises(__UpperCamelCase			):
									cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)


if __name__ == "__main__":
					unittest.main()