Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. // Problem N/A. Find a "long" (longest) message (sequence of words in the lexicon) and
  2. // encryption key tgat creates an "interesting" and "ambiguous" collection of possible
  3. // decryption sequences.
  4. public List<String> getNGrams(int n) {
  5. List<String> ngrams = new ArrayList<String>();
  6. for (String word : plaintextWords) {
  7. if (word.length() == n) {
  8. ngrams.add(word);
  9. }
  10. }
  11. return ngrams;
  12. }
  13. public int getAmbiguity(String cyphertext, List<String> ngrams, boolean print) {
  14. int n=0;
  15. for (int[] decryptKey : affineKeySet) {
  16. if (ngrams.contains(affineDecrypt(decryptKey, cyphertext))) {
  17. n++;
  18. if (print == true) {
  19. System.out.println(affineDecrypt(decryptKey,cyphertext) + Arrays.toString(decryptKey) + cyphertext);
  20. }
  21. }
  22. }
  23. return n;
  24. }
  25. public int getMostAmbiguous(String word, List<String> ngrams, boolean print) {
  26. int max = -1;
  27. int len = 0;
  28. int[] maxKey = null;
  29. for (int[] key : affineKeySet) {
  30. len = getAmbiguity(affineEncrypt(key, word),ngrams, false);
  31. if (len > max) {
  32. max=len;
  33. maxKey = key.clone();
  34. }
  35. }
  36. if (print == true) {
  37. System.out.println(Arrays.toString(maxKey));
  38. }
  39. return getAmbiguity(affineEncrypt(maxKey,word),ngrams,print);
  40. }
  41. public int getMostAmbiguousNGram(int n) {
  42. int max = 0;
  43. int len = 0;
  44. String maxWord = "";
  45. List<String> ngrams = getNGrams(n);
  46. for (String word : ngrams) {
  47. len = getMostAmbiguous(word,ngrams, false);
  48. if (len > max) {
  49. max = len;
  50. maxWord = word;
  51. }
  52. }
  53. System.out.println(maxWord);
  54. return getMostAmbiguous(maxWord,ngrams,true);
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement