/** * File: Calculate.java * @author A.Kurd * * Description: Implements a Java 1.1 version of a GUI tool for calculating the accuracy of each KeyGroup. It checks if the * the letters of the bigrams and trigrams appear in the 5 groups of the KeyGroups that they are supposed to be in. * Credits: This program is modelled after the frequency test of the CryptoToolJ program by Ralph Morelli. * * Copyright: This program is in the public domain. You can do whatever you want with * it as long as I get credit for my work and as long as you offer your changes to me * so I can possible add them to the "official" version. */ package hcrypto.analyzer; import hcrypto.cipher.*; import java.awt.*; public class Calculate { public String message; private TextArea text; // Pointers to the text to be analyzed public int freq [] [] = new int [128] [3]; public int acc [] = new int [128]; public void Calculate(String text) { message = text; } //Calculate /*changes the frequencies of trigrams in to ints from chars. Used later during comparisons.*/ public void triFreq(char [][] frequencies){ for(int i=0; i<12; i++) if(frequencies[i] != null){ freq[i][0] = (int)frequencies[i][0]; freq[i][1] = (int)frequencies[i][1]; freq[i][2] = (int)frequencies[i][2];}//if }//triFreq /*Checks if the letters of bigram and trigram data of the encrypted text appear in the proper spots of the array containing the encrypted text based ordered by letter frequency. It then increments the score of the KeyGroup accordingly.*/ public int accumulate(int [] res, int [] [] tab, int total) { for(int i = 1; i < 9; i++){ for(int j = 1; j<9; j++){ for(int k = 1; k<9; k++){ if(res[i] == tab[0][1] && res[j] == tab[0][2] && i!=j) total = total +30- (i + j); else if(res[i] == tab[1][1] && res[0] == tab[1][2]) total = total + 29-i; else if(res[i] == tab[2][1] && res[j] == tab[2][2] && i!=j) total = total + 28- (i + j); else if(res[0] == tab [3][1] && res[i] == tab [3][2]) total = total + 27-i; else if(res[i] == tab[4][1] && res[j] == tab[4][2] && i!=j) total = total + 26- (i + j); else if(res[i] == tab [5][1] && res[0] == tab [5][2]) total = total + 25-i; else if(res[0] == tab [6][1] && res[(i%2)+9] == tab [6][2]) total = total + 24-(9-(i%2)); else if(res[i] == tab[7][1] && res[j] == tab[7][2] && i!=j) total = total + 23- (i + j); else if(res[0] == tab [8][1] && res[i] == tab [8][2]) total = total +22-i; else if(res[i] == tab[9][1] && res[j] == tab[9][2] && i!=j) total = total +21- (i + j); else if(res[0] == tab [10][1] && res[i] == tab [10][2]) total = total +20-i; else if(res[i] == tab[11][1] && res[j] == tab[11][2] && i!=j) total = total +19- (i + j); else if(res[i] == tab[12][1] && res[j] == tab[12][2] && i!=j) total = total +18- (i + j); else if(res[i] == tab[13][1] && res[j] == tab[13][2] && i!=j) total = total +17- (i + j); else if(res[i] == tab[14][1] && res[j] == tab[14][2] && i!=j) total = total +16- (i + j); if(((char)res[i]==freq[0][0]&&(char)res[j]==freq[0][1]&&(char)res[0]==freq[0][2]) || ((char)res[i]==freq[0][0]&&(char)res[0]==freq[0][1]&&(char)res[j]==freq[0][2]) || ((char)res[0]==freq[0][0]&&(char)res[i]==freq[0][1]&&(char)res[j]==freq[0][2])) total = 3*(total + 48 -(i+j)); else if((char)res[i]==freq[1][0]&&(char)res[j]==freq[1][1]&&(char)res[(k%9)+11]==freq[1][2]) total = 2*(total + 47 -(i+j+k)); else if((char)res[i]==freq[2][0]&&(char)res[j]==freq[2][1]&&(char)res[(k%2)+9]==freq[2][2]) total = 2*(total + 46 -(i+j+k)); else if(((char)res[i]==freq[3][0]&&(char)res[j]==freq[3][1]&&(char)res[0]==freq[3][2]) || ((char)res[i]==freq[3][0]&&(char)res[0]==freq[3][1]&&(char)res[j]==freq[3][2]) || ((char)res[0]==freq[3][0]&&(char)res[i]==freq[3][1]&&(char)res[j]==freq[3][2])) total = 2*(total + 45 -(i+j)); else if(((char)res[0]==freq[4][0]&&(char)res[i]==freq[4][1]&&(char)res[0]==freq[4][2]) || ((char)res[i]==freq[4][0]&&(char)res[0]==freq[4][1]&&(char)res[0]==freq[4][2]) || ((char)res[0]==freq[4][0]&&(char)res[0]==freq[4][1]&&(char)res[i]==freq[4][2])) total = 2*(total + 44 -i); else if(((char)res[i]==freq[5][0]&&(char)res[j]==freq[5][1]&&(char)res[0]==freq[5][2]) || ((char)res[i]==freq[5][0]&&(char)res[0]==freq[5][1]&&(char)res[j]==freq[5][2]) || ((char)res[0]==freq[5][0]&&(char)res[i]==freq[5][1]&&(char)res[j]==freq[5][2])) total = 2*(total + 43 -(i+j)); else if((char)res[i]==freq[6][0]&&(char)res[j]==freq[6][1]&&(char)res[k]==freq[6][2]) total = 2*(total + 42 -(i+j+k)); else if((char)res[i]==freq[7][0]&&(char)res[j]==freq[7][1]&&(char)res[k]==freq[7][2]) total = 2*(total + 41 -(i+j+k)); else if((char)res[i]==freq[8][0]&&(char)res[j]==freq[8][1]&&(char)res[(k%9)+11]==freq[8][2]) total = total + 40 -(i+j+k); else if(((char)res[i]==freq[9][0]&&(char)res[j]==freq[9][1]&&(char)res[0]==freq[9][2]) || ((char)res[i]==freq[9][0]&&(char)res[0]==freq[9][1]&&(char)res[j]==freq[9][2]) || ((char)res[0]==freq[9][0]&&(char)res[i]==freq[9][1]&&(char)res[j]==freq[9][2])) total = total + 39 -(i+j); else if((char)res[i]==freq[1][0]&&(char)res[j]==freq[1][1]&&(char)res[(k%9)+11]==freq[1][2]) total = total + 38 -(i+j+k); else if((char)res[i]==freq[2][0]&&(char)res[j]==freq[2][1]&&(char)res[(k%2)+9]==freq[2][2]) total = total + 37 -(i+j+k); }//trigram for }//for inside }//for outside return total; }//accumulate }//class