Frequency Analysis with Excel
Aim
You are going to use Excel VBA to develop frequency analysis tools.
Text Entry Box.
This workbook uses textboxes and command buttons to count the frequency distribution of the letters in the ciphertext. This is one of the first jobs that you should perform on a new cipher text as it often yields vital clues to the type of cipher that you are dealing with.
The workbook will shift the message into uppercase automatically and eliminate any punctuation.
This webpage assumes that you have already completed:
Open a new Excel workbook and get the worksheet into Design Mode by clicking on the Design Mode toggle. Add a large text box and a command button to your workbook. |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||
The first block of VBA code is taken from the clean and case button of the Simple Shift Cipher. Part 2 project. Test your code to check that it strips out punctuation etc. and converts lower to uppercase. |
Private Sub CommandButton1_Click() newStr = "" newStr1 = "" sourceStr = TextBox1.Value newStr = Format(sourceStr, ">") For i = 1 To Len(sourceStr) If (Asc(Mid(newStr, i, 1)) >= 65) Then If (Asc(Mid(newStr, i, 1)) <= 90) Then newStr1 = newStr1 + Mid(newStr, i, 1) End If End If If (Asc(Mid(newStr, i, 1)) = 32) Then newStr1 = newStr1 + Mid(newStr, i, 1) End If Next i TextBox1.Value = newStr1 End Sub | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Now we need to add the code to work through the ciphertext and count up occurences of each letter. sourceStr = TextBox1.Value For i = 0 To 25 Worksheets("Sheet1").Cells(6 + i, 28).Value = 0 Next i For i = 1 To Len(sourceStr) If ((Asc(Mid(sourceStr, i, 1)) >= 65) And (Asc(Mid(sourceStr, i, 1)) <= 90)) Then letterCount = Worksheets("Sheet1").Cells(Asc(Mid(sourceStr, i, 1)) - 59, 28).Value + 1 Worksheets("Sheet1").Cells(Asc(Mid(sourceStr, i, 1)) - 59, 28).Value = letterCount End If Next i End Sub Note
Use Excel in the normal way to draw frequency graphs for normal English and the ciphertext. Use this code =100*M6/M$32 in cell N6 to calculate the percentage of A's in the text. Copy it down the column for the other letters. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
Extension to Periodic Viginere Ciphers. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
Start a new workbook. Add a 'spin button' from the controls toolbar to the sheet and use this to alter the period of the suspected Viginere repeat. Put the label 'period' into cell I9 and make the target of the spin button cell I10. Add a button and call it 'run'. Also add the statisticss for plain English and a large text box. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
Make sure that you are in design mode and double click the 'run' button to summon the VBA editor. The code you need is shown below with commentary on the right.
Private Sub CommandButton1_Click() |
freqjump is the number of letters in the suspected password. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Example of the 'product'. In this case the guess of 6 for the keyword length is wrong as the stats don't look close enough to english. They are not that for off though.
Once you get a match. Try to identify letters such as ETAION and use these to work back to candidate keywords. IE if E has become such and such what letter is A? The cipher text letter corresponding to A will be one of teh letters of the keyword. Extension to use with product moment correlation coefficients. |
last updated 6th October 2013