Didart

Encrypting Password

Dec 5th, 2021
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5.  
  6. public class EncryptingPassword {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         int input = Integer.parseInt(scanner.nextLine());
  11.  
  12.         String encryptedPassword = "([\\W+\\w\\d]+)>(?<firstGroup>[0-9]{3})\\|(?<secondGroup>[a-z]{3})\\|(?<thirdGroup>[A-Z]{3})\\|(?<forthGroup>[^<>]{3})<\\1";
  13.  
  14.         Pattern pattern = Pattern.compile(encryptedPassword);
  15.         Matcher matcher = null;
  16.  
  17.         for (int i = 0; i < input; i++) {
  18.             String inputText = scanner.nextLine();
  19.             matcher = pattern.matcher(inputText);
  20.  
  21.             if (matcher.find()){
  22.                 StringBuilder password = new StringBuilder();
  23.  
  24.                 String first = matcher.group("firstGroup");
  25.                 String second = matcher.group("secondGroup");
  26.                 String  third = matcher.group("thirdGroup");
  27.                 String forth = matcher.group("forthGroup");
  28.  
  29.                 if (!first.equals("0") && !second.equals("0")&&!third.equals("0")&&!forth.equals("0")) {
  30.                     password.append(first).append(second).append(third).append(forth);
  31.                     System.out.printf("Password: %s%n", password);
  32.                 }else {
  33.                     System.out.println("Try another password!");
  34.                 }
  35.             }else {
  36.                 System.out.println("Try another password!");
  37.             }
  38.         }
  39.     }
  40. }
  41.  
  42.  
  43.  
  44.  
Advertisement
Add Comment
Please, Sign In to add comment