1//For Strings:
2
3 //Encode:
4 public static String encode(String input) {
5
6 return Base64.getEncoder().encodeToString(input.getBytes());
7
8 }
9
10 //Decode:
11 public static String decode(String input) {
12
13 byte[] decodedBytes = Base64.getDecoder().decode(input);
14 return new String(decodedBytes);
15
16 }
17
18//For Urls
19 //Encode:
20 public static String encodeURL(String input) {
21
22 return Base64.getUrlEncoder().encodeToString(input.getBytes());
23
24 }
25
26 //Decode:
27 public static String decodeURL(String input) {
28
29 byte[] decodedBytes = Base64.getUrlDecoder().decode(input);
30 return new String(decodedBytes);
31
32 }
1String originalInput = "test input";
2String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());
3