Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.79 KB | None | 0 0
  1. public class MiniEncryption {
  2.  
  3.     /**
  4.      * @param args the command line arguments
  5.      */
  6.     private static Scanner input = new Scanner(System.in);
  7.  
  8.     public static void main(String[] args) {
  9.         String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ,.!";
  10.         String userInput = "";
  11.  
  12.         int i = 0;
  13.         while (userInput.length() < characters.length()) {
  14.             System.out.format("Map for %s ", characters.charAt(i));
  15.             char charainput = input.nextLine().toUpperCase().charAt(0);
  16.             if (!checkDuplicate(userInput, charainput) && checkValues(characters, charainput)) {
  17.                 userInput += charainput;
  18.                 i++;
  19.             } else {
  20.                 System.out.println("wrong value please try again");
  21.             }
  22.         }
  23.         while (true) {
  24.             System.out.println("Mapping complete enter encrypted word");
  25.             String word = input.nextLine();
  26.             String encrypted = encryptWord(characters, userInput, word);
  27.             String decrypted = decryptword(characters, userInput, word);
  28.  
  29.             System.out.println("the word encrypted is " + encrypted);
  30.             System.out.println("the word decrypted is " + decrypted);
  31.            
  32.              System.out.println("Do you wish to try again? (y/n)");
  33.              char answer = input.nextLine().toUpperCase().charAt(0);
  34.              if (answer == 'N') {
  35.                 System.out.println("thank you");
  36.                 break;
  37.             }
  38.         }
  39.     }
  40.  
  41.     public static boolean checkDuplicate(String Usercharacters, char input) {
  42.        
  43.         for (int i = 0; i < Usercharacters.length(); i++) {
  44.             if (Usercharacters.contains(Character.toString(input))) {
  45.                 return true;
  46.             }
  47.         }
  48.         return false;
  49.  
  50.     }
  51.  
  52.     public static boolean checkValues(String Mapcharacters, char input) {
  53.         for (int i = 0; i < Mapcharacters.length(); i++) {
  54.             if (!Mapcharacters.contains(Character.toString(input))) {
  55.                 return true;
  56.             }
  57.         }
  58.         return false;
  59.  
  60.     }
  61.  
  62.     public static String encryptWord(String characters, String usercharacters, String word) {
  63.         String encryptword = "";
  64.         for (int i = 0; i < word.length(); i++) {
  65.             int index = characters.indexOf(word.charAt(i));
  66.             encryptword += usercharacters.charAt(index);
  67.         }
  68.         return encryptword;
  69.     }
  70.  
  71.     public static String decryptword(String characters, String usercharacters, String word) {
  72.         String decryptword = "";
  73.         for (int i = 0; i < word.length(); i++) {
  74.             int index = usercharacters.indexOf(word.charAt(i));
  75.             decryptword += characters.charAt(index);
  76.         }
  77.         return decryptword;
  78.     }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement