← Go Back

How to Encode and Decode With base64

Python implements the base64 algorithm in the standard library:

>>> from base64 import b64encode, b64decode
>>> s = b64encode(b"Hello, world!")
>>> s
b'SGVsbG8sIHdvcmxkIQ=='
>>> b64decode(s)
b'Hello, world!'

Note that b64encode() and b64decode() work with bytes objects. To encrypt a string, encode it first:

>>> text = "Hello, world!"
>>> s = b64encode(text.encode("utf8"))
>>> s
b'SGVsbG8sIHdvcmxkIQ=='
>>> b64decode(s).decode("utf8")
'Hello, world!'


cryptography base64


🐍 You might also find interesting: