Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.12 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.lang.reflect.Array;
  4. import java.util.Arrays;
  5. import java.util.stream.IntStream;
  6.  
  7. public class pod_1 {
  8.  
  9. static String encryption(String s, String k)
  10. {
  11.     //geting length of msg and key
  12.     int temp1 = s.length() % 8;
  13.     int mising_chars = 8 - temp1;
  14.     for(int i = 0; i < mising_chars; i++)
  15.     {
  16.         s = s + " ";
  17.     }
  18.     //putting message into char[]
  19.     char[] msg_to_encript = s.toCharArray();
  20.     char[] key = k.toCharArray();
  21.  
  22.     //setting up columns and rows size
  23.     int columns = 8;
  24.     int rows = 0;
  25.     rows = (msg_to_encript.length / 8);
  26.  
  27.     //creating matrixes
  28.     char[][] matrix = new char[rows][columns];
  29.     char[][] sorted_matrix = new char[columns][rows];
  30.  
  31.     //putting key into matrix
  32.     for(int i = 0; i < columns; i++)
  33.     {
  34.         matrix[0][i] = key[i];
  35.     }
  36.  
  37.     //putting message into matrix
  38.     int temp2 = 0;
  39.     int c2 = 0;
  40.     for(int r = 1; r < rows; r++)
  41.     {
  42.         for(int c = 0; c < columns; c++)
  43.         {
  44.             matrix[r][c] = msg_to_encript[(c + c2)];
  45.             temp2 = c;
  46.         }
  47.         c2 = c2 + temp2 + 1;
  48.     }
  49.  
  50.     //printing matrix
  51.     for(int r = 0; r < rows; r++)
  52.     {
  53.         for(int c = 0; c < columns; c++)
  54.         {
  55.             System.out.print(matrix[r][c]);
  56.         }
  57.         System.out.println();
  58.     }
  59.     System.out.println();
  60.  
  61.     //rotating matrix
  62.     for(int r = 0; r < rows; r++)
  63.     {
  64.         for(int c = 0; c < columns; c++)
  65.         {
  66.             sorted_matrix[c][r] = matrix[r][c];
  67.         }
  68.     }
  69.  
  70.     //printing sorted matrix
  71. //    for(int c = 0; c < columns; c++)
  72. //    {
  73. //        for(int r = 0; r < rows; r++)
  74. //        {
  75. //            System.out.print(sorted_matrix[c][r]);
  76. //        }
  77. //        System.out.println();
  78. //    }
  79. //    System.out.println();
  80.  
  81.     Arrays.sort(sorted_matrix, (a, b) -> Integer.compare((int)a[0], (int)b[0]));
  82.  
  83. //    //printing sorted matrix
  84. //    for(int c = 0; c < columns; c++)
  85. //    {
  86. //        for(int r = 0; r < rows; r++)
  87. //        {
  88. //            System.out.print(sorted_matrix[c][r]);
  89. //        }
  90. //        System.out.println();
  91. //    }
  92. //    System.out.println();
  93.  
  94.     //printing sorted matrix
  95.  
  96.  
  97.     //creating encrypted message basing on sorted matrix
  98.     String encrypted_msg = "";
  99.     for(int r = 0; r < columns; r++)
  100.     {
  101.         for(int c = 1; c < rows; c++)
  102.         {
  103.             encrypted_msg += String.valueOf(sorted_matrix[r][c]);
  104.         }
  105.     }
  106.  
  107. //    System.out.println(encrypted_msg);
  108. //    System.out.println();
  109.     return encrypted_msg;
  110. }
  111.  
  112. static String decryption(String s, String k)
  113. {
  114. //    System.out.println(s);
  115. //    System.out.println();
  116.     char[] msg_to_decript = s.toCharArray();
  117.     char[] key = k.toCharArray();
  118.     char[] helper_key = {'0', '1', '2', '3', '4', '5', '6', '7'};
  119.     int columns = 8;
  120.     int rows = (msg_to_decript.length / 8);
  121.  
  122.     char[][] matrix = new char[rows+1][columns];
  123.     char[][] rotated_matrix = new char[columns][rows + 1];
  124.     char[][] decrypted_matrix = new char[columns][rows+1];
  125.  
  126.     //putting key into matrix
  127.     for(int i = 0; i < columns; i++)
  128.     {
  129.         matrix[0][i] = helper_key[i];
  130.     }
  131.  
  132.     //putting message into matrix
  133.     int temp = 0;
  134.     int r2 = 0;
  135.     for(int c = 0; c < columns; c++)
  136.     {
  137.         for (int r = 1; r <= rows; r++)
  138.         {
  139.             matrix[r][c] = msg_to_decript[r-1+r2];
  140.             temp = r;
  141.         }
  142.         r2 = r2+ temp;
  143.     }
  144.  
  145. //    //printing matrix
  146. //    for(int c = 0; c < columns; c++)
  147. //    {
  148. //        for(int r = 0; r <= rows; r++)
  149. //        {
  150. //            System.out.print(matrix[r][c]);
  151. //        }
  152. //        System.out.println();
  153. //    }
  154.  
  155.     //rotating matrix
  156.     for(int c = 0; c < columns; c++)
  157.     {
  158.         for(int r = 0; r <= rows; r++)
  159.         {
  160.             rotated_matrix[c][r] = matrix[r][c];
  161.         }
  162.     }
  163.  
  164.     for(int i = 0; i < key.length; i++)
  165.     {
  166.         for(int c = 0; c < columns; c++)
  167.         {
  168.             if(key[i] == rotated_matrix[c][0])
  169.             {
  170.                 for(int r = 0; r <= rows; r++)
  171.                 {
  172.                     decrypted_matrix[c][r] = rotated_matrix[c][r];
  173.                 }
  174.             }
  175.         }
  176.     }
  177.  
  178.  
  179.     for(int r = 0; r <= rows; r++)
  180.     {
  181.         for(int c = 0; c < columns; c++)
  182.         {
  183.             System.out.print(decrypted_matrix[c][r]);
  184.         }
  185.         System.out.println();
  186.     }
  187.  
  188.     String decrypted_msg = "";
  189.     for(int i = 0; i < key.length; i++)
  190.     {
  191.         for(int c = 0; c < columns; c++)
  192.         {
  193.             if(key[i] == decrypted_matrix[c][0])
  194.             {
  195.                 for(int r = 0; r <= rows; r++)
  196.                 {
  197.                     decrypted_msg += String.valueOf(decrypted_matrix[c][r]);
  198.                 }
  199.             }
  200.         }
  201.     }
  202.  
  203.     System.out.println();
  204.     System.out.println(decrypted_msg);
  205.  
  206.     String ss = "return kurwo";
  207.     return ss;
  208. }
  209.  
  210. static void gui()
  211. {
  212.     JFrame window = new JFrame();
  213.     window.setTitle("Derulo Chiper Machine");
  214.     window.setSize(400, 220);
  215.     window.setLayout(new BorderLayout());
  216.     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  217.  
  218.  
  219.     window.setVisible(true);
  220.  
  221. }
  222.  
  223. public static void main(String args[])
  224. {
  225.     decryption(encryption("pszczoły są żółte i fajne", "53742601"), "53742601");
  226. }
  227.  
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement