
"""simple docstring"""


import random
import sys

from . import cryptomath_module as cryptomath

__A 		= (
    r' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`'
    r'abcdefghijklmnopqrstuvwxyz{|}~'
)



def 					_lowerCamelCase(__UpperCamelCase					,				__UpperCamelCase					,				__UpperCamelCase )       ->				None:
	if mode == "encrypt":
		if key_a == 1:
			sys.exit(
			    """The affine cipher becomes weak when key """
			    """A is set to 1. Choose different key""" )
		if key_b == 0:
			sys.exit(
			    """The affine cipher becomes weak when key """
			    """B is set to 0. Choose different key""" )
	if key_a < 0 or key_b < 0 or key_b > len(__UpperCamelCase ) - 1:
		sys.exit(
		    """Key A must be greater than 0 and key B must """
		    F'''be between 0 and {len(__UpperCamelCase ) - 1}.''' )
	if cryptomath.gcd(__UpperCamelCase					,				len(__UpperCamelCase ) ) != 1:
		sys.exit(
		    F'''Key A {key_a} and the symbol set size {len(__UpperCamelCase )} '''
		    """are not relatively prime. Choose a different key.""" )



def 					_lowerCamelCase(__UpperCamelCase					,				__UpperCamelCase )       ->				str:
	_lowerCAmelCase		,							_lowerCAmelCase			=divmod(__UpperCamelCase					,				len(__UpperCamelCase ) )
	check_keys(__UpperCamelCase					,				__UpperCamelCase					,				"""encrypt""" )
	_lowerCAmelCase			=""""""
	for symbol in message:
		if symbol in SYMBOLS:
			_lowerCAmelCase			=SYMBOLS.find(__UpperCamelCase )
			cipher_text += SYMBOLS[(sym_index * key_a + key_b) % len(__UpperCamelCase )]
		else:
			cipher_text += symbol
	return cipher_text



def 					_lowerCamelCase(__UpperCamelCase					,				__UpperCamelCase )       ->				str:
	_lowerCAmelCase		,							_lowerCAmelCase			=divmod(__UpperCamelCase					,				len(__UpperCamelCase ) )
	check_keys(__UpperCamelCase					,				__UpperCamelCase					,				"""decrypt""" )
	_lowerCAmelCase			=""""""
	_lowerCAmelCase			=cryptomath.find_mod_inverse(__UpperCamelCase					,				len(__UpperCamelCase ) )
	for symbol in message:
		if symbol in SYMBOLS:
			_lowerCAmelCase			=SYMBOLS.find(__UpperCamelCase )
			plain_text += SYMBOLS[
			    (sym_index - key_b) * mod_inverse_of_key_a % len(__UpperCamelCase )
			]
		else:
			plain_text += symbol
	return plain_text



def 					_lowerCamelCase()       ->				int:
	while True:
		_lowerCAmelCase			=random.randint(2					,				len(__UpperCamelCase ) )
		_lowerCAmelCase			=random.randint(2					,				len(__UpperCamelCase ) )
		if cryptomath.gcd(__UpperCamelCase					,				len(__UpperCamelCase ) ) == 1 and key_b % len(__UpperCamelCase ) != 0:
			return key_b * len(__UpperCamelCase ) + key_b



def 					_lowerCamelCase()       ->				None:
	_lowerCAmelCase			=input("""Enter message: """ ).strip()
	_lowerCAmelCase			=int(input("""Enter key [2000 - 9000]: """ ).strip() )
	_lowerCAmelCase			=input("""Encrypt/Decrypt [E/D]: """ ).strip().lower()

	if mode.startswith("""e""" ):
		_lowerCAmelCase			="""encrypt"""
		_lowerCAmelCase			=encrypt_message(__UpperCamelCase					,				__UpperCamelCase )
	elif mode.startswith("""d""" ):
		_lowerCAmelCase			="""decrypt"""
		_lowerCAmelCase			=decrypt_message(__UpperCamelCase					,				__UpperCamelCase )
	print(F'''\n{mode.title()}ed text: \n{translated}''' )


if __name__ == "__main__":
			import doctest

			doctest.testmod()
			# main()





"""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()



