Advertisement
Guest User

Drehraster Verschlüsselung - Matrix

a guest
Jan 28th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. /**
  2.  * Beschreiben Sie hier die Klasse main.
  3.  *
  4.  * @author (Ihr Name)
  5.  * @version (eine Versionsnummer oder ein Datum)
  6.  */
  7. public class matrixRaster
  8. {
  9.     public static void main(String[] args)
  10.     {
  11.         System.out.println("Encrypted: " + encrypt("ICHBINDERDOKTOREISENBART",6));
  12.         System.out.println("--------------------------------------------------");
  13.         System.out.println("Decrypted: " + decrypt("IDTECEONHRRBBDEAIOIRNKST   ", 6));
  14.     }
  15.  
  16.     private static String encrypt(String pText, int encryptionKey)
  17.     {
  18.         //Initialize String Builder to build encrypted String
  19.         StringBuilder sb = new StringBuilder();
  20.         //calculate rows based on encryptionKey (+1 if text length is not divisible by encryptionKey)
  21.         int zeile = (pText.length()%encryptionKey == 0 ? pText.length()/encryptionKey : pText.length()/encryptionKey+1),
  22.         spalte = encryptionKey;
  23.         //Create matrix according to columns and rows
  24.         char[][] matrix = new char[zeile][spalte];
  25.         //fill matrix
  26.         for(int i = 0; i < zeile; i++)
  27.         {
  28.             for(int j = 0; j < spalte; j++)
  29.             {
  30.                 //fill first letter of string into matrix
  31.                 matrix[i][j] = pText.charAt(0);
  32.                 //can't fill the matrix with no characters left.
  33.                 if(pText.length() <= 1)break;
  34.                 //remove first letter of string
  35.                 pText = pText.substring(1);
  36.             }
  37.         }
  38.         //print matrix
  39.         System.out.println("Matrix: ");
  40.         printMatrix(matrix);
  41.         //read matrix from top to bottom to build encrypted string
  42.         for(int i = 0; i < spalte; i++)
  43.         {
  44.             for(int j = 0; j < zeile; j++)
  45.             {
  46.                 sb.append(matrix[j][i]);
  47.             }
  48.         }
  49.         System.out.println();
  50.         //return encrypted String
  51.         return sb.toString();
  52.     }
  53.    
  54.     private static String decrypt(String pText, int decryptionKey)
  55.     {
  56.         //when swapping columns with rows while encrypting an already encrypted string, it actually decrypts said string
  57.         return encrypt(pText,(pText.length()/decryptionKey));
  58.     }
  59.  
  60.     private static void printMatrix(char[][] matrix)
  61.     {
  62.         for(int i = 0; i < matrix.length; i++)
  63.         {
  64.             for(int j = 0; j < matrix[i].length; j++)
  65.                 System.out.print(matrix[i][j]);
  66.             System.out.println();
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement