Descriptions. I guess I’ll come up with a program that breaks encrypted messages that used Caesar’s algorithm. It sounds pretty simple, the problem is that you need to figure out which position was used to create the encrypted message in order to decrypt the encoded message. Therefore, I have a method called public void train(String trainingFileName)that reads in a file with lots of text to determine the frequency of each lowercase alphabetical English character (a - z)that is stored in double array []. This method works great, so you don’t need to look if you want it, but I use most of this code in a method in which I am having problems, according to which my method public int decrypt(String cipherTextFileName, String outputFileName). The code works beautifully when Caesar Cipher is set to 3 positions, but something else this creates a lot of problems. I have a loopdo-whilein my method public int decrypt(String cipherTextFileName, String outputFileName), which decrypts an encoded message starting at 0 positions, and then using the "distance" formula that I use (NOTE: I cannot use any other formula) to find the minimum distance between knownFrequenciesand observedFreqin my encrypted message. Right now I have a loop installed do-while, where if distance- less than 0.6then stop the loop. Theoretically, when I have the correct number of positions in the Caesar code, the distance should be below this value.
Problem: the program works fine when it numberOfPositionsis 3, but when I use an encrypted message that does not use 3 positions in Caesar Cipher, it distancenever drops below 1 and in debug mode, when I set numberOfPositionswhat is needed to decrypt the message, the message is still encrypted .
Question: How can I implement this method better, so I do not test my distancehard value to stop the cycle do-while? I tried to use Math.min(), but it does not work. Why can't I decode a message with Caesar cipher positions other than 3.
. . 3 . ... 1000. train. , .
, 3 , 5 .
Wkh surjudp zdv krvwhg eb dfwru Slhufh Eurvqdq dqg kdg frpphqwdub iurp pdqb Kroobzrrg dfwruv dqg iloppdnhuv Prylh txrwdwlrqv wkdw ylhzhuv xvh lq wkhlu rzq olyhv dqg vlwxdwlrqv
Ymj uwtlwfr bfx mtxyji gd fhytw Unjwhj Gwtxsfs fsi mfi htrrjsyfwd kwtr rfsd Mtqqdbtti fhytwx fsi knqrrfpjwx Rtanj vztyfyntsx ymfy anjbjwx zxj ns ymjnw tbs qnajx fsi xnyzfyntsx
:
The program was hosted by actor Pierce Brosnan and had commentary from many Hollywood actors and filmmakers Movie quotations that viewers use in their own lives and situations
, , ( ), , :
public class CodeBreaker {
public final int NUMBER_OF_LETTERS = 26;
private double[] knownFrequencies = new double[NUMBER_OF_LETTERS];
public double[] getKnownFrequencies() {
return knownFrequencies;
}
public void setKnownFrequencies(double[] knownFrequencies) {
this.knownFrequencies = knownFrequencies;
}
public void train(String trainingFileName) {
try {
Scanner fileIO = new Scanner(new File(trainingFileName));
int total = 0;
String temp = "";
while (fileIO.hasNext()) {
temp += fileIO.next().toLowerCase().replaceAll("[ -,!?';:.]+", "");
}
char[] c = temp.toCharArray();
total += c.length;
int k = (int) 'a';
int[] counter = new int[NUMBER_OF_LETTERS];
for (int j = 0; j < total; j++) {
for (int i = k - k; i < knownFrequencies.length; i++) {
char[] d = new char[knownFrequencies.length];
d[i] = (char) (k + i);
if (c[j] == d[i]) {
counter[i]++;
knownFrequencies[i] = (double) counter[i] / total;
}
}
}
fileIO.close();
} catch (FileNotFoundException e) {
System.err.println(e);
System.exit(0);
}
}
public int decrypt(String cipherTextFileName, String outputFileName) {
Scanner fileIO;
int numberOfPositions = 0;
double distance = 0.000000;
try {
fileIO = new Scanner(new File(cipherTextFileName));
PrintWriter writer = new PrintWriter(new File(outputFileName));
String temp = "";
while (fileIO.hasNext()) {
temp += fileIO.next().toLowerCase().replaceAll(" ", "");
}
fileIO.close();
do {
distance = 0.0;
int total = 0;
double[] observedFreq = new double[NUMBER_OF_LETTERS];
temp = decrypt(temp, numberOfPositions);
char[] c = temp.toCharArray();
total += c.length;
int k = (int) 'a';
int[] counter = new int[NUMBER_OF_LETTERS];
for (int j = 0; j < total; j++) {
for (int i = k - k; i < observedFreq.length; i++) {
char[] d = new char[observedFreq.length];
d[i] = (char) (k + i);
if (c[j] == d[i]) {
counter[i]++;
observedFreq[i] = (double) counter[i] / total;
}
}
}
for (int j = 0; j < knownFrequencies.length; j++) {
distance += Math.abs(knownFrequencies[j] - observedFreq[j]);
}
numberOfPositions = numberOfPositions + 1;
} while (distance > 0.6);
Scanner fileIO2 = new Scanner(new File(cipherTextFileName));
while (fileIO2.hasNextLine()) {
temp = fileIO2.nextLine();
writer.println(decrypt(temp, numberOfPositions));
}
writer.close();
fileIO2.close();
System.out.println(distance);
} catch (FileNotFoundException e) {
System.err.println(e);
System.exit(0);
}
return numberOfPositions;
}
public String decrypt(String ciphertext, int numberOfPositions) {
return encrypt(ciphertext, -numberOfPositions);
}
public String encrypt(String msg, int offset) {
offset = offset % 26 + 26;
StringBuilder encoded = new StringBuilder();
for (char i : msg.toCharArray()) {
if (Character.isLowerCase(i)) {
int j = (i - 'a' + offset) % 26;
encoded.append((char) (j + 'a'));
} else if (Character.isUpperCase(i)) {
int h = (i - 'A' + offset) % 26;
encoded.append((char) (h + 'A'));
} else {
encoded.append(i);
}
}
return encoded.toString();
}
public static void main(String[] args) {
CodeBreaker cb = new CodeBreaker();
cb.train(args[0]);
System.out.println(cb.decrypt(args[1], args[2]));
}
}