Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.Math;
- import java.util.ArrayList;
- public class ShiftCipher {
- private String[][] wordBank = {
- {"a", "i"},
- {"an", "or", "if", "is", "am", "be", "to", "of"},
- {"and", "the", "for", "but", "are", "not", "you"}
- };
- public String encode(String message) {
- int offset = (int)(Math.random()*25) + 1;
- return encode(message, offset) + "\n" + offset;
- }
- public String encode(String message, int offset) {
- String result = "";
- for (int i = 0; i < message.length(); i++) {
- int n = (int)message.toLowerCase().charAt(i);
- if (n >= 97 && n <= 122) {
- n += offset - 97;
- n = n % 26 + 97;
- }
- char c = (char)n;
- result += c;
- }
- return result;
- }
- public String decode(String message) {
- ArrayList<SolvedShift> matches = new ArrayList<SolvedShift>();
- int[] lettersUsed = new int[26];
- for (int i = 1; i < wordBank.length + 1; i++) {
- String[] clumps = findClumps(message, i);
- for (String c : clumps) {
- for (String word : wordBank[i - 1]) {
- int offset = scanWord(c, word);
- if (offset > -1) {
- if (lettersUsed[offset] <= 0) {
- lettersUsed[offset] = matches.size() + 1;
- matches.add(new SolvedShift(decode(message, offset), offset, word));
- } else {
- matches.get(lettersUsed[offset] - 1).addKeyword(word);
- }
- //matches.add(new SolvedShift(decode(message, offset), offset, word));
- }
- //return c + " " + word;
- }
- }
- }
- return formatList(matches.toArray(new SolvedShift[matches.size()]));
- }
- public String decode(String message, int offset) {
- String result = "";
- for (int i = 0; i < message.length(); i++) {
- int n = (int)message.toLowerCase().charAt(i);
- if (n >= 97 && n <= 122) {
- n += (26 - offset) - 97;
- n = n % 26 + 97;
- }
- char c = (char)n;
- result += c;
- }
- return result;
- }
- public String[] findClumps(String message, int length) {
- message += " ";
- ArrayList<String> clumps = new ArrayList<String>();
- int count = 0;
- for (int i = 0; i < message.length(); i++) {
- int n = (int)message.toLowerCase().charAt(i);
- if (n >= 97 && n <= 122) {
- count++;
- } else if (count == length) {
- clumps.add(message.toLowerCase().substring(i - length, i));
- count = 0;
- } else {
- count = 0;
- }
- }
- return clumps.toArray(new String[clumps.size()]);
- }
- public int scanWord(String message, String word) {
- int offset = (int)message.charAt(0) - (int)word.charAt(0);
- if (offset < 0) {
- offset += 26;
- }
- if (decode(message, offset).equals(word)) {
- return offset;
- }
- return -1;
- }
- public String formatList(SolvedShift[] ciphers) {
- for (SolvedShift s:ciphers) {
- s.cleanKeywords();
- }
- for (int i = 0; i < ciphers.length; i++) {
- int index = i;
- for (int n = i; n < ciphers.length; n++) {
- if (ciphers[n].compareTo(ciphers[index]) < 0) {
- index = n;
- }
- }
- SolvedShift temp = ciphers[i];
- ciphers[i] = ciphers[index];
- ciphers[index] = temp;
- }
- String result = "";
- for(SolvedShift s : ciphers) {
- result += "Response: " + s.message + "\nOffset: " + s.offset;
- if (s.keywords.length > 1) {
- result += "\nBased on keywords: ";
- for (String k : s.keywords) {
- result += k + ", ";
- }
- result = result.substring(0, result.length() - 2);
- } else {
- result += "\nBased on keyword: " + s.keywords[0];
- }
- result += "\n\n";
- }
- if (ciphers.length == 0) {
- result = "Decoding failed.";
- }
- return result;
- }
- public void error(String text) {
- throw new IllegalArgumentException(text);
- }
- }
Add Comment
Please, Sign In to add comment