Frequency Analysis with Python

Frequency Analysis is commonly used in cryptanalysis on classical ciphers as a step in deducing the plain text from cipher text. It works on the principle that certain letters on average appear more frequently than others. For example the letters “E” and “T” in the English language are most common. This means in monoalphabetic ciphers the most common letter found during frequency analysis is likely to be a common letter in the English language.
Frequency analysis works better the larger the text to be analysed is. This is because the results aren’t as easily skewed with less common letters populating a short sentence.
This script allows for analysis of single letters or grouping of letters known as n-grams. This is useful when common English letter pairs like TH and ER can be matched to the corresponding letter pairs in the analysed cipher text.

This is my second python program. You enter some cipher text into the input. You then select what n-gram you want to analyse for and press enter. The program print the n-grams it finds along with the occurrences. The results are printed in order of value.

#-------------------------------------------------------------------------------
# Name:        Frequency Analysis
# Purpose:     Count the amount of n-grams in ciphertext
#
# Author:      James Woolley
#
# Created:     28/06/2012
# Copyright:   (c) James 2012
# Licence:     Open Source
#-------------------------------------------------------------------------------
inputText = str(raw_input("Please enter the cipher text to be analysed:")).replace(" ", "") #Input used to enter the cipher text. replace used to strip whitespace.
ngramDict = {}
highestValue = 0
def ngram(n): #Function used to populate ngramDict with n-grams. The argument is the amount of characters per n-gram.
    count = 0
    for letter in inputText:
        if str(inputText[count : count + n]) in ngramDict: #Check if the current n-gram is in ngramDict
            ngramDict[str(inputText[count : count + n])] = ngramDict[str(inputText[count : count + n])] + 1 #increments its value by 1
        else:
            ngramDict[str(inputText[count : count + n])] = 1 #Adds the n-gram and assigns it the value 1
        count = count + 1
    for bigram in ngramDict.keys(): #Iterates over the Bigram dict and removes any values which are less than the adaquate size (< n argument in function)
        if len(bigram) < n:
            del ngramDict[bigram]
ngram(int(raw_input("Please enter the n-gram value. (eg bigrams = 2 trigrams = 3)")))
ngramList = [ (v,k) for k,v in ngramDict.iteritems() ] #iterates through the ngramDict. Swaps the keys and values and places them in a tuple which is in a list to be sorted.
ngramList.sort(reverse=True) #Sorts the list by the value of the tuple
for v,k in ngramList: #Iterates through the list and prints the ngram along with the amount of occurrences
    print("There are " + str(v) + " " + str(k))

Cracking the Caesar Shift Cipher with Python

I recently started learning Python and have created a small script which can encrypt or decrypt plaintext or cipher text from a Caesar Shift Cipher. It’s nothing special but I’m curious as to how much easier it can be accomplished. I’m sure my method is very long winded and substantially more complicated than it needs to be.

(Currently wont work with Spaces) Now works with Spaces.


#-------------------------------------------------------------------------------
# Name:        Ceaser Shift Cipher
# Purpose:      Example code for analysing shifts on plain text by a Caesar shift cipher.
#
# Author:      James Woolley
#
# Created:     14/06/2012
# Copyright:   Open Source
#-------------------------------------------------------------------------------
#Creates the base Alphabet which is used for finding preceeding characters from the ciphertext.
baseAlphabet = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')
print ("Welcome to a python Caesar shift cipher analyser. You will first be asked to enter the cipher text to be decrypted and then the amount of shifts you want to perform. Entering ALL for the shift amount will iterate through all 26 combinations.")
cipherText = raw_input("Please enter the Cipher text")
shiftAmount = raw_input("Please enter the shift amount. Enter ALL for brute force shifting.")
if shiftAmount == "all":
    shiftAmount = str(shiftAmount)
else:
    shiftAmount = int(shiftAmount)
baseLetterIndex = 0
completePlainText = [] #The variable each processed letter is appended to
def shiftAndStore(shift):
    for increment in cipherText:
        for value in baseAlphabet:
                    if value == increment:
                            while shift + baseAlphabet.index(value) >= 26: #Checks if the shift will be higher more than the 26 letters of the alphabet.
                                shift = shift - 26 #If shifts are higher 26 is subtracted till the shift and base alphabet value is lower than 26.
                            baseLetterIndex = baseAlphabet.index(value) + shift #assigns the index value of the processed letter to baseLetterIndex variable.
                            completePlainText.append(baseAlphabet[baseLetterIndex]) # appends the processed letter to the completePlainText variable.
                    if increment == (" "): #Handles the spaces Temporarily makes the value of increment X to prevent it looping 26 times.
                        completePlainText.append(" ")
                        increment = ("X")


if shiftAmount == "all": #Checks weather user selected brute force method or specific shift value.
    shiftAmount = int(0)
    while shiftAmount < 25: #Iterates through all 26 combinations producing a processed value each time.
        shiftAndStore(shiftAmount)
        shiftAmount = shiftAmount + 1
        print "The Encoded / Decoded text on shift  " + str(shiftAmount - 1) + " is " + (''.join(completePlainText)) #Prints the shift amount and processed text.
        completePlainText = []
else: #Executed if specific shift value is chosen and not brute force.
    shiftAndStore(shiftAmount)
    print "The Shift Amount is " + str(shiftAmount)
    print "The Encoded / Decoded text is " + (''.join(completePlainText))

Cracking the Caesar Shift Cipher with Java

For one of my modules at uni I’m studying Java. It’s the first programming language I have learned and I thought it would be helpful to try and create my first program in Netbeans. I thought a good one to start with is to make a program that can decipher the Caesar shift cipher. It took me about 5 – 6 hours to do over a couple of days and I finally got it to work. (I can hear you all laughing at the amount of time it took). I thought I would release the source code as well so that anyone else in my shoes who is learning java can have a look to see how I did it. Then make their own much better version as my code is very messy. Anyway, below is the screenshot of the GUI and the download links.

James Cipher Cracker Executable JAR file (2250)
James Cipher Cracker Source Code (1815)

 

WIFI Situation

Me and @ryanb4084 decided to drive around near where we live and try and get some information on the situation of WIFI access points. We used Kismet to log the access points and Giskismet to add the Kismet log file into a SQL database which can be outputted into a CSV file or KML file to view the access points locations on Google Earth. Overall we logged just over 1000 access points. Below are a few pie charts showing what we found.

Its quite worrying how many people still have no encryption and still use WEP.