CaptainSpaceCat

ShiftCipher

Sep 9th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 KB | None | 0 0
  1. import java.lang.Math;
  2. import java.util.ArrayList;
  3.  
  4. public class ShiftCipher {
  5.  
  6.   private String[][] wordBank = {
  7.     {"a", "i"},
  8.     {"an", "or", "if", "is", "am", "be", "to", "of"},
  9.     {"and", "the", "for", "but", "are", "not", "you"}
  10.   };
  11.  
  12.   public String encode(String message) {
  13.     int offset = (int)(Math.random()*25) + 1;
  14.     return encode(message, offset) + "\n" + offset;
  15.   }
  16.  
  17.   public String encode(String message, int offset) {
  18.     String result = "";
  19.     for (int i = 0; i < message.length(); i++) {
  20.       int n = (int)message.toLowerCase().charAt(i);
  21.       if (n >= 97 && n <= 122) {
  22.         n += offset - 97;
  23.         n = n % 26 + 97;
  24.       }
  25.       char c = (char)n;
  26.       result += c;
  27.     }
  28.     return result;
  29.   }
  30.  
  31.   public String decode(String message) {
  32.     ArrayList<SolvedShift> matches = new ArrayList<SolvedShift>();
  33.     int[] lettersUsed = new int[26];
  34.    
  35.     for (int i = 1; i < wordBank.length + 1; i++) {
  36.       String[] clumps = findClumps(message, i);
  37.       for (String c : clumps) {
  38.         for (String word : wordBank[i - 1]) {
  39.           int offset = scanWord(c, word);
  40.           if (offset > -1) {
  41.             if (lettersUsed[offset] <= 0) {
  42.               lettersUsed[offset] = matches.size() + 1;
  43.               matches.add(new SolvedShift(decode(message, offset), offset, word));
  44.             } else {
  45.               matches.get(lettersUsed[offset] - 1).addKeyword(word);
  46.             }
  47.             //matches.add(new SolvedShift(decode(message, offset), offset, word));
  48.           }
  49.           //return c + " " + word;
  50.         }
  51.       }
  52.     }
  53.     return formatList(matches.toArray(new SolvedShift[matches.size()]));
  54.   }
  55.  
  56.   public String decode(String message, int offset) {
  57.     String result = "";
  58.     for (int i = 0; i < message.length(); i++) {
  59.       int n = (int)message.toLowerCase().charAt(i);
  60.       if (n >= 97 && n <= 122) {
  61.         n += (26 - offset) - 97;
  62.         n = n % 26 + 97;
  63.       }
  64.       char c = (char)n;
  65.       result += c;
  66.     }
  67.     return result;
  68.   }
  69.  
  70.  
  71.   public String[] findClumps(String message, int length) {
  72.     message += " ";
  73.     ArrayList<String> clumps = new ArrayList<String>();
  74.     int count = 0;
  75.     for (int i = 0; i < message.length(); i++) {
  76.       int n = (int)message.toLowerCase().charAt(i);
  77.      
  78.       if (n >= 97 && n <= 122) {
  79.         count++;
  80.       } else if (count == length) {
  81.         clumps.add(message.toLowerCase().substring(i - length, i));
  82.         count = 0;
  83.       } else {
  84.         count = 0;
  85.       }
  86.     }
  87.     return clumps.toArray(new String[clumps.size()]);
  88.   }
  89.  
  90.   public int scanWord(String message, String word) {
  91.     int offset = (int)message.charAt(0) - (int)word.charAt(0);
  92.     if (offset < 0) {
  93.       offset += 26;
  94.     }
  95.     if (decode(message, offset).equals(word)) {
  96.       return offset;
  97.     }
  98.     return -1;
  99.   }
  100.  
  101.   public String formatList(SolvedShift[] ciphers) {
  102.    
  103.     for (SolvedShift s:ciphers) {
  104.       s.cleanKeywords();
  105.     }
  106.    
  107.     for (int i = 0; i < ciphers.length; i++) {
  108.       int index = i;
  109.       for (int n = i; n < ciphers.length; n++) {
  110.         if (ciphers[n].compareTo(ciphers[index]) < 0) {
  111.           index = n;
  112.         }
  113.       }
  114.       SolvedShift temp = ciphers[i];
  115.       ciphers[i] = ciphers[index];
  116.       ciphers[index] = temp;
  117.     }
  118.    
  119.     String result = "";
  120.     for(SolvedShift s : ciphers) {
  121.       result += "Response: " + s.message + "\nOffset: " + s.offset;
  122.       if (s.keywords.length > 1) {
  123.         result += "\nBased on keywords: ";
  124.         for (String k : s.keywords) {
  125.           result += k + ", ";
  126.         }
  127.         result = result.substring(0, result.length() - 2);
  128.       } else {
  129.         result += "\nBased on keyword: " + s.keywords[0];
  130.       }
  131.       result += "\n\n";
  132.     }
  133.     if (ciphers.length == 0) {
  134.       result = "Decoding failed.";
  135.     }
  136.     return result;
  137.   }
  138.  
  139.   public void error(String text) {
  140.     throw new IllegalArgumentException(text);
  141.   }
  142.  
  143. }
Add Comment
Please, Sign In to add comment