Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.umsl;
- import java.util.*;
- class Main{
- public static void main(String[] args) {
- EncodeDecode EncodeDecode = new EncodeDecode();
- String defaultInput = "SSBsb3ZlIG15IENNUCBTQ0kgMjI2MSBjbGFzcyBzbyBtdWNoLCBJIHdpc2ggSSBjb3VsZCBiZSBqdXN0IGFzIGNvb2wgYXMgbXkgcHJvZmVzc29y";
- System.out.println("The default base64 input is : \n"+defaultInput);
- System.out.println("The encoded version of the base64 input is: \n"+EncodeDecode.decode(defaultInput));
- System.out.println("Write any string to encode into base64:");
- String userString = validateInput();
- String encodedString = EncodeDecode.encode(userString);
- System.out.println("The user input is : \n"+userString);
- System.out.println("The encoded version of the user input is: \n"+encodedString);
- }
- public static String validateInput() {
- boolean validateInput = false;
- Scanner input = new Scanner(System.in);
- String userInput = null;
- while (!validateInput) {
- userInput = input.nextLine();
- if (userInput.trim().isEmpty()) {
- System.out.println("Empty String Input !! Please try again !!");
- } else {
- validateInput = true;
- }
- }
- return userInput;
- }
- }
- package edu.umsl;
- import java.nio.charset.*;
- import java.util.*;
- public class EncodeDecode {
- public String decode(String data){
- byte[] decoded = Base64.getDecoder().decode(data);
- String out = new String(decoded);
- return out;
- }
- public String encode(String str){
- return Base64.getEncoder().encodeToString(str.getBytes(StandardCharsets.UTF_8));
- }
- }
Add Comment
Please, Sign In to add comment