Calculate f

The function in the GuessKey program [Component 3] which calculates the value of f for a given shift amount.

 

Global Data Structures Used:

' For letters of the alphabet [0 to 25] the frequency of occurrence of each

Dim charFreq(0 To 25) As Double

' For keys [1 to k] and cipherLetters [0 to 25] the number of occurrences of each character, relative to the total number of characters encoded with that key

Dim relFreqTab(1 To maxKeyLen, 0 To 25) As Double

' For and keys [1 to k] and shiftamounts [0 to 25], the Fee Value

Dim feeTab(1 To maxKeyLen, 0 To 25) As Double

 

VB Code:

Public Sub genFeeTable(ByVal keyLen As Integer)

Dim k, shift, ciphLet As Integer

Dim fee As Double

 For k = 1 To keyLen

  For shift = 0 To 25

   fee = 0

   For ciphLet = 0 To 25

    fee = fee + relFreqTab(k, ciphLet) * charFreq((ciphLet + 26 - shift) Mod 26)

   Next ciphLet

   feeTab(k, shift) = fee

  Next shift

 Next k

End Sub

 

Discussion & Algorithm

 

Global Data Structures Used:

1. A 1-d array charFreq containing the frequency table provided in file: charFreqEng.txt

2. A 2-d array relFreqTab containing the relative frequency table calculated by this

program.  Algorithm is given below.

3. A 2-d array feeTab containing the f value at each key position for each of the 26

possible shift values.

 

Algorithm to calculate relative frequency table:

1. Calculate the table of frequencies of each letter for each key position of the cipher text.

   a. Declare a 2-d array freqTab [keypositions][alphabet-positions] of type integer

   b. For key positions 0®25, go through cipher text, for each letter encountered,

bump up the counter for that letter.

2. For each entry in freqTab divide the value by the number of letters at the given position and enter into relFreqTab.

 

Algorithm:

0a. Parameters:

   a. keylen – the length of the key used to create the cipher text

0b. Local variables:

   a. k – index used to iterate through each of the key positions

   b. shift – index used to iterate through each of the 26 possible shift values

   c. ciphLet – index used to iterate through each of the 26 possible letters in the

cipher text, represented as integers [0..25].

   d. fee – floating point variable used to sum f for each key position & each shift

amount.

1. For k = 1 to keylen: iterate through all possible key positions

  2. For shift = 0 to 25: iterate through all possible shift amounts

   a. fee = 0: initialize f to 0.

   3. For ciphLet = 0 to 25: iterate through all letters of the alphabet

      a. fee += relFreqTab[k, ciphLet] * charFreq[[ciphLet + 26 – shift] % 26]

   Calculate f for that letter/shift combination and add it to the sum.

   Multiply:

          Relative frequency of that letter in the cipher text at that key position

          Frequency in English of the (possible) plain text letter, given that

particular shift amount.

 

Main Algorithm

 

1. Input keyLen from the file keyLength.txt

2. Call readCharFreq to read the file charFreqEng.txt

3. Call readCipher returning the value mesgLen

4. Call genFreqTab to generate the tables freqTab and relFreqTab

5. Call genFeeTab to generate the table feeTab

6. Call sortFeeValues to produce a ranked order feeRank of indices into feeTab

7. Call printFeeTables to print out the f values both sorted and unsorted.