MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / read_from_file_and_decrypt

Function read_from_file_and_decrypt

ciphers/rsa_cipher.py:93–112  ·  view source on GitHub ↗
(message_filename: str, key_filename: str)

Source from the content-addressed store, hash-verified

91
92
93def read_from_file_and_decrypt(message_filename: str, key_filename: str) -> str:
94 key_size, n, d = read_key_file(key_filename)
95 with open(message_filename) as fo:
96 content = fo.read()
97 message_length_str, block_size_str, encrypted_message = content.split("_")
98 message_length = int(message_length_str)
99 block_size = int(block_size_str)
100
101 if key_size < block_size * 8:
102 sys.exit(
103 f"ERROR: Block size is {block_size * 8} bits and key size is {key_size} "
104 "bits. The RSA cipher requires the block size to be equal to or greater "
105 "than the key size. Were the correct key file and encrypted file specified?"
106 )
107
108 encrypted_blocks = []
109 for block in encrypted_message.split(","):
110 encrypted_blocks.append(int(block))
111
112 return decrypt_message(encrypted_blocks, message_length, (n, d), block_size)
113
114
115def main() -> None:

Callers 1

mainFunction · 0.85

Calls 4

read_key_fileFunction · 0.85
splitMethod · 0.80
decrypt_messageFunction · 0.70
appendMethod · 0.45

Tested by

no test coverage detected