How To Encode In Base64 Java Code Example

Snippet 1

  //For Strings:
  
  //Encode:
  public static String encode(String input) {

      return Base64.getEncoder().encodeToString(input.getBytes());

  }

  //Decode:
  public static String decode(String input) {

      byte[] decodedBytes = Base64.getDecoder().decode(input);
      return new String(decodedBytes);

  }
  
//For Urls
  //Encode:
  public static String encodeURL(String input) {

      return Base64.getUrlEncoder().encodeToString(input.getBytes());

  }

  //Decode:
  public static String decodeURL(String input) {

      byte[] decodedBytes = Base64.getUrlDecoder().decode(input);
      return new String(decodedBytes);

  } 

Copyright © Code Fetcher 2020

 

 

Published

Leave a comment

Your email address will not be published. Required fields are marked *