Advertisement
Guest User

Encrypting Block Algorithm

a guest
Mar 29th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8.     public static String message;
  9. public static String key;
  10. public static int min,max;
  11. public static StringBuilder encrypted = new StringBuilder();
  12.     public static void main(String[] args) {
  13.         Scanner sc = new Scanner(System.in);
  14.         getInput(sc);
  15.         testForKeyLength();
  16.         testForRepeatingNumbers();
  17.         message = addIntervalsIfNeeded(message);
  18.         min = getMin();
  19.         max = getMax();
  20.         int i = 0, j=8;
  21.         String block;
  22.         while(true){
  23.             try {
  24.                 block = message.substring(i, j);
  25.             } catch(IndexOutOfBoundsException e){
  26.                 break;
  27.             }
  28.             i += 8;
  29.             j += 8;
  30.         for(int y=0; y<block.length(); y++) {
  31.             int index = getIndex((char) min);
  32.             encrypted.append(block.charAt(index));
  33.             min++;
  34.             if(min>max){
  35.                 min= getMin();
  36.             }
  37.         }
  38.         }
  39.         System.out.println(encrypted.toString());
  40.  
  41.  
  42.     }
  43.  
  44.     private static void getInput(Scanner sc) {
  45.         System.out.println("Enter message:");
  46.         message = sc.nextLine();
  47.         System.out.println("Enter key:");
  48.         key = sc.nextLine();
  49.     }
  50.  
  51.     private static void testForKeyLength() {
  52.         if(key.length() != 8){
  53.             System.out.println("ERROR KEY MUST BE 8 SYMBOLS!");
  54.             System.exit(0);
  55.         }
  56.     }
  57.  
  58.     private static void testForRepeatingNumbers() {
  59.         LinkedHashMap<Character,Integer> map = new LinkedHashMap<Character,Integer>();
  60.         for(char ch: key.toCharArray()){
  61.             if(map.containsKey(ch)){
  62.                 map.put(ch,map.get(ch)+1);
  63.                 continue;
  64.             }
  65.             map.put(ch,1);
  66.         }
  67.  
  68.         for(Map.Entry<Character,Integer> entry : map.entrySet()){
  69.             if(entry.getValue()>=2){
  70.                 System.out.println("ERROR KEY MUST BE WITH UNIQUE NUMBERS!");
  71.                 System.exit(0);
  72.             }
  73.         }
  74. }
  75.  
  76.     private static String addIntervalsIfNeeded(String message) {
  77.         if(message.length()%8!=0) {
  78.             StringBuilder temp = new StringBuilder(message);
  79.             int numIntervals = message.length()%8;
  80.             temp.append(" ".repeat(Math.max(0, numIntervals)));
  81.             message = temp.toString();
  82.         }
  83.         return message;
  84.     }
  85.  
  86.     public static int getMin(){
  87.         int min = Integer.MAX_VALUE;
  88.     for(int i=0; i<key.length(); i++){
  89.         if(key.charAt(i) - '0' < min){
  90.             min = key.charAt(i) - '0';
  91.         }
  92.     }
  93.     return min;
  94. }
  95.  
  96.  
  97.  
  98.     public static int getMax(){
  99.         int min = Integer.MIN_VALUE;
  100.         for(int i=0; i<key.length(); i++){
  101.             if(key.charAt(i) - '0' > max){
  102.                 max = key.charAt(i) - '0';
  103.             }
  104.         }
  105.         return max;
  106.     }
  107.     public static int getIndex(char ch){
  108.         for(int i=0; i<key.length(); i++){
  109.             if(key.charAt(i)-48==ch){
  110.                 return i;
  111.             }
  112.         }
  113.         return -1;
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement