
"""simple docstring"""


import safs







class 	lowerCamelCase__      (      safs.SaFileSystem       ):





			'''simple docstring'''





"""simple docstring"""


import os
import sys

from . import rsa_key_generator as rkg

__A 		= 128
__A 		= 256



def 					_lowerCamelCase(__UpperCamelCase					,				__UpperCamelCase = DEFAULT_BLOCK_SIZE )       ->				list[int]:
	_lowerCAmelCase			=message.encode("""ascii""" )

	_lowerCAmelCase			=[]
	for block_start in range(0					,				len(__UpperCamelCase )					,				__UpperCamelCase ):
		_lowerCAmelCase			=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:
	_lowerCAmelCase			=[]
	for block_int in block_ints:
		_lowerCAmelCase			=[]
		for i in range(block_size - 1					,				-1					,				-1 ):
			if len(__UpperCamelCase ) + i < message_length:
				_lowerCAmelCase			=block_int // (BYTE_SIZE**i)
				_lowerCAmelCase			=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]:
	_lowerCAmelCase			=[]
	_lowerCAmelCase		,							_lowerCAmelCase			=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:
	_lowerCAmelCase			=[]
	_lowerCAmelCase		,							_lowerCAmelCase			=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]:
	with open(__UpperCamelCase ) as fo:
		_lowerCAmelCase			=fo.read()
	_lowerCAmelCase		,							_lowerCAmelCase		,							_lowerCAmelCase			=content.split(""",""" )
	return (int(__UpperCamelCase ), int(__UpperCamelCase ), int(__UpperCamelCase ))



def 					_lowerCamelCase(__UpperCamelCase					,				__UpperCamelCase					,				__UpperCamelCase					,				__UpperCamelCase = DEFAULT_BLOCK_SIZE					,				)       ->				str:
	_lowerCAmelCase		,							_lowerCAmelCase		,							_lowerCAmelCase			=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 ) )

	_lowerCAmelCase			=[str(__UpperCamelCase ) for i in encrypt_message(__UpperCamelCase					,				(n, e)					,				__UpperCamelCase )]

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



def 					_lowerCamelCase(__UpperCamelCase					,				__UpperCamelCase )       ->				str:
	_lowerCAmelCase		,							_lowerCAmelCase		,							_lowerCAmelCase			=read_key_file(__UpperCamelCase )
	with open(__UpperCamelCase ) as fo:
		_lowerCAmelCase			=fo.read()
	_lowerCAmelCase		,							_lowerCAmelCase		,							_lowerCAmelCase			=content.split("""_""" )
	_lowerCAmelCase			=int(__UpperCamelCase )
	_lowerCAmelCase			=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 ) )

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

	return decrypt_message(__UpperCamelCase					,				__UpperCamelCase					,				(n, d)					,				__UpperCamelCase )



def 					_lowerCamelCase()       ->				None:
	_lowerCAmelCase			="""encrypted_file.txt"""
	_lowerCAmelCase			=input(R"""Encrypt\Decrypt [e\d]: """ )

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

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

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

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

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



