The goal is to calculate the frequency of the ciphertext letters and then replace them with English letters based on their frequency statistics.
What I have tried:
I have generated the frequencies but when I'm using the replace method it's not giving the desired output, for example, if I use one replace() method, it outputs accordingly(
String A = encryptedText.replace('a','z');
. It replaces all the 'a's with 'z's but if I add the replace methods(one replace method for each character of the alphabet for example
(<pre> String B = encryptedText.replace('b','y');
,
(<pre> String C = encryptedText.replace('c','x');
) and so on only the first method replace function is executed, the others are not executed and it doubles all the letters in the output, for example, if I replace 'a','b' and 'c' with 'z','y' and 'x' respectively, it outputs as "
zbcaycabxabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc
".
Below is my code:
import java.util.Arrays;
import java.util.Scanner;
public class FrequencyAnalysisCaesarCipher {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter message to be decrypted: ");
String encryptedText = input.nextLine().toLowerCase();
int[] freq = new int[encryptedText.length()];
int i = 0;
int j;
char[] string = encryptedText.toCharArray();
Arrays.sort(string);
Arrays.sort(new int[]{freq[i]});
for (i = 0; i < encryptedText.length(); i++) {
freq[i] = 1 ;
for (j = i + 1; j < encryptedText.length(); j++) {
if (string[i] == string[j]) {
freq[i]++;
string[j] = '0';
}
}
}
System.out.println("Characters and their corresponding frequencies");
for (i = 0; i < freq.length; i++) {
if (string[i] != ' ' && string[i] != '0') {
System.out.println(string[i] + "-" + freq[i]);
}
}
System.out.println("..............................................");
String A = encryptedText.replace('a','z');
String B = encryptedText.replace('b','y');
String C = encryptedText.replace('c','x');
String D = encryptedText.replace('d','w');
String E = encryptedText.replace('e','v');
String F = encryptedText.replace('f','u');
String G = encryptedText.replace('g','t');
String H = encryptedText.replace('h','s');
String I = encryptedText.replace('i','r');
String J = encryptedText.replace('j','q');
String K = encryptedText.replace('k','p');
String L = encryptedText.replace('l','o');
String M = encryptedText.replace('m','n');
String N = encryptedText.replace('n','m');
String O = encryptedText.replace('o','l');
String P = encryptedText.replace('p','k');
String Q = encryptedText.replace('q','j');
String R = encryptedText.replace('r','i');
String S = encryptedText.replace('s','h');
String T = encryptedText.replace('t','g');
String U = encryptedText.replace('u','f');
String V = encryptedText.replace('v','e');
String W = encryptedText.replace('w','d');
String X = encryptedText.replace('x','c');
String Y = encryptedText.replace('y','b');
String Z = encryptedText.replace('z','a');
System.out.print(A+B+C+D+E+F+G+H+I+J+K+L+M+N+O+P+Q+R+S+T+U+V+W+X+Y+Z);
}
}