Caesar Cipher Decryption Tool in Java

One of my university modules tasked me with developing a Caesar cipher decryption tool. The tool had to be able to decrypt any encrypted text without knowing the offset used to encrypt the text. The code is surprisingly short and I am not aware of any other Java examples.  The code was originally developed with a command line interface however I have ported it to run on a tomcat server.

To tool is available here: CCDT and the text file mentioned below is from Practical Cryptography

Quick outline:

What is a Caesar cipher?
A Caesar cipher works by moving the letters in the alphabet by a certain number called the offset. If I encrypt the letter ‘A’ using an offset of 3 then ‘A’ becomes ‘D’. The rotation loops around the alaphabet so the letter ‘Z’ would become ‘C’ with an offset of 3.

Patterns in a language
A language has certain patterns in the writing, for example in English the letter ‘E’ will appear more often than the letter ‘Z’ so these pattersn can be used to try and work out the rotation. The program I developed uses a text file called ‘quadgrams’ which contains a block of four letters with the number of occurances in a piece of text.

Brief description of the program
The program takes in a piece of encrypted text and changes the offset all the way up to 25 and for each offset it passes it to a method that works out the mathematical probability that the piece of text is now English with that offset. To work out the mathematical probability it loads the quadgrams text file into a HashMap and searches the map for quadgrams that appear in the cipered text. It then stores the overall probability for that particular offset, moves to the next offset and repeats, if the probability is higher than the previous offset then this offset and probability is stored. See flow diagram below!

CCDT Flow Chart
CCDT Flow Chart

Disclaimer:
You can use this code anyway you like but you must provide acknowledgement to Monotok.org/Christopher Hamer. If you have any improvements or questions please comment

This is the main class:

The file reader class:

This is the index.jsp file, this is basically the html homepage. Netbeans has a palette to help add elements to the page. Very helpful if you haven’t got much experience in html.

This is the Java servlet that interacts with the index page and uses the other classes.

Explanation of the code will be available soon.

Please share 🙂