base64url in java

java base64 encode example
base64url encode
byte array to base64 java
base64 decode
java program to encode and decode a string
java encode string
java encoder example
java decode

https://web.archive.org/web/20110422225659/https://en.wikipedia.org/wiki/Base64#URL_applications

talks about base64Url - Decode


a modified Base64 for URL variant exists, where no padding '=' will be used, and the '+' and '/' characters of standard Base64 are respectively replaced by '-' and '_'


I created the following function:

public static String base64UrlDecode(String input) {
    String result = null;
    BASE64Decoder decoder = new BASE64Decoder();
    try {
        result = decoder.decodeBuffer(input.replace('-','+').replace('/','_')).toString();
    }
    catch (IOException e) {
        System.out.println(e.getMessage());
    }
    return result;
}

it returns a very small set of characters that don't even resemble to the expected results. any ideas?

Java8+

import java.util.Base64;


return Base64.getUrlEncoder().encodeToString(bytes);

base64url in java, How to do Base64 encoding and decoding in Java, using the new APIs introduced in Java 8 as well as Apache Commons. Base64url was created to allow safe use of base64 encoded data in URL strings. This is done by avoiding = padding at the end and emitting - and _ instead of the usual + and / characters. Base64url is used to encode JSON Web Tokens (JWT).

Base64 encoding is part of the JDK since Java 8. URL safe encoding is also supported with java.util.Base64.getUrlEncoder(), and the "=" padding can be skipped by additionally using the java.util.Base64.Encoder.withoutPadding() method:

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public String encode(String raw) {
    return Base64.getUrlEncoder()
            .withoutPadding()
            .encodeToString(raw.getBytes(StandardCharsets.UTF_8));
}

URL Encoding/Decoding using Base64 in Java, is described in RFC 4648 § 5, where it is mentioned that the standard Base64 alphabet contains invalid characters for URLs and filenames. Base64 Encoding and Decoding in Java. Base64 is a binary-to-text encoding scheme that describes binary data in an ASCII string format by translating it into a radix-64 representation. It is primarily used to transfer content-based messages over the Internet. In this article, you'll learn how to perform Base64 encoding and decoding by using the Base64 class in Java.

With the usage of Base64 from Apache Commons, who can be configured to URL safe, I created the following function:

import org.apache.commons.codec.binary.Base64;

public static String base64UrlDecode(String input) {
    String result = null;
    Base64 decoder = new Base64(true);
    byte[] decodedBytes = decoder.decode(input);
    result = new String(decodedBytes);
    return result;
}

The constructor Base64(true) makes the decoding URL-safe.

Java Base64 Encoding and Decoding, () method as parameter then convert return value to string. talks about base64Url - Decode a modified Base64 for URL variant exists, where no padding '=' will be used, and the '+' and '/' characters of standard Base64 are respectively replaced by '-' and '_' I created the following function:

Guava now has Base64 decoding built in.

https://google.github.io/guava/releases/17.0/api/docs/com/google/common/io/BaseEncoding.html

Base64URL | Base64 Standards, How do you check whether a string is base64 encoded or not in Java? Base64URL Decode is a free online tool for decoding Base64URL values to original data. By default, it decodes Base64URL as plain text, nevertheless, it also supports binary data, such as images or other files. If you want to encode data to Base64URL string, check the Base64URL Encoder. Or, you may want to encode data to Base64 using the online convertor which supports various standards.

public static byte[] encodeUrlSafe(byte[] data) {
    byte[] encode = Base64.encode(data);
    for (int i = 0; i < encode.length; i++) {
        if (encode[i] == '+') {
            encode[i] = '-';
        } else if (encode[i] == '/') {
            encode[i] = '_';
        }
    }
    return encode;
}

public static byte[] decodeUrlSafe(byte[] data) {
    byte[] encode = Arrays.copyOf(data, data.length);
    for (int i = 0; i < encode.length; i++) {
        if (encode[i] == '-') {
            encode[i] = '+';
        } else if (encode[i] == '_') {
            encode[i] = '/';
        }
    }
    return Base64.decode(encode);
}

Basic Type Base64 Encoding and Decoding in Java, in your system has some specific limitation or identification. For example, a database persistence framework for Java objects might use Base64 encoding to encode a relatively large unique id (generally 128-bit UUIDs) into a string for use as an HTTP parameter in HTTP forms or HTTP GET URLs.

Encode a String to UTF-8 in Java, (Java) Base64url Encoding. Base64url encoding is identical to base64 encoding except it uses non-reserved URL characters (e.g. '–' is used instead of '+', and  Java 8’s Base64 API contains implementations for all the Base64 encoding and decoding variants described in the official RFC 4648. Following variants of Base64 encoding and decoding is supported - Basic: This is the standard Base64 encoding defined in the RFC 4648.

How to check whether a string is Base64 encoded or not, This page provides Java code examples for org.jose4j.base64url.Base64Url.​encode. The examples are extracted from open source Java projects. java.util.Base64 public class Base64 extends Object This class consists exclusively of static methods for obtaining encoders and decoders for the Base64 encoding scheme. The implementation of this class supports the following types of Base64 as specified in RFC 4648 and RFC 2045.

Java Base64url Encoding, This page provides Java code examples for org.jose4j.base64url.Base64Url.​decode. The examples are extracted from open source Java projects. This article explains the basics of how to do Base64 encoding and decoding in Java, using the new APIs introduced in Java 8 as well as Apache Commons. Finally, there are a few other APIs that are worth mentioning for providing similar functionality – for example java.xml.bind.DataTypeConverter with printHexBinary and parseBase64Binary .

Comments
  • You shouldn't use sun.misc.BASE64Decoder because it is internal Sun/Oracle code (not part of J2SE) and may disappear at any time. The class Base64 in Apache Commons should provide you with all the functionality you need.
  • my question is how to properly decode a base64url string in java. and thanks i'll try apache commons
  • @Benjamin, wow, I didn't realize BASE64Decoder was a Sun class, internal or otherwise. I wonder why they broke with their own naming convention. +1
  • @BenjaminMuschko Might be nice to clarify that is it specifically in Apache Commons Codec for those that don't realise it is a link in your comment! :)
  • Shouldn't it be replace('_','/')
  • If you don't like trailing '=' (padding), then use: Base64.getUrlEncoder().withoutPadding().encodeToString(str.g‌​etBytes(UTF_8))
  • @kiedysktos do you have an example sequence? Table The "URL and Filename safe" Base 64 Alphabet in does not allow / in RFC 4648 which getUrlEncoder() uses
  • EDIT: OK, I was using getEncoder() instead of getUrlEncoder(), my fault
  • The resulting string still contains equal signs. That's not URL safe, is it?
  • Use Base64.getUrlEncoder().withoutPadding().encodeToString(str.getBytes(UTF_8))
  • idle curiosity, why make it that verbose if you could just do this? return new String(new Base64(true).decode(input));
  • Warning: Trailing = characters get swallowed!