{"id":668,"date":"2008-08-08T10:57:33","date_gmt":"2008-08-08T10:57:33","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2008\/08\/08\/simple-encryption-introduction-and-substitution-ciphers\/"},"modified":"2008-08-08T10:57:33","modified_gmt":"2008-08-08T10:57:33","slug":"simple-encryption-introduction-and-substitution-ciphers","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2008\/08\/08\/simple-encryption-introduction-and-substitution-ciphers\/","title":{"rendered":"Simple Encryption: Introduction and Substitution Ciphers"},"content":{"rendered":"<p> The starting point talking about encryption is to understand<br \/>\nwhat the point of it is; what it&#8217;s supposed to do, what problems it&#8217;s supposed to avoid.<\/p>\n<p> Encryption is fundamentally about communication: you&#8217;ve got two parties who want to communicate, but don&#8217;t want anyone else to be able to listen in.<\/p>\n<p> They way that you do that is by sharing a secret. You use that secret to somehow modify the information that you&#8217;re going to send, so that it can&#8217;t be read by someone who doesn&#8217;t have the secret. People often think of encryption as a way of using a password to hide information, but a password is just one of many kinds of secrets that you can use. The secret that you share with your counterpart can be a password, a number, a textbook, or just about anything else you can imagine.<\/p>\n<p><!--more--><\/p>\n<p> For example, one very hard to break encryption is based on you and your counterpart having identical copies of a textbook. Then for each word in your message,<br \/>\nyou find that word in your textbook. You encode the word as page#, paragraph#, word#. The encrypted message is then a sequence of triplets of numbers. Without knowing what book you used, if you do a good job with this kind of system,<br \/>\nyou can make it pretty much impossible to decode your message without knowing<br \/>\nwhat book you used. But this illustrates an important point: in encryption, you need to have a clear understanding of exactly <em>what<\/em> the shared secret is, and<br \/>\ntake care to protect it. (During World War 2, the allies only managed to break<br \/>\nthe Enigma encryption system after they got an actual Enigma encryption machine: the structure of the machine is a critical part of the secret of Enigma encryption. With<br \/>\nthe machine, and a few other tricks to figure out the rest of the secret, Enigma<br \/>\nwas cracked. Without the machine, it wouldn&#8217;t have been.)<\/p>\n<p> One of the simplest forms of encryption is something called a simple<br \/>\nsubstitution cipher.  The idea of that is that you have a mapping from one set of symbols to another. If you know the mapping, you can encode and decode messages. The secret that you&#8217;re sharing with the person you&#8217;re communicating with is the character mapping. If someone can either get, or figure out that mapping, then<br \/>\nyou no longer have a secret. As I&#8217;ll explain later, the problem with this kind<br \/>\nof encryption is that it&#8217;s pretty easy to figure out the secret, and thus break<br \/>\nthe encryption.<\/p>\n<p> To see how substitution ciphers work, we&#8217;ll look at a simple example. To keep things simpule, we&#8217;ll only look at letters; we&#8217;ll leave spaces and punctuation unchanged, and upper\/lower case unchanged. The substitution map is as follows:<\/p>\n<table border=\"1\">\n<tr>\n<th>Character<\/th>\n<td>A<\/td>\n<td>B<\/td>\n<td>C<\/td>\n<td>D<\/td>\n<td>E<\/td>\n<td>F<\/td>\n<td>G<\/td>\n<td>H<\/td>\n<td>I<\/td>\n<td>J<\/td>\n<td>K<\/td>\n<td>L<\/td>\n<td>M<\/td>\n<\/tr>\n<tr>\n<th>Coding<\/th>\n<td>G<\/td>\n<td>Q<\/td>\n<td>F<\/td>\n<td>C<\/td>\n<td>Z<\/td>\n<td>L<\/td>\n<td>Y<\/td>\n<td>J<\/td>\n<td>B<\/td>\n<td>E<\/td>\n<td>W<\/td>\n<td>R<\/td>\n<td>X<\/td>\n<\/tr>\n<tr>\n<th>Character<\/th>\n<td>N<\/td>\n<td>O<\/td>\n<td>P<\/td>\n<td>Q<\/td>\n<td>R<\/td>\n<td>S<\/td>\n<td>T<\/td>\n<td>U<\/td>\n<td>V<\/td>\n<td>W<\/td>\n<td>X<\/td>\n<td>Y<\/td>\n<td>Z<\/td>\n<\/tr>\n<tr>\n<th>Coding<\/th>\n<td>P<\/td>\n<td>L<\/td>\n<td>D<\/td>\n<td>S<\/td>\n<td>I<\/td>\n<td>U<\/td>\n<td>H<\/td>\n<td>V<\/td>\n<td>A<\/td>\n<td>K<\/td>\n<td>O<\/td>\n<td>T<\/td>\n<td>N<\/td>\n<\/tr>\n<\/th>\n<\/table>\n<p> Using this, we can encrypt things. We take each letter of the message<br \/>\nwe want to send, and look it up in the character row of the table, and replace it<br \/>\nwith the corresponding letter in the coding row. To decrypt, we do the opposite: look up the letter in the encrypted message in the coding row, and replace it with the corresponding letter in the character row.<\/p>\n<p> For example, &#8220;Mark has a big nose&#8221; would be encrypted as &#8220;Xgiw jgu g qby pluz&#8221;.<\/p>\n<p> Implementing a cipher like this borders on trivial. Here&#8217;s a way of doing it in python, with a sample cipher and invocation demo:<\/p>\n<pre>\nclass Cipher(object):\ndef __init__(self, mapping):\nself._mapping = mapping\nself._reverse = {}\n# set up the reverse mapping for decryption.\nfor k in mapping:\nself._reverse[mapping[k]] = k\ndef encrypt(self, message):\n\"\"\"Take a message string, and encrypt it according to the\ncipher mapping\"\"\"\"\nresult = \"\"\nfor c in message:\nif c.islower():\ncu = c.upper()\nelse:\ncu = c\nif self._mapping.has_key(cu):\nm = self._mapping[cu]\nelse:\nm = cu\nif c.isupper():\nresult += m\nelse:\nresult += m.lower()\nreturn result\ndef decrypt(self, message):\nresult = \"\"\nfor c in message:\nif c.islower():\ncu = c.upper()\nelse:\ncu = c\nif self._reverse.has_key(cu):\nm = self._reverse[cu]\nelse:\nm = cu\nif c.isupper():\nresult += m\nelse:\nresult += m.lower()\nreturn result\ncipher = Cipher({ 'A': 'G', 'B': 'Q', 'C': 'F', 'D': 'C', 'E': 'Z',\n'F': 'L', 'G': 'Y', 'H': 'J', 'I': 'B', 'J': 'E',\n'K': 'W', 'L': 'R', 'M': 'X', 'N': 'P', 'O': 'L',\n'P': 'D', 'Q': 'S', 'R': 'I', 'S': 'U', 'T': 'H',\n'U': 'V', 'V': 'A', 'W': 'K', 'X': 'O', 'Y': 'T',\n'Z': 'N', })\nen = cipher.encrypt(\"Mark has a Big nose\")\nde = cipher.decrypt(en)\nprint \"Encrypted = %s\" % en\nprint \"Decrypted = %s\" % de\n<\/pre>\n<p> That&#8217;s it, and the largest part of that is really dealing with maintaining upper and lower case through the encryption.<\/p>\n<p> Obviously, this isn&#8217;t a great way of encrypting things. Why?<\/p>\n<p> By maintaining case, punctuation, and word boundaries, you make it <em>really<\/em> easy to identify some likely mappings. For example, if you see a single uppercase letter anywhere other than the beginning of a sentence, you can be almost certain that it&#8217;s &#8220;I&#8221;. If you see a singe character that appears on its own in lowercase, it&#8217;s<br \/>\nquite probably &#8220;a&#8221;. Frequent three letter words will be either &#8220;and&#8221; or &#8220;the&#8221;, and &#8220;the&#8221; will frequently appear at the beginning of sentences.  Even if you include mappings for all of the punctuation and space, and you don&#8217;t distinguish case, it&#8217;s still easy to decrypt.<br \/>\nWith a fixed mapping like this, by using frequency tables for the characters and character groups, you&#8217;ll be able to pretty quickly<br \/>\nfigure it out. This is easy enough to crack that there&#8217;s actually a popular daily newspaper puzzle called &#8220;CryptoQuote&#8221; that presents an encrypted quote every day for readers to decode.<\/p>\n<p> Just to give you a sense of how easy this is: just look at the encryption of &#8220;Mark has a big nose&#8221;: &#8220;&#8221;Xgiw jgu g qby pluz&#8221;. The &#8220;g&#8221; is obviously an A. So we immediately<br \/>\nget &#8220;xAiw jAu A qby pluz&#8221;. A three letter word with &#8220;a&#8221; as the middle is probably<br \/>\n&#8220;has&#8221;, so we can guess that &#8220;h&#8221; is translated to &#8220;j&#8221;, and &#8220;s&#8221; is translated to &#8220;u&#8221;. So<br \/>\nwe get &#8220;xAiw HAS A qbz plSe&#8221;. In 10 seconds, you&#8217;ve got a third of the message decoded &#8211; and that&#8217;s for a message that&#8217;s only 5 words\/20 letters long!<\/p>\n<p> Improving on this, while keeping the same basic concept is pretty easy. For example, one common technique is to have each letter that you encode <em>also<\/em> alter the translation in some way. Ultimately, that&#8217;s the basis for how the infamous &#8220;Enigma&#8221; encryption machine used by the Nazi&#8217;s during World War 2 worked. Next post, I&#8217;ll show you a basic example of this technique, using something called a rotating cipher.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The starting point talking about encryption is to understand what the point of it is; what it&#8217;s supposed to do, what problems it&#8217;s supposed to avoid. Encryption is fundamentally about communication: you&#8217;ve got two parties who want to communicate, but don&#8217;t want anyone else to be able to listen in. They way that you do [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[84],"tags":[],"class_list":["post-668","post","type-post","status-publish","format-standard","hentry","category-encryption"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-aM","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/comments?post=668"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/668\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=668"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}