Base64URL Encode and Decode using Python

base64 module with custom code

from base64 import urlsafe_b64encode, urlsafe_b64decode


def base64UrlEncode(data):
    return urlsafe_b64encode(data).rstrip(b'=')


def base64UrlDecode(base64Url):
    padding = b'=' * (4 - (len(base64Url) % 4))

    return urlsafe_b64decode(base64Url + padding)


text = '<<<?!?!?>>>'
base64Url = base64UrlEncode(text.encode('utf-8')).decode('utf-8')
print(base64Url)

text = base64UrlDecode(base64Url.encode('utf-8')).decode('utf-8')
print(text)

Leave a Comment

Cancel reply

Your email address will not be published.