public StreamCipherDecryptor() { cryptogramsArray = Operations.getCryptogramsArray(cryptogramsFilePath); codeToDecrypt = cryptogramsArray[cryptogramsArray.length - 1]; max = codeToDecrypt.size(); key = new int[codeToDecrypt.size()]; /** **************** Initialize every key value on -1 ********************* */ for (int i = 0; i < key.length; i++) key[i] = -1; /** *********** Getting every chars that can be used in text ************** */ possibleChars = Operations.getPossibleChars(); /** *********** In loop program looking for most probably keys ************ */ for (int i = 0; i < max; i++) { probablyKeys = new ArrayList<>(); for (ArrayList<Integer> cryptogram : cryptogramsArray) { int value = cryptogram.get(i); for (SingleChar singleChar : possibleChars) { int key = xor(value, singleChar.getCharacter()); ProbablyKey p = getProbablyKey(key); if (p == null) { p = new ProbablyKey(key); p.addWeight(singleChar.getWeight()); probablyKeys.add(p); } else p.addWeight(singleChar.getWeight()); } } probablyKeys = Sort.sort(probablyKeys); key[i] = chooseBestKey(i); } printDecryted(); }
private Boolean isCharacter(int k) { for (SingleChar singleChar : possibleChars) if (singleChar.getCharacter() == k) return true; return false; }