base64 url safe removes =

The following code(using commons codec Base64):

  byte[] a = Hex.decodeHex("9349c513ed080dab".toCharArray());
  System.out.println(Base64.encodeBase64URLSafeString(a));
  System.out.println(Base64.encodeBase64String(a));

gives the following output:

k0nFE-0IDas         //should be k0nFE-0IDas=
k0nFE+0IDas=

Base64.encodeBase64URLSafeString(a) returns k0nFE-0IDas instead of k0nFE-0IDas=. Why is this happening?

Jon Skeet
people
quotationmark

Why is this happening?

Because that's what it's documented to do:

Note: no padding is added.

The = characters at the end of a base64 string are called padding. They're used to make sure that the final string's length is a multiple of 4 characters - but they're not really required, in terms of information theory, so it's reasonable to remove them so long as you then convert the data back to binary using a method which doesn't expect padding. The Apache Codec Base64 class claims it transparently handles both regular and URL-safe base64, so presumably does handle a lack of padding.

people

See more on this question at Stackoverflow