×

Welcome to TagMyCode

Please login or create account to add a snippet.
1
0
 
1
Language: Java
Posted by: Wilmar Giraldo
Added: Feb 10, 2017 3:08 PM
Modified: Feb 23, 2017 10:37 PM
Views: 37
Encode and Decode a String using Base64 system in Java 8
  1. import java.util.Base64;
  2.  
  3. public byte[] encode(String textToEncode) {
  4.         Base64.Encoder encoder = Base64.getEncoder();
  5.         byte[] encodedContent = encoder.encode(textToEncode.getBytes());
  6.         return encodedContent;
  7. }
  8.  
  9. public String decode(byte[] encodedContent) {
  10.         Base64.Decoder decoder = Base64.getDecoder();
  11.         byte[] decodedContent = decoder.decode(encodedContent);
  12.         String decodedString = new String(decodedContent);
  13.         return decodedString;
  14. }
  15.