© Prabath Siriwardena 2020
P. SiriwardenaAdvanced API Securityhttps://doi.org/10.1007/978-1-4842-2050-4_20

Base64 URL Encoding

Prabath Siriwardena1 
(1)
San Jose, CA, USA
 

Base64 encoding defines how to represent binary data in an ASCII string format. The objective of base64 encoding is to transmit binary data such as keys or digital certificates in a printable format. This type of encoding is needed if these objects are transported as part of an email body, a web page, an XML document, or a JSON document.

To do base64 encoding, first the binary data are grouped into 24-bit groups. Then each 24-bit group is divided into four 6-bit groups. Now, a printable character can represent each 6-bit group based on its bit value in decimal (see Figure E-1). For example, the decimal value of the 6-bit group 000111 is 7. As per Figure E-1, the character H represents this 6-bit group. Apart from the characters shown in Figure E-1, the character = is used to specify a special processing function, which is to pad. If the length of the original binary data is not an exact multiple of 24, then we need padding. Let’s say the length is 232, which is not a multiple of 24. Now we need to pad this binary data to make its length equal to the very next multiple of the 24, which is 240. In other words, we need to pad this binary data by 8 to make its length 240. In this case, padding is done by adding eight 0s to the end of the binary data. Now, when we divide this 240 bits by 6 to build 6-bit groups, the last 6-bit group will be of all zeros—and this complete group will be represented by the padding character =.
../images/323855_2_En_20_Chapter/323855_2_En_20_Fig1_HTML.jpg
Figure E-1

Base64 encoding

The following example shows how to base64-encode/decode binary data with Java 8. The java.util.Base64 class was introduced from Java 8.
byte[] binaryData = // load binary data to this variable
// encode
String encodedString = Base64.getEncoder().encodeToString(binaryData);
// decode
binary[] decodedBinary = Base64.getDecoder().decode(encodedString);

One issue with base64 encoding is that it does not work quite well with URLs. The + and / characters in base64 encoding (see Figure E-1) have a special meaning when used within a URL. If we try to send a base64-encoded image as a URL query parameter and if the base64-encoded string carries any of the preceding two characters, then the browser will interpret the URL in a wrong way. The base64url encoding was introduced to address this problem. The way base64url encoding works is exactly the same as base64 encoding other than two exceptions: the character - is used in base64url encoding instead of the character + in base64 encoding, and the character _ is used in base64url encoding instead of the character / in base64 encoding.

The following example shows how to base64url-encode/decode binary data with Java 8. The java.util.Base64 class was introduced from Java 8.
byte[] binaryData = // load binary data to this variable
// encode
String encodedString = Base64.getUrlEncoder().encodeToString(binaryData);
// decode
binary[] decodedBinary = Base64.getUrlEncoder().decode(encodedString);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.103.8