saleemthp

project3

Mar 4th, 2022
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1. package edu.umsl;
  2. import java.util.*;
  3. class Main{
  4.     public static void main(String[] args) {
  5.         EncodeDecode EncodeDecode = new EncodeDecode();
  6.         String defaultInput = "SSBsb3ZlIG15IENNUCBTQ0kgMjI2MSBjbGFzcyBzbyBtdWNoLCBJIHdpc2ggSSBjb3VsZCBiZSBqdXN0IGFzIGNvb2wgYXMgbXkgcHJvZmVzc29y";
  7.         System.out.println("The default base64 input is : \n"+defaultInput);
  8.         System.out.println("The encoded version of the base64 input is: \n"+EncodeDecode.decode(defaultInput));
  9.         System.out.println("Write any string to encode into base64:");
  10.  
  11.         String userString = validateInput();
  12.         String encodedString = EncodeDecode.encode(userString);
  13.         System.out.println("The user input is : \n"+userString);
  14.         System.out.println("The encoded version of the user input is: \n"+encodedString);
  15.     }
  16.  
  17.     public static String validateInput() {
  18.         boolean validateInput = false;
  19.         Scanner input = new Scanner(System.in);
  20.         String userInput = null;
  21.         while (!validateInput) {
  22.             userInput = input.nextLine();
  23.             if (userInput.trim().isEmpty()) {
  24.                 System.out.println("Empty String Input !! Please try again !!");
  25.             } else {
  26.                 validateInput = true;
  27.             }
  28.         }
  29.         return userInput;
  30.     }
  31. }
  32.  
  33. package edu.umsl;
  34.  
  35. import java.nio.charset.*;
  36. import java.util.*;
  37. public class EncodeDecode {
  38.     public String decode(String data){
  39.         byte[] decoded = Base64.getDecoder().decode(data);
  40.         String out = new String(decoded);
  41.         return out;
  42.     }
  43.     public String encode(String str){
  44.         return Base64.getEncoder().encodeToString(str.getBytes(StandardCharsets.UTF_8));
  45.     }
  46. }
  47.  
  48.  
  49.  
  50.  
  51.  
Add Comment
Please, Sign In to add comment