Sunday 26 February 2017

The Code Book

Introduction


I recently finished reading "The Code Book" by  Simon Singh. Published in 1999, The Code Book provided a greatly detailed account of the history of cryptography, and its necessary counter-part: code breaking. I thoroughly enjoyed reading it because it was extremely informative and well structured. Singh guides the reader through the history of humanity and our need to secure our communications. From the advent of written information, there has always been a need to hide important information from certain people who would benefit from the knowing of said information, and would likely disadvantage the sender and receiver in some way.

Most commonly, the need for secrecy arises in times of war and conflict. As such, throughout history war has been the most influential driving force in the advancement of cryptography, and its code breaking complement. Singh describes how the race between these two forces has pushed each to new levels of intelligence and creativity, and the overall advantage continues to alternate between code-makers and code-breakers as breakthroughs in each discipline occur.

[....]

The book also provides a challenge for the reader, a code breaking challenge that Singh has prepared. It was originally offered in 1999 with the publishing of the book along with a $15,000 prize for the first person to complete it. It was first solved in October of 2000. It consists of a series of 10 cipher texts of apparently increasing difficulty. I am going to work through my solution to solving them in this post. The texts themselves can be found directly on the author's website.

Even though the website states that only the last 2 exercises actually require computing power, I decided I would have more fun solving all of them through programming. It will be a good foray into cryptography techniques and I will be building some general functions/tools that I can re-use to solve subsequent problems.

Cipher 1


Cipher text can be found here. It is a "Simple Monoalphabetic Substitution Cipher" apparently, so that means each letter in the cipher text represents another letter in the plain text. Simple substitution x-->y where y is A through Z. The trick is finding each corresponding value of x. I took to Java to start messing around with the text. I assumed that this might just be a simple Caesar cipher, so I built a quick function to convert a String of cipher text into a caesar shifted version. Function shown below.

    public static String caesarShift(String text, int offset){
     
        StringBuilder sB = new StringBuilder();
        char[] textArray = text.toCharArray();
     
        for(char c : textArray){
            if((c >= 65 && c <= 90)){ //Uppercase
                c = (char) ( 65 + ((c + offset) - 65)%26);
            }
            else if(((c >= 97 && c <= 122))){ //Lowercase
                c = (char) ( 97 + ((c + offset) - 97)%26);
            }
            sB.append(c);
        }
     
        return sB.toString();
    }

I ran the cipher text through this function for all 25 possible shifts and the result was...garbage every time. So clearly this was not a shift cipher. This means the reordered cipher alphabet is either random, or rearranged according to some other keyword or relationship. Regardless, figuring it out is relatively simple with frequency analysis, or even just some simple deduction skills combined with knowledge of the English language (I guess I'm assuming the plaintext is in English). I chose to pursue the latter method, and so I turned to some pen and paper!

A little simple pattern recognition lead me to realizing words like "JPX" probably represented "THE" and a single "M" had to represent either "A" or "I". Following those assumptions, its easy to deduce that since "JPMJ" either can be "THAT" or "THIT", then M has to be represented by A. Building upon this, the rest of the cipher alphabet quickly falls into place, and the process is snowballed as more of it is revealed. Finally, once I had finished the cipher alphabet, I made a function to do the grunt work of actually decrypting the cipher text for me. Function shown below.


public static String substituteCipher(String text, char[] subs){
        
        StringBuilder sB = new StringBuilder();
        char[] textArray = text.toCharArray();
        
        for(char c : textArray){
            if((c >= 65 && c <= 90)){
                c = subs[c-65];
            }
            else if(((c >= 97 && c <= 122))){
                c = subs[c-97];
            }
            sB.append(c);
        }
        
        return sB.toString();           
    }

All I had to do was type in a character array representing the substituted letters and use that as the second argument to the function above, along with a string representation of the cipher text, and then print out the output. The output is shown below.

IN THE SAME HOUR CAME FORTH FINGERS OF A MAN’S HAND, AND WROTE OVER AGAINST THE CANDLESTICK UPON THE PLASTER OF THE WALL OF THE KING’S PALACE; AND THE KING SAW THE PART OF THE HAND THAT WROTE. THEN THE KING’S COUNTENANCE WAS CHANGED, AND HIS THOUGHTS TROUBLED HIM, SO THAT THE JOINTS OF HIS LOINS WERE LOOSED, AND HIS KNEES SMOTE ONE AGAINST ANOTHER. THE KING CRIED ALOUD TO BRING IN THE ASTROLOGERS, THE CHALDEANS, AND THE SOOTHSAYERS. AND THE KING SPAKE, AND SAID TO THE WISE MEN OF BABYLON, WHOSOEVER SHALL READ THIS WRITING, AND SHOW ME THE INTERPRETATION THEREOF, SHALL BE CLOTHED WITH SCA
Voila! The first cipher is decoded. 

No comments:

Post a Comment