Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.Math;
- import java.util.ArrayList;
- public class SubstitutionCipher {
- private String symbolBank = "!@#$%^&*()_+-={}[]|./?<>:\"";
- private String letterBank = "abcdefghijklmnopqrstuvwxyz";
- private String[][] wordBank = {
- {"a", "i"},
- {"an", "at", "as", "go", "hi", "or", "if", "is", "in", "it", "me", "my", "am", "be", "to", "of", "so", "us", "we"},
- {"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"}
- };
- public String encode(String message) {
- message = message.toLowerCase();
- String b;
- if ((int)(Math.random()*2) == 0) {
- b = symbolBank;
- } else {
- b = letterBank;
- }
- char[] bank = b.toCharArray();
- String output = "";
- for (int i = 0; i < message.length(); i++) {
- output += "_";
- }
- boolean[] tracker = new boolean[message.length()];
- for (int i = 0; i < message.length(); i++) {
- System.out.print(i + ": ");
- if (!tracker[i]) {
- System.out.print("Replacing... ");
- if ((int)message.charAt(i)>=65 && (int)message.charAt(i)<=122) {
- char a = nextReplacement(message.charAt(i), bank);
- System.out.println(message.charAt(i) + " with " + a);
- for (int v = i; v < message.length(); v++) {
- if (message.charAt(v) == message.charAt(i)) {
- output = transcribe(output, v, a);
- tracker[v] = true;
- }
- }
- } else {
- System.out.println("Failed. Non alphanumeric.");
- //output = transcribe(output, i, message.charAt(i));
- output = transcribe(output, i, ' ');
- tracker[i] = true;
- }
- } else {
- System.out.println("Failed. Already replaced.");
- }
- }
- return output;
- }
- public char nextReplacement(char a, char[] b) {
- int index;
- while (true) {
- index = (int)(Math.random()*b.length);
- if (b[index] != '`' && b[index] != a) {
- break;
- }
- }
- char choice = b[index];
- b[index] = '`';
- return choice;
- }
- public String transcribe(String s, int i, char b) {
- return s.substring(0, i) + b + s.substring(i+1);
- }
- public String decode(String message) {
- String[][] messageBank = tabulateMessage(message);
- for (int i = 0; i < messageBank.length; i++) {
- for (int v = 0; v < messageBank[0].length; v++) {
- System.out.print(messageBank[i][v] + " ");
- }
- System.out.println("");
- }
- for (int len = 0; len < wordBank.length; len++) {
- }
- return "";
- }
- public String[][] tabulateMessage(String message) {
- boolean flag = false;
- int wordnum = 0;
- for (int i = 0; i < message.length(); i++) {
- if ((int)message.charAt(i)>=65 && (int)message.charAt(i)<=122) {
- flag = true;
- if (i == message.length()-1) {
- wordnum++;
- }
- } else if (flag) {
- flag = false;
- wordnum++;
- }
- }
- System.out.println(wordnum + " words");
- flag = false;
- int[] counter = new int[wordnum];
- int wordlen = 0;
- int wordmax = 0;
- for (int i = 0; i < message.length(); i++) {
- if ((int)message.charAt(i)>=65 && (int)message.charAt(i)<=122) {
- flag = true;
- wordlen++;
- if (i == message.length()-1) {
- if (wordlen > wordmax) {
- wordmax = wordlen;
- }
- counter[wordlen-1]++;
- }
- } else if (flag) {
- flag = false;
- if (wordlen > wordmax) {
- wordmax = wordlen;
- }
- counter[wordlen-1]++;
- wordlen=0;
- }
- }
- int lenmax = 0;
- for (int i = 0; i < counter.length; i++) {
- if (counter[i] > lenmax) {
- lenmax = counter[i];
- }
- }
- String[][] messageWords = new String[wordmax][lenmax];
- int[] tracker = new int[wordmax];
- flag = false;
- wordlen = 0;
- for (int i = 0; i < message.length(); i++) {
- if ((int)message.charAt(i)>=65 && (int)message.charAt(i)<=122) {
- flag = true;
- wordlen++;
- if (i == message.length()-1) {
- messageWords[wordlen-1][tracker[wordlen-1]] = message.substring(i-wordlen+1);
- tracker[wordlen-1]++;
- }
- } else if (flag) {
- messageWords[wordlen-1][tracker[wordlen-1]] = message.substring(i-wordlen, i);
- tracker[wordlen-1]++;
- flag = false;
- wordlen=0;
- }
- }
- return messageWords;
- }
- public void error(String text) {
- throw new IllegalArgumentException(text);
- }
- }
Add Comment
Please, Sign In to add comment