Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.88 KB | None | 0 0
  1. import java.io.*;
  2. import javax.swing.*;
  3. import java.util.*;
  4.  
  5. public class Main {
  6.     int keyLength;
  7.     String key;
  8.     List<Block> blocks;
  9.  
  10.     public static void main(String[] args){
  11.         new Main().runProgram();
  12.     }
  13.     public void runProgram(){
  14.         Scanner in = new Scanner(System.in);
  15.         System.out.println("Введите ключ:");
  16.         key = in.nextLine();
  17.         keyLength = key.length();
  18. //        makeTablesForCrypt();
  19. //        crypt();
  20.         makeTablesForDecrypt();
  21.     }
  22.     public void crypt(){
  23.         int[] indexes = getSortedIndexes(key.toCharArray());
  24. //        File file = chooseFile();
  25.         try(BufferedWriter out = new BufferedWriter(new FileWriter(System.getProperty("user.home") + "/Desktop" + "/криптограммаслона.txt"))) {
  26.             for (Block block : blocks) {
  27.                 for (int i = 0; i < indexes.length; i++) {
  28.                     for (String line : block.getBlock()) {
  29.                         if ((line.length() - 1) >= indexes[i])
  30.                             out.write(line.charAt(indexes[i]));
  31.                     }
  32.                 }
  33.             }
  34.         }catch(IOException ex){
  35.             ex.printStackTrace();
  36.         }
  37.     }
  38.  
  39.     public void makeTablesForCrypt(){
  40.         blocks = new ArrayList<>();
  41.         String buffer = readFromFile(chooseFile());
  42.  
  43.         for(int i = 0; i < buffer.length();){
  44.             List<String> currentBlock = new ArrayList<>();
  45.             // формируем блок;
  46.             for(int lengthCurrentLine = keyLength; lengthCurrentLine > 0 && i < buffer.length(); lengthCurrentLine--) {
  47.                 StringBuilder currentLineBlock = new StringBuilder();
  48.                 for (int j = 0; j < lengthCurrentLine && i < buffer.length(); i++) {
  49.                     if(isGoodChar(buffer.charAt(i))) {
  50.                         currentLineBlock.append(buffer.charAt(i));
  51.                         j++;
  52.                     }
  53.                 }
  54.                 currentBlock.add(currentLineBlock.toString());
  55.             }
  56.             //
  57.             Block block = new Block(currentBlock);
  58.             block.printBlock();
  59.             blocks.add(block);
  60.         }
  61.     }
  62.  
  63.     public void makeTablesForDecrypt(){
  64.         blocks = new ArrayList<>();
  65.         String buffer = readFromFile(chooseFile());
  66.         int[] indexes = getSortedIndexes(key.toCharArray());
  67.  
  68.         for(int i = 0; i < buffer.length();){
  69.             List<StringBuilder> currentBlock = getEmptyTempBlock(keyLength);
  70.             for(int indexColumn : indexes){
  71.                 for(int indexLine = 0; indexLine < keyLength - indexColumn && i < buffer.length(); indexLine++, i++)
  72.                     currentBlock.get(indexLine).setCharAt(indexColumn, buffer.charAt(i));
  73.             }
  74.             List<String> block = new ArrayList<>();
  75.             for(StringBuilder line : currentBlock){
  76.                 block.add(line.toString());
  77.             }
  78.             Block temp = new Block(block);
  79.             temp.printBlock();
  80.             blocks.add(temp);
  81.  
  82.         }
  83.     }
  84.  
  85.     public List<StringBuilder> getEmptyTempBlock(int keyLength){
  86.         List<StringBuilder> result = new ArrayList<>();
  87.         int key = keyLength;
  88.         for(int i = 0; i < keyLength ; i++, key--) {
  89.             StringBuilder currentLine = new StringBuilder();
  90.             for (int lengthCurrentLine = key; lengthCurrentLine > 0; lengthCurrentLine--){
  91.                 currentLine.append("#");
  92.             }
  93.             result.add(currentLine);
  94.         }
  95.         return result;
  96.     }
  97.  
  98.     public static String readFromFile(File file){
  99.         StringBuilder result = new StringBuilder();
  100.         try(BufferedReader in = new BufferedReader(new FileReader(file))){
  101.             String currentLine;
  102.             while((currentLine = in.readLine()) != null)
  103.                 result.append(currentLine);
  104.             return result.toString().toUpperCase();
  105.         }catch(IOException ex){
  106.             ex.printStackTrace();
  107.         }
  108.         return null;
  109.     }
  110.  
  111.     public File chooseFile() {
  112.         JFileChooser chooser = new JFileChooser();
  113.         chooser.showOpenDialog(null);
  114.         return chooser.getSelectedFile();
  115.     }
  116.  
  117.     public static boolean isGoodChar(char value){
  118.         if((value >= 'А' && value <= 'Я')
  119.                 || (value >= '0' && value <= '9')
  120.                 || (value == 'Ё')){
  121.             return true;
  122.         }
  123.         return false;
  124.     }
  125.  
  126.     int[] getSortedIndexes(char[] key){
  127.         ArrayList<Pair> pairs = new ArrayList<>();
  128.         for(int i = 0; i < key.length; i++)
  129.             pairs.add(new Pair(i, key[i]));
  130.         Collections.sort(pairs);
  131.         int[] result = new int[pairs.size()];
  132.         int index = 0;
  133.         for(Pair pair : pairs)
  134.             result[index++] = pair.getIndex();
  135.         return result;
  136.     }
  137.  
  138.     class Block {
  139.         List<String> block;
  140.  
  141.         Block(List<String> block){
  142.             this.block = block;
  143.         }
  144.  
  145.         public List<String> getBlock(){
  146.             return block;
  147.         }
  148.  
  149.         public char getCharAt(int i, int j){
  150.             return block.get(i).charAt(j);
  151.         }
  152.  
  153.         public void printBlock(){
  154.             for(String line : block){
  155.                 StringBuilder currentLine = new StringBuilder();
  156.                 for(int i = 0; i < line.length(); i++){
  157.                     currentLine.append(line.charAt(i) + " ");
  158.                 }
  159.                 System.out.println(currentLine.toString());
  160.             }
  161.         }
  162.     }
  163.  
  164.     class Pair implements Comparable<Pair> {
  165.         int i;
  166.         char c;
  167.         public Pair(int i, char c){
  168.             this.i = i;
  169.             this.c = c;
  170.         }
  171.         int getIndex(){
  172.             return i;
  173.         }
  174.         @Override
  175.         public int compareTo(Pair o) {
  176.             return Character.valueOf(this.c).compareTo(Character.valueOf(o.c));
  177.         }
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement