You are going to use Excel VBA to develop frequency analysis tools.
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.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
last updated 29th June 2007