Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Problem N/A. Find a "long" (longest) message (sequence of words in the lexicon) and
- // encryption key tgat creates an "interesting" and "ambiguous" collection of possible
- // decryption sequences.
- public List<String> getNGrams(int n) {
- List<String> ngrams = new ArrayList<String>();
- for (String word : plaintextWords) {
- if (word.length() == n) {
- ngrams.add(word);
- }
- }
- return ngrams;
- }
- public int getAmbiguity(String cyphertext, List<String> ngrams, boolean print) {
- int n=0;
- for (int[] decryptKey : affineKeySet) {
- if (ngrams.contains(affineDecrypt(decryptKey, cyphertext))) {
- n++;
- if (print == true) {
- System.out.println(affineDecrypt(decryptKey,cyphertext) + Arrays.toString(decryptKey) + cyphertext);
- }
- }
- }
- return n;
- }
- public int getMostAmbiguous(String word, List<String> ngrams, boolean print) {
- int max = -1;
- int len = 0;
- int[] maxKey = null;
- for (int[] key : affineKeySet) {
- len = getAmbiguity(affineEncrypt(key, word),ngrams, false);
- if (len > max) {
- max=len;
- maxKey = key.clone();
- }
- }
- if (print == true) {
- System.out.println(Arrays.toString(maxKey));
- }
- return getAmbiguity(affineEncrypt(maxKey,word),ngrams,print);
- }
- public int getMostAmbiguousNGram(int n) {
- int max = 0;
- int len = 0;
- String maxWord = "";
- List<String> ngrams = getNGrams(n);
- for (String word : ngrams) {
- len = getMostAmbiguous(word,ngrams, false);
- if (len > max) {
- max = len;
- maxWord = word;
- }
- }
- System.out.println(maxWord);
- return getMostAmbiguous(maxWord,ngrams,true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement