Java Util Base64 Encode Example

How to do Base64 Encoding in Java

Base64 is a binary-to-text encoding scheme that represents binary data in a printable ASCII string format.

In this article, you'll learn how to Base64 encode any arbitrary text in Java using the Base64 API that was introduced in Java 8.

The Java 8 Base64 API supports three different variants of Base64 encoding as described in RFC 4648 and RFC 2045.

  • Basic: This is the standard Base64 encoding defined in RFC 4648. The output contains characters from the set A-Z, a-z, 0-9, + and /. The decoder rejects data that contains characters outside this set.

  • URL and Filename Safe: It is same as the Basic Base64 encoding except that + is replaced by - and / is replaced by _ to make the output URL and filename safe. The decoder rejects data that contains characters outside A-Za-z0-9-_

  • MIME: The MIME variant uses the Basic Base64 alphabet which contains characters from the set A-Z, a-z, 0-9, + and /. The encoded output is organized into lines of no more than 76 characters. Each line (except the last line) is separated from the next line via a carriage return (\r) followed by a linefeed (\n). The decoder ignores all line separators or other characters not found in the basic base64 alphabet.

Java 8 Base64 Encoding Example

                                  import                  java.io.                                    UnsupportedEncodingException                  ;                  import                  java.nio.charset.                                    StandardCharsets                  ;                  import                  java.util.                                    Base64                  ;                  import                  java.util.                                    Scanner                  ;                  class                  Base64EncodeExample                  {                  private                  static                  String                  base64Encode                  (                  String                  value)                  {                  try                  {                  return                  Base64                  .                  getEncoder                  (                  )                  .                  encodeToString                  (value.                  getBytes                  (                  StandardCharsets                  .UTF_8.                  toString                  (                  )                  )                  )                  ;                  }                  catch                  (                  UnsupportedEncodingException                  ex)                  {                  throw                  new                  RuntimeException                  (ex)                  ;                  }                  }                  public                  static                  void                  main                  (                  String                  [                  ]                  args)                  {                  System                  .out.                  println                  (                  "Enter a value to Encode : "                  )                  ;                  Scanner                  keyboard                  =                  new                  Scanner                  (                  System                  .in)                  ;                  String                  input                  =                  keyboard.                  nextLine                  (                  )                  ;                  System                  .out.                  println                  (                  base64Encode                  (input)                  )                  ;                  keyboard.                  close                  (                  )                  ;                  }                  }                              
                $ javac Base64EncodeExample.java  $ java Base64EncodeExample Enter a value to Encode                  :                  hello:world                  aGVsbG86d29ybGQ                  =                              

Java 8 Base64 URL and Filename safe Encoding

The basic Base64.getEncoder() function provided by the Base64 API uses the standard Base64 alphabet that contains characters A-Z, a-z, 0-9, +, and /.

Since + and / characters are not URL and filename safe, The RFC 4648 defines another variant of Base64 encoding whose output is URL and Filename safe. This variant replaces + with minus (-) and / with underscore (_). Java contains an implementation of this variant as well. You can use it like so -

                                  private                  static                  String                  base64UrlEncode                  (                  String                  value)                  {                  try                  {                  return                  Base64                  .                  getUrlEncoder                  (                  )                  .                  encodeToString                  (value.                  getBytes                  (                  StandardCharsets                  .UTF_8.                  toString                  (                  )                  )                  )                  ;                  }                  catch                  (                  UnsupportedEncodingException                  ex)                  {                  throw                  new                  RuntimeException                  (ex)                  ;                  }                  }                              

Java 8 Base64 MIME Encoding

The MIME variant of Base64 encoding organizes the output into lines of no more than 76 characters. Each line (except the last line) is separated from the next line via a carriage return (\r) followed by a linefeed (\n). You can use the MIME implementation like this -

                                  private                  static                  String                  base64MimeEncode                  (                  String                  value)                  {                  try                  {                  return                  Base64                  .                  getMimeEncoder                  (                  )                  .                  encodeToString                  (value.                  getBytes                  (                  StandardCharsets                  .UTF_8.                  toString                  (                  )                  )                  )                  ;                  }                  catch                  (                  UnsupportedEncodingException                  ex)                  {                  throw                  new                  RuntimeException                  (ex)                  ;                  }                  }                              

Java 8 Base64 Encoding without Padding

The Base64 encoding algorithm organizes the input into 24-bit groups (three 8-bit bytes), and then represents each 24-bit group by four Base64 alphabets (One Base64 alphabet is represented by 6-bits). When the input has fewer than 24-bits at the end, then zero-bits are added to make it a multiple of 6. After that, one or two pad characters are appended to the output depending on whether the last group contained two bytes or only one byte.

It is always advisable to use padding. But if you need to skip the padding, perhaps because the encoded data will never be decoded, then you can do so like this -

                                  private                  static                  String                  base64EncodeWithoutPadding                  (                  String                  value)                  {                  try                  {                  return                  Base64                  .                  getEncoder                  (                  )                  .                  withoutPadding                  (                  )                  .                  encodeToString                  (value.                  getBytes                  (                  StandardCharsets                  .UTF_8.                  toString                  (                  )                  )                  )                  ;                  }                  catch                  (                  UnsupportedEncodingException                  ex)                  {                  throw                  new                  RuntimeException                  (ex)                  ;                  }                  }                              

Also Read: Java Base64 Decode Example

References

  • Java 8 Base64 API

rosenbergonces1993.blogspot.com

Source: https://www.base64encoder.io/java/

0 Response to "Java Util Base64 Encode Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel