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

Function encrypt_and_write_to_file

ciphers/rsa_cipher.py:70–90  ·  view source on GitHub ↗
(
    message_filename: str,
    key_filename: str,
    message: str,
    block_size: int = DEFAULT_BLOCK_SIZE,
)

Source from the content-addressed store, hash-verified

68
69
70def encrypt_and_write_to_file(
71 message_filename: str,
72 key_filename: str,
73 message: str,
74 block_size: int = DEFAULT_BLOCK_SIZE,
75) -> str:
76 key_size, n, e = read_key_file(key_filename)
77 if key_size < block_size * 8:
78 sys.exit(
79 f"ERROR: Block size is {block_size * 8} bits and key size is {key_size} "
80 "bits. The RSA cipher requires the block size to be equal to or greater "
81 "than the key size. Either decrease the block size or use different keys."
82 )
83
84 encrypted_blocks = [str(i) for i in encrypt_message(message, (n, e), block_size)]
85
86 encrypted_content = ",".join(encrypted_blocks)
87 encrypted_content = f"{len(message)}_{block_size}_{encrypted_content}"
88 with open(message_filename, "w") as fo:
89 fo.write(encrypted_content)
90 return encrypted_content
91
92
93def read_from_file_and_decrypt(message_filename: str, key_filename: str) -> str:

Callers 1

mainFunction · 0.85

Calls 2

read_key_fileFunction · 0.85
encrypt_messageFunction · 0.70

Tested by

no test coverage detected