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. 

Tuesday, 3 November 2015

thoughts: The Martian


This book was recommended to me by my co-worker Paul, who was reading it at the time. The movie looked good and in my experience I almost always tend to enjoy a book more than its movie counterpart. I think in order to enjoy the book without being influenced by someone else's interpretation of it, you have to read it before watching the movie. Hence why I downloaded it onto my Kobo and got reading!

Overall, I loved this book, despite the fact that it was fiction. I don't read a whole lot of fiction anymore, but I like to slip in a fiction novel after reading a few non-fiction novels just so things don't get repetitive. I read non-fiction because I love learning, and in general I think i am more engaged in a books written about real life, and real things. Now that I'm considering it, I think I should say I'm less engaged in a fictional book precisely because its not real. Doesn't really matter; semantics.

However, despite the fact that it was fictional, The Martian reads very much like a non-fiction novel. The author, Andy Weir, works as a software engineer (in addition to writing books now I suppose) and apparently he tried to make the story as realistic and scientifically accurate as possible. I'm not an astrophysicist, botanist, or NASA employee, but I was convinced that what I was reading was at least conceptually plausible and that makes more a much more interesting story.

Quick Plot Summary: A Manned mission to Mars in the near future involves a crew of 6 astronauts. The plan was to be on Mars for 31 days but the mission is cut short on day 6 when a heavy dust-storm risks damaging the "ascent vehicle". 5/6 astronauts make it to the ascent vehicle in order to leave the planet but the 6th, Mark Watney, is struck by a flying piece of debris and the crew presumes he is dead due to the puncture in his space suit (they can all view bio-monitor data provided from their suits which is relayed to each crew member). Watney survives while the rest of the crew leaves in the only way off Mars. He has to figure out how to survive on Mars for 4 years until the next planned mission arrives with only a years supply of food.

What I loved about this book was that Weir didn't try to incorporate too many thematic elements into it. Within the first twenty pages you understand exactly how the story is going to be told, and what you're getting into. The book is written as a series of journal logs and audio logs recorded by Watney while stranded on Mars, interspersed with narrated scenes from Earth as well as the spacecraft carrying the other crew members back to Earth. Set-up this way, the book gives the feeling of an intimate connection with Watney as he plans his survival.

Being an engineer, I love problem solving. In the book, Watney is a mechanical engineer/botanist, which provided him with possibly the best foundation of knowledge to maximize his chances of survival. Watney approaches all the problems he encounters with a sort of emotionally-detached, logical view that I can relate to. He understands that, facing death as the only other alternative to escaping the planet, he simply has to line-up the problems standing between him and escaping, and do his best to solve them. He takes you through his thought process as he works, top-down, to formulate his plan of action and slowly accomplish tasks as his plan granulates and becomes more detailed. The story serves as a great example of the engineering problem-solving process. It should be recommended reading for those mandatory professional engineering courses you have to take in your undergrad.

Considering that the premise of the book revolves around a lone man, left to die on a planet 54.6 million kilometers away from Earth with little chance of survival, you could easily see how the story could internalize and focus on Watney's emotions, state of mind, or mental well-being. This is not the case at all however, the story focuses on the reality of the situation. Watney takes you through the exact process of how he survives, and he leaves no room in his journal logs to wax philosophical on his situation, or go off on tangents regarding the implications of his situation in the context of the history of humans. While these could certainly be explored as themes in a story like this, that's not what The Martian is about. It's left up to the reader to develop these views and to realize the bravery and resilience that Watney exhibits.

In essence, what I'm saying is that The Martian is a story built around technical details. It probably is not for everyone, specifically people who don't have much interest in technology or space exploration. But if you do like those areas, or if you have some sort of STEM background, than I would recommend it to you, non-existent reader of this blog post.

Weir's writing style is definitely influenced by his background in computer programming. He is not exactly the most illustrative in his prose, but he manages to convey the details necessary to build each scene in your head and to understand the character motivations without having to explicitly describe them. I think the idea behind the story is so intriguing and relevant to today's zeitgeist that it really only needs a writer like Weir to flesh it out with the vivid reality provided by technical feasibility. Weir wanted to ensure the details of Watney's survival plan and the Mars mission in general were as close to real-life as possible, and I think this is really what makes this stand out as an exemplary work of sci-fi fiction. You almost get the feeling at times that you're reading " A Survival Guide to Mars".


I'm going to go see this in theatres on  Friday with my Grandma. I'll update with my thoughts on the movie then.

Update:  So I ended up not seeing the movie until Saturday with Prosha (Grandma wasn't feeling up to it) but I thought it was excellent. I think it was one of the more faithful book-to-movie adaptations I've ever seen. Ridley Scott managed to keep the main storyline almost completely the same, and made very few concessions in it's recreation. Many of my favourite lines of dialogue from the book made it to the movie script, and the actors that delivered them did a great job.
I really liked how they retained the great humor from the book, it was one of the best features of the book itself.






Wednesday, 28 October 2015

hello world

I've decided to start writing things down. I have been thinking about it lately and I think it would be a good idea for me to be able to organize my thoughts. Mehrzad was over tonight to watch the second half of the raptors season opener with me (they won 106-99), and I ran the idea by him and he had apparently the same idea today. I think Mehrzad and myself are very similar. I've also started reading two other blogs recently, which have provided more inspiration and motivation for me.

The first blog is Thoughts from Inside the Box, which is written by a guy that is currently working at Google, and is living inside an 18' box-truck in the Google parking lot. Apparently the ridiculous housing market in San Francisco was a big factor in his decision, and he decided investing in a Truck instead of paying $1000+ a month for a bedroom was a better financial strategy...which it is. I think this guy really resonated with me because I am in a similar situation to him, but on the other side of the coin where I am the schmuck that is paying $1000+ for rent every month and not putting it towards something smarter like...oh I dunno, my student loans. Really not happy with my financial situation presently.

The second blog is the late Aaron Swartz's, Raw Thought. I watched a documentary on Swartz recently and have read up on him quite a bit since then. He is an absolutely fascinating, and tragic, story. I think he represented the epitome of our internet generation; an extremely intelligent mind combined with early exposure to computers resulted in the innovative and idealistic advocate for the power of the internet that was Swartz. That sentence was a mouthful, I think I need to work on my prose. I guess that's why I'm starting this blogging thing. I think I will implement a no-backtracking rule while I write these posts. Just a written regurgitation of my stream of thought...hence the name of the blog: flow.

Well really it's because that's my nickname but there's levels to this shit.

Anyways, Swartz's blog is insane. He basically wrote an exposition for every event, thought, or book he encountered in his life. Some of the posts are so interesting to read. I remember coming across his blog last year after I finished reading Infinite Jest and I was trying to figure out what the fuck happened in it. He had posted a concise and super informative breakdown of what actually occurred at the ending of the book, and as far as I could tell this was basically an original theory he had written and it almost certainly was correct. I find that mind-blowing considering this book is one of the most highly acclaimed (and convoluted) works of post-modern fiction from the 20th century. Swartz was a genius; I really look up to him and I am really starting to connect with some of the views and realizations he expressed in his early-20's, now that I'm also in my early 20's.  If he was strong advocate that blogging is beneficial for oneself, which he was, then I think it would be helpful to give it a try.