Matt23

SImple encryption

Dec 19th, 2015
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. public class main
  2. {  
  3.     public static void main(String[] args)
  4.     {
  5.         encryption("ALGORYTM_PRZESTAWIENIOWY", true);
  6.         encryption("BTLLTU_ĘL_EOYPM_ĄPJZLCYNDREOKYLI_ZMFO_ĄGJY_Ó_N_DEWFWGISYSII_ŁEI", false);
  7.     }
  8.    
  9.     public static void encryption(String toEncrypt, boolean action)
  10.     {
  11.         int n = (int) Math.ceil(Math.sqrt(toEncrypt.length()));
  12.        
  13.         char[][] tab = new char[n][n];
  14.         char[][] tab2 = new char[n][n];
  15.        
  16.         for (int i = 0; i < n; i++)
  17.             for (int j = 0; j < n; j++)
  18.             {
  19.                 if (toEncrypt.length() > 0)
  20.                 {
  21.                     tab[i][j] = toEncrypt.charAt(0);
  22.                     toEncrypt = toEncrypt.substring(1);
  23.                 }
  24.                 else
  25.                     tab[i][j] = '_';
  26.             }
  27.        
  28.         if (action)
  29.             System.out.println("Tablica znaków przed zaszyfrowaniem:");
  30.         else
  31.             System.out.println("Tablica znaków przed odszyfrowaniem:");
  32.        
  33.         printArray(tab);
  34.                
  35.         for (int i = 0; i < n; i++)
  36.             for (int j = 0; j < n; j++)
  37.                 if (action)
  38.                     tab2[i][j] = tab[j][i];
  39.                 else
  40.                     tab2[j][i] = tab[i][j];
  41.        
  42.         if (action)
  43.             System.out.println("Tablica znaków po zaszyfrowaniu:");
  44.         else
  45.             System.out.println("Tablica znaków po odszyfrowaniu:");
  46.        
  47.         printArray(tab2);
  48.     }
  49.    
  50.     public static void printArray(char[][] array)
  51.     {
  52.         for (int i = 0; i < array.length; i++)
  53.         {
  54.             for (int j = 0; j < array.length; j++)
  55.             {
  56.                 System.out.print(array[i][j]);
  57.                 if (j != array.length - 1)
  58.                     System.out.print(" ");
  59.             }
  60.             System.out.println();
  61.         }
  62.         System.out.println();
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment