CaptainSpaceCat

SubstitutionCipher

Sep 9th, 2017
78
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.lang.Math;
  2. import java.util.ArrayList;
  3.  
  4. public class SubstitutionCipher {
  5.  
  6.   private String symbolBank = "!@#$%^&*()_+-={}[]|./?<>:\"";
  7.   private String letterBank = "abcdefghijklmnopqrstuvwxyz";
  8.  
  9.   private String[][] wordBank = {
  10.     {"a", "i"},
  11.     {"an", "at", "as", "go", "hi", "or", "if", "is", "in", "it", "me", "my", "am", "be", "to", "of", "so", "us", "we"},
  12.     {"and", "the", "for", "but", "are", "not", "you", "all", "any", "can", "her", "was", "one", "our", "out", "day", "get", "has", "him", "his", "how", "man", "new", "now", "old", "men", "see", "two", "way", "who", "boy", "did", "its", "let", "put", "say", "she", "too", "use", "dad", "mom", "try", "why"}
  13.   };
  14.  
  15.   public String encode(String message) {
  16.     message = message.toLowerCase();
  17.     String b;
  18.     if ((int)(Math.random()*2) == 0) {
  19.       b = symbolBank;
  20.     } else {
  21.       b = letterBank;
  22.     }
  23.     char[] bank = b.toCharArray();
  24.     String output = "";
  25.     for (int i = 0; i < message.length(); i++) {
  26.       output += "_";
  27.     }
  28.     boolean[] tracker = new boolean[message.length()];
  29.     for (int i = 0; i < message.length(); i++) {
  30.       System.out.print(i + ": ");
  31.       if (!tracker[i]) {
  32.         System.out.print("Replacing... ");
  33.         if ((int)message.charAt(i)>=65 && (int)message.charAt(i)<=122) {
  34.           char a = nextReplacement(message.charAt(i), bank);
  35.           System.out.println(message.charAt(i) + " with " + a);
  36.           for (int v = i; v < message.length(); v++) {
  37.             if (message.charAt(v) == message.charAt(i)) {
  38.               output = transcribe(output, v, a);
  39.               tracker[v] = true;
  40.             }
  41.           }
  42.         } else {
  43.           System.out.println("Failed. Non alphanumeric.");
  44.           //output = transcribe(output, i, message.charAt(i));
  45.           output = transcribe(output, i, ' ');
  46.           tracker[i] = true;
  47.         }
  48.       } else {
  49.         System.out.println("Failed. Already replaced.");
  50.       }
  51.     }
  52.     return output;
  53.   }
  54.  
  55.   public char nextReplacement(char a, char[] b) {
  56.     int index;
  57.     while (true) {
  58.       index = (int)(Math.random()*b.length);
  59.       if (b[index] != '`' && b[index] != a) {
  60.         break;
  61.       }
  62.     }
  63.     char choice = b[index];
  64.     b[index] = '`';
  65.     return choice;
  66.   }
  67.  
  68.   public String transcribe(String s, int i, char b) {
  69.     return  s.substring(0, i) + b + s.substring(i+1);
  70.   }
  71.  
  72.   public String decode(String message) {
  73.     String[][] messageBank = tabulateMessage(message);
  74.     for (int i = 0; i < messageBank.length; i++) {
  75.       for (int v = 0; v < messageBank[0].length; v++) {
  76.         System.out.print(messageBank[i][v] + " ");
  77.       }
  78.       System.out.println("");
  79.     }
  80.     for (int len = 0; len < wordBank.length; len++) {
  81.      
  82.     }
  83.     return "";
  84.   }
  85.  
  86.   public String[][] tabulateMessage(String message) {
  87.     boolean flag = false;
  88.     int wordnum = 0;
  89.     for (int i = 0; i < message.length(); i++) {
  90.       if ((int)message.charAt(i)>=65 && (int)message.charAt(i)<=122) {
  91.         flag = true;
  92.         if (i == message.length()-1) {
  93.           wordnum++;
  94.         }
  95.       } else if (flag) {
  96.         flag = false;
  97.         wordnum++;
  98.       }
  99.     }
  100.     System.out.println(wordnum + " words");
  101.     flag = false;
  102.     int[] counter = new int[wordnum];
  103.     int wordlen = 0;
  104.     int wordmax = 0;
  105.     for (int i = 0; i < message.length(); i++) {
  106.       if ((int)message.charAt(i)>=65 && (int)message.charAt(i)<=122) {
  107.         flag = true;
  108.         wordlen++;
  109.         if (i == message.length()-1) {
  110.           if (wordlen > wordmax) {
  111.             wordmax = wordlen;
  112.           }
  113.           counter[wordlen-1]++;
  114.         }
  115.       } else if (flag) {
  116.         flag = false;
  117.         if (wordlen > wordmax) {
  118.           wordmax = wordlen;
  119.         }
  120.         counter[wordlen-1]++;
  121.         wordlen=0;
  122.       }
  123.     }
  124.     int lenmax = 0;
  125.     for (int i = 0; i < counter.length; i++) {
  126.       if (counter[i] > lenmax) {
  127.         lenmax = counter[i];
  128.       }
  129.     }
  130.    
  131.     String[][] messageWords = new String[wordmax][lenmax];
  132.     int[] tracker = new int[wordmax];
  133.     flag = false;
  134.     wordlen = 0;
  135.     for (int i = 0; i < message.length(); i++) {
  136.       if ((int)message.charAt(i)>=65 && (int)message.charAt(i)<=122) {
  137.         flag = true;
  138.         wordlen++;
  139.         if (i == message.length()-1) {
  140.           messageWords[wordlen-1][tracker[wordlen-1]] = message.substring(i-wordlen+1);
  141.           tracker[wordlen-1]++;
  142.         }
  143.       } else if (flag) {
  144.         messageWords[wordlen-1][tracker[wordlen-1]] = message.substring(i-wordlen, i);
  145.         tracker[wordlen-1]++;
  146.         flag = false;
  147.         wordlen=0;
  148.       }
  149.     }
  150.     return messageWords;
  151.   }
  152.  
  153.   public void error(String text) {
  154.     throw new IllegalArgumentException(text);
  155.   }
  156. }
RAW Paste Data Copied