Convert String to Base64 in Python
Code to convert string (from utf-8) to base64 encoding and base64 back to utf-8: import base64 si = input('\nenter text:\n') print('\nyou entered\n' + si) b64 = base64.b64encode(bytes(si, 'utf-8')) print('\nbase64 is\n' + str(b64)) b64_bs = bytearray(base64.b64encode(bytes(si, 'utf-8'))) print('\nbase64 byte stream is\n' + str(list(b64_bs))) so = base64.b64decode(b64).decode('utf-8', 'ignore') print('\nstring is\n' + so) This example encodes a string to Base64 and decodes it back in Python using the base64 module. Base64 represents arbitrary bytes as plain text, which is useful when binary data must travel through text only channels such as JSON or email. Remember that Base64 is an encoding, not encryption: it provides no secrecy and grows the data by about a third. Encode for transport, not for protection.