/** * File: BigramCount.java * @author A.Kurd * * Description: Implements a Java 1.1 version of a GUI tool for cycling through an encrypted message and collecting * how often a pair letters appear in the message. It then orders the bigrams in descending order based * on their frequency. * 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 BigramCount { public final int MAX_CHARS = 128; // Possibly all ASCII characters private int alphabetRange = Alphabet.RANGE_ascii; public String message; private Alphabet alphabet = null; public int tabulate [] [] = new int [MAX_CHARS] [3];//accumulates the bigrams and their frequencies public void BigramCount(String text) { message = text; } //BigramCount /*This method counts the frequency of the bigrams in a message. It cycles through the whole message collecting groups of two letters. It then checks how often each pair appears in the array and increments the tabulate array accordingly. Later these are ordered in descending order based on trigram frequency.*/ public void count(String text) { int size = 128; FrequencyRecord frequencies[] [] = new FrequencyRecord[size] [size]; for (int k = 0; k < frequencies.length; k++) for (int i = 0; i < frequencies[k].length; i++) frequencies[k] [i]= new FrequencyRecord((char)k, (char)i, 0); try { alphabet = new Alphabet(alphabetRange); } catch (Exception e) { e.printStackTrace(); }//catch int charCount = 0; int alphabeticsCount = 0; int highestFreq = 0; for (int k = 0; (k+1) < text.length(); k++) { char ch = text.charAt(k); char ch2=text.charAt(k+1); if (Character.isLetter(ch) && Character.isLetter(ch2)) ++alphabeticsCount; if (alphabet.isInAlphabet(ch) && alphabet.isInAlphabet(ch2)) { // Ignore chars > 127 charCount++; frequencies[ch] [ch2].count++; } //if } //for int j = 0; for (int k = 0; k < frequencies.length; k++) for (int i = 0; i < frequencies[k].length; i++) { if (frequencies [k] [i].count > 0) { highestFreq = frequencies[k] [i].count; // Keep track of highest f tabulate [j] [0] = highestFreq; tabulate [j] [1] = k; tabulate [j] [2] = i; j++; }//if }//for int temp, temp1, temp2; for (int pass = 1; pass < tabulate.length; pass++) for (int pair = 1; pair < tabulate.length; pair++) if (tabulate[pair -1][0] < tabulate[pair] [0]){ temp = tabulate[pair-1][0]; temp1 = tabulate[pair-1][1]; temp2 = tabulate[pair-1][2]; tabulate[pair-1][0] = tabulate[pair][0]; tabulate[pair-1][1] = tabulate[pair][1]; tabulate[pair-1][2] = tabulate[pair][2]; tabulate[pair][0] = temp; tabulate[pair][1] = temp1; tabulate[pair][2] = temp2; }//if } //count }//class