""" Encoding a message to the 8-bits blocks. Decoding a message from the 8-bits blocks. """ import base64 def encode(message: str): """ Encode the UTF-8 string into base64 and converts bytes to a list which element represent a byte in a binary. """ encoded = base64.b64encode(message.encode('utf-8')) return map(lambda c: format(c, '08b'), encoded) def decode(message: list): """ Decode the list of binary string into bytes to decode this with base64 to get the UTF-8 string. """ decoded_bytes = bytes(map(lambda c: int(c, 2), message)) return base64.b64decode(decoded_bytes).decode('utf-8') def encode_bytes(byte_list: list): """ Encode the UTF-8 string into base64 and converts bytes to a list which element represent a byte in a binary. """ byte_list = [int(b, 2) for b in byte_list] byte_data = bytes(byte_list) return base64.b64encode(byte_data) def decode_bytes(message: str): """ Decode the list of binary string into bytes to decode this with base64 to get the UTF-8 string. """ decoded = base64.b64decode(message) byte_list = list(decoded) return map(lambda b: format(b, '08b'), byte_list)