Python Built-In Implementation of Base64URL

This section provides a tutorial example on how to perform Base64URL encoding on binary files using the default Python implementation of the Base64URL encoding algorithm.

The Base64URL encoding algorithm bas been implemented in Python 3 language with two methods, base64.urlsafe_b64encode() and base64.urlsafe_b64encode().

However, the Python implementation of Base64URL does not remove "=" paddings. You may need to do it yourself.

Here is a sample program that allows you to perform Base64URL encoding/decoding and other variations.

# Base64_Tool.py
# Copyright (c) 2010 HerongYang.com. All Rights Reserved.
#
import sys
import base64

if (len(sys.argv)<5):
  print("Usage:")
  print("python Base64_Tool.py mode standard input output")
  exit
  
mode = sys.argv[1]
standard = sys.argv[2]
inputFile = sys.argv[3]
outputFile = sys.argv[4]

inputStream = open(inputFile, "rb")
inputBytes = inputStream.read()
inputStream.close()

outputBytes = b""
if (mode=="decode"):
  if (standard=="mime"):
    outputBytes = base64.decodebytes(inputBytes)
  elif (standard=="url"):
    outputBytes = base64.urlsafe_b64decode(inputBytes)
  else:
    outputBytes = base64.b64decode(inputBytes)
else:  
  if (standard=="mime"):
    outputBytes = base64.encodebytes(inputBytes)
  elif (standard=="url"):
    outputBytes = base64.urlsafe_b64encode(inputBytes)
  else:
    outputBytes = base64.b64encode(inputBytes)

print(outputBytes.decode(errors="replace"))

outputStream = open(outputFile, "wb")
outputStream.write(outputBytes)
outputStream.close()

Let's try it with different test cases:

herong$ python Base64_Tool.py encode url A.txt encoded.txt 
QQ==

herong$ python Base64_Tool.py decode url encoded.txt decoded.txt   
A

herong$ python Base64_Tool.py encode url lazy_dog.txt encoded.txt
VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4=

herong$ python Base64_Tool.py decode url encoded.txt decoded.txt   
The quick brown fox jumps over the lazy dog.
  
herong$ python Base64_Tool.py encode url url.txt encoded.txt 
aS5odG0_NjQ-NjM=

herong$ python Base64_Tool.py decode url encoded.txt decoded.txt   
i.htm?64>63

herong$ python Base64_Tool.py encode standard url.txt encoded.txt 
aS5odG0/NjQ+NjM=

herong$ python Base64_Tool.py decode standard encoded.txt decoded.txt   
i.htm?64>63

herong$ python Base64_Tool.py encode url icon.png encoded.txt 
iVBORw0KGgoAAAANSUhEUgAAABgAAAAY ... HwXgCfzQYOFCEQYGAAAAAElFTkSuQmCC

herong$ python Base64_Tool.py decode url encoded.txt decoded.txt   
?PNG...

As you can see, Python implementation of Base64URL works as expected. The only issue is the "=" padding, which can be removed easily.

Table of Contents

 About This Book

 Base64 Encoding

 Base64 Encoding and Decoding Tools

Base64URL - URL Safe Base64 Encoding

 Base64URL Encoding Algorithm

 Java Built-In Implementation of Base64URL

Python Built-In Implementation of Base64URL

 PHP Implementation of Base64URL Encoding

 Perl Implementation of Base64URL Encoding

 Base32 Encoding

 URL Encoding, URI Encoding, or Percent Encoding

 UUEncode Encoding

 References

 Full Version in PDF/EPUB