Base64URL Encoding Algorithm

This section describes the Base64URL encoding algorithm, which is a variation of the standard Base64 encoding algorithm with 2 characters 2 characters '+/' replaced by '-_'.

What Is Base64URL? - Base64URL, also called URL Safe Base64, is an encoding algorithm to encode any binary data, an stream of bytes, into a stream of 64-printable characters that are URL safe (Not using any URL special characters).

Base64URL, as specified in "RFC 4648 - The Base16, Base32, and Base64 Data Encodings" at https://tools.ietf.org/html/rfc4648, is as variation of the standard Base64 encoding specified in "RFC 1521 - MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies" at https://tools.ietf.org/html/rfc1521.

In RFC 4648, 2 modifications on Base64 encoding are suggested to define Base64URL encoding:

On top of the above 2 modifications, others also suggest 2 more rules to make Base64URL encoding even more safe.

The character set map used by Base64URL encoding is listed below:

Value Encoding  Value Encoding  Value Encoding  Value Encoding
    0 A            17 R            34 i            51 z
    1 B            18 S            35 j            52 0
    2 C            19 T            36 k            53 1
    3 D            20 U            37 l            54 2
    4 E            21 V            38 m            55 3
    5 F            22 W            39 n            56 4
    6 G            23 X            40 o            57 5
    7 H            24 Y            41 p            58 6
    8 I            25 Z            42 q            59 7
    9 J            26 a            43 r            60 8
   10 K            27 b            44 s            61 9
   11 L            28 c            45 t            62 -
   12 M            29 d            46 u            63 _
   13 N            30 e            47 v
   14 O            31 f            48 w
   15 P            32 g            49 x
   16 Q            33 h            50 y

Example 1: Input data, 1 byte, "A". Encoded output, 2 characters, "QQ"

Input Data          A
Input Bits   01000001
Padding      01000001 00000000 00000000
                   \      \      \
Bit Groups   010000 010000 000000 000000
Mapping           Q      Q      A      A
Overriding        Q      Q 

Example 2: Input data, 2 bytes, "AB". Encoded output, 3 characters, "QUI"

Input Data          A        B
Input Bits   01000001 01000010
Padding      01000001 01000010 00000000
                   \      \      \
Bit Groups   010000 010100 001000 000000
Mapping           Q      U      I      A
Overriding        Q      U      I 

Example 3: Input data, 3 bytes, "ABC". Encoded output, 4 characters, "QUJD"

Input Data          A        B        C
Input Bits   01000001 01000010 01000011
                   \      \      \
Bit Groups   010000 010100 001001 000011
Mapping           Q      U      J      D

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