{"id":1810,"date":"2011-05-25T20:16:57","date_gmt":"2011-05-25T20:16:57","guid":{"rendered":"http:\/\/cemclinux1.math.uwaterloo.ca\/~cscircles\/wordpress\/?page_id=1810"},"modified":"2011-05-25T20:16:57","modified_gmt":"2011-05-25T20:16:57","slug":"15c","status":"publish","type":"page","link":"https:\/\/olescs.hkmu.edu.hk\/python\/15c\/","title":{"rendered":"15C: Caesar's JVTIVK JRCRU IVTZGV"},"content":{"rendered":"<!-- Please retain this notice and add more notes if you create a new version.<br \/>\nOriginal lesson author: David Pritchard, daveagp@gmail.com, http:\/\/cscircles.ca<br \/>\nLicense: http:\/\/creativecommons.org\/licenses\/by-nc-sa\/3.0\/-->\n<p><em>Exercises 15A, 15B, and 15C can be completed in any order.<\/em><\/p>\n<p>Cryptography is the art and science of hiding the meaning of information, in a way that only some people can see it. In this lesson we will introduce one of the simplest cryptographic methods, the <strong><a href=\"http:\/\/en.wikipedia.org\/wiki\/Caesar_cipher\">Caesar cipher<\/a> <\/strong>(also known as a\u00a0<strong>shift cipher<\/strong>), and you will to write a program to break it. You will design every aspect of your solution (unlike lesson 15A, which we broke into sub-parts).<\/p>\n<p>The Caesar cipher works by replacing each letter of the alphabet by another letter. To be precise, whenever you want to encode some text you need to pick a <strong>shift value<\/strong><em> S<\/em>, which is a number between 0 and 25. Then, you replace each letter in the text with the letter which is <em>S<\/em> positions later in the alphabet, circling around to the start after you reach Z at the end of the alphabet.<\/p>\n<h2>Example<\/h2>\n<p>Suppose we want to encode the secret message<\/p>\n<p style=\"text-align: center\"><code>JOIN ME AT EIGHT BY THE ZOO<\/code><\/p>\n<p>using the shift value <em>S<\/em>=2. The encryption rule says each letter is replaced by the one which occurs 2 positions later in the alphabet. For example, since the alphabet is ABCDEFGHI<strong>J<\/strong>K<strong>L<\/strong>..., the first letter <code>J<\/code> will be replaced by the letter <code>L<\/code>. Continuing, the <code>O<\/code> is replaced by <code>Q<\/code>, the <code>I<\/code> is replaced by <code>K<\/code>, et cetera. To encode the letter Y, we have to circle back to the start: after Y comes Z, then A, so <code>Y<\/code> is replaced by <code>A<\/code>. Likewise <code>Z<\/code> is replaced by <code>B<\/code>. So the encoded version of our secret message is:<\/p>\n<p style=\"text-align: center\"><code>LQKP OG CV GKIJV DA VJG BQQ<\/code><\/p>\n<p>If some spy saw this message, it would not be obvious to them what your message was about.<\/p>\n<div class='pybox modeNeutral' id='pybox0'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Short Answer Exercise: <\/span><span class='title'>Spy Coder<\/span><\/div>What is the result of encrypting <code>SPY CODER<\/code> with the shift value <em>S<\/em>=5? (Use uppercase.) You can write a program in the <a href=\"\/console\/\">console<\/a> to compute this, if you like \u2014 it might be useful later.<br><label for=\"pyShortAnswer0\">Your answer: <\/label><input type=\"text\" onkeypress=\"{if (event.keyCode==13) pbShortCheck(0)}\" id=\"pyShortAnswer0\"><div class=\"pyboxbuttons\"><input type=\"hidden\" name=\"type\" value=\"trimmableString\"\/><input type=\"hidden\" name=\"correct\" value=\"XUD HTIJW\"\/><input type=\"hidden\" name=\"slug\" value=\"15c.encode\"\/><input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type='submit' style='margin:5px;' value='Check answer' onClick = 'pbShortCheck(0)'\/><\/div><div class=\"pbresults\" id=\"pyShortResults0\"><\/div><div class=\"epilogue\">Correct!<\/div><\/div>\n<h2>Decoding<\/h2>\n<p>Once your friend gets the message, if they know the secret shift value <em>S<\/em> then it is easy for them to decipher the message: each letter is replaced by the one appearing <em>S<\/em> places <em>earlier<\/em> in the alphabet. For example, they would look at <code>L<\/code>, step back two positions in the alphabet and find <code>J<\/code>, which they now know is the first letter of your secret message. Again, they have to treat the alphabet as cyclic, assuming that <code>Z<\/code> comes before <code>A<\/code>.<\/p>\n<p><div class='pybox modeNeutral' id='pybox1'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Short Answer Exercise: <\/span><span class='title'>Spy Decoder<\/span><\/div>If the encrypted message is <code>HUD<\/code>, and\u00a0the shift value is\u00a0<em>S<\/em>=6, what was the original message? (Use uppercase.)<br><label for=\"pyShortAnswer1\">Your answer: <\/label><input type=\"text\" onkeypress=\"{if (event.keyCode==13) pbShortCheck(1)}\" id=\"pyShortAnswer1\"><div class=\"pyboxbuttons\"><input type=\"hidden\" name=\"type\" value=\"trimmableString\"\/><input type=\"hidden\" name=\"correct\" value=\"BOX\"\/><input type=\"hidden\" name=\"slug\" value=\"15c.decode\"\/><input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type='submit' style='margin:5px;' value='Check answer' onClick = 'pbShortCheck(1)'\/><\/div><div class=\"pbresults\" id=\"pyShortResults1\"><\/div><div class=\"epilogue\">Correct!<\/div><\/div>\n<h2>Working for the Bad Guys<\/h2>\n<p>You have been hired by a spy to help decipher a message encrypted using a shift cipher. Sadly, the spy does not know the value of <em>S<\/em>. You will use statistics\u00a0to write a program that has a good chance to automatically find the right value of <em>S<\/em>.<\/p>\n<p>Our method will be to use <strong>letter frequency analysis<\/strong>. In English, some letters are very common (like E) and some are very rare (like J). Here is a table of frequencies, based on counting the occurrences in a large body of text.<\/p>\n<table class=\"minyspace\" style=\"text-align: center\">\n<tbody>\n<tr>\n<th>A<\/th>\n<th>B<\/th>\n<th>C<\/th>\n<th>D<\/th>\n<th>E<\/th>\n<th>F<\/th>\n<th>G<\/th>\n<th>H<\/th>\n<th>I<\/th>\n<\/tr>\n<tr>\n<td>.0817<\/td>\n<td>.0149<\/td>\n<td>.0278<\/td>\n<td>.0425<\/td>\n<td>.1270<\/td>\n<td>.0223<\/td>\n<td>.0202<\/td>\n<td>.0609<\/td>\n<td>.0697<\/td>\n<\/tr>\n<tr>\n<th>J<\/th>\n<th>K<\/th>\n<th>L<\/th>\n<th>M<\/th>\n<th>N<\/th>\n<th>O<\/th>\n<th>P<\/th>\n<th>Q<\/th>\n<th>R<\/th>\n<\/tr>\n<tr>\n<td>.0015<\/td>\n<td>.0077<\/td>\n<td>.0402<\/td>\n<td>.0241<\/td>\n<td>.0675<\/td>\n<td>.0751<\/td>\n<td>.0193<\/td>\n<td>.0009<\/td>\n<td>.0599<\/td>\n<\/tr>\n<tr>\n<th>S<\/th>\n<th>T<\/th>\n<th>U<\/th>\n<th>V<\/th>\n<th>W<\/th>\n<th>X<\/th>\n<th>Y<\/th>\n<th>Z<\/th>\n<th><\/th>\n<\/tr>\n<tr>\n<td>.0633<\/td>\n<td>.0906<\/td>\n<td>.0276<\/td>\n<td>.0098<\/td>\n<td>.0236<\/td>\n<td>.0015<\/td>\n<td>.0197<\/td>\n<td>.0007<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>For example, the frequency of L is .0402=4.02%, which means that in average English text, 4.02% of the letters are L.<\/p>\n<p>Call the <strong>goodness<\/strong> of a letter its value in the chart above. Then for our statistical method, define the <strong>goodness<\/strong> of a sentence to equal the sum of the goodness of each of its letters. For example the goodness of the string <code>GOOD<\/code> is<\/p>\n<p style=\"text-align: center\">goodness(\"<code>GOOD<\/code>\") = .0202 + .0751 + .0751 + .0425 = .2129<\/p>\n<p>Now, the idea behind frequency analysis is that strings with higher goodness are more likely to represent English text. For example, if the spy sees the encoded message \"<code>JRRG<\/code>\", this might either represent the original text \"<code>GOOD<\/code>\" with shift\u00a0<em>S<\/em>=3, or \"<code>IQQF<\/code>\" with <em>S<\/em>=1. But the goodness of \"<code>IQQF<\/code>\" is<\/p>\n<p style=\"text-align: center\">goodness(\"<code>IQQF<\/code>\") = .0697 + .0009 + .0009 + .0223 = .0938<\/p>\n<p>and since .0938 &lt; .2129, your program should conclude that <code>GOOD<\/code> is more likely to be the correct message than <code>IQQF<\/code>.<\/p>\n<p><table class='pywarn'><tr><td class='pywarnleft'><img src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/warning.png'\/><\/td><td class='pywarnright'><span> Measuring strings by <em>goodness<\/em> is not perfect. Say the secret message was\u00a0<code>JAZZ<\/code> and the secret shift value was\u00a0<em>S<\/em>=10, so the encoded text was <code>TKJJ<\/code>. When you input <code>TKJJ<\/code> to your solver it will try <em>S<\/em>=10, giving <code>JAZZ<\/code> as a possibility with goodness .0846, but the best is\u00a0<em>S<\/em>=5, giving\u00a0<code>OFEE<\/code>\u00a0with goodness of .3514 as the program's best guess for the secret message. Your program should go ahead and output the non-English word <code>OFEE<\/code>. (Frequency analysis of this type works better if the secret message is longer.) <\/span><\/td><\/table><\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform2\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox2'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Auto-Decryption<\/span><\/div>\n<div>Write a program which does the following. First, it should read one line of input, which is the encoded message, and will consist of capital letters and spaces. Your program must try decoding the message with all 26 possible values of the shift <em>S<\/em>; out of these 26 possible original messages, print the one which has the highest goodness.<\/div>\n<p> For your convenience, we will pre-define the variable <code>letterGoodness<\/code> for you, a list of length 26 which equals the values in the frequency table above, <\/p>\n<pre>letterGoodness = [.0817,.0149,.0278,.0425,.1270,.0223,.0202,...<\/pre> <\/p>\n<div><a class=\"hintlink\"  id=\"hintlink3\">Click here for some general advice.<\/a><div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode2' id='usercode2'  cols=10 rows=20   class='pyboxCode RW'>\nprint(letterGoodness) #testing... it's predefined\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory2' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput2\">Enter testing statements like <code>print(myfunction(\"test argument\"))<\/code> below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<input type='hidden' id='defaultCode2' value='print(letterGoodness) #testing... it\\u0027s predefined\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit2' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch2\" value=\"Input Switch\" onclick=\"pbInputSwitch(2,'Y')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(2)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(2,'Y')\" ><\/td>\n<\/tr><\/table><select id='pbSelect2' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(2,'15c.autodecrypt')\" >History<\/option>\n<option name='default' data-pbonclick=\"pbSetText(2,descape($('#defaultCode2').val()))\" >Reset code to default<\/option>\n<option name='help' data-pbonclick=\"helpClick(2);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse2\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"2\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"44fbbb6d3ec3e23628a02ce6fc38edd6\"\/>\n<div id='pbresults2' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(2);});pbInputSwitch(2,\"Y\");<\/script>\n<\/div>\n<p><table class='pywarn'><tr><td class='pywarnleft'><img src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/warning.png'\/><\/td><td class='pywarnright'><span> The solution above is called a <em>brute force<\/em> solution since you just tried all 26 possibilities. You could have even done this by hand if you needed to. Modern cryptographic protocols, on the other hand, are designed so that brute force solutions fail, even if you use very fast computers to try all possibilities. <\/span><\/td><\/table><\/p>\n<p>If you wanted to make this auto-decoding system more accurate, you could pay attention to other English statistics, such as \"what letters are likely to occur in pairs next to each other? what letters are likely to start a word?\" This is also useful for more general <a href=\"http:\/\/en.wikipedia.org\/wiki\/Substitution_cipher\">substitution ciphers<\/a>, where letters replace letters but not in a cyclic way. For such ciphers brute force does not work since there are 26*25*...*1\u00a0possible substitution ciphers, which equals approximately 4 * 10<sup>26<\/sup> (four hundred septillion).<\/p>\n<p>You have completed this lesson! Feel free to send your friends an encrypted message to celebrate.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Exercises 15A, 15B, and 15C can be completed in any order. Cryptography is the art and science of hiding the meaning of information, in a way that only some people can see it. In this lesson we will introduce one &hellip; <a href=\"https:\/\/olescs.hkmu.edu.hk\/python\/15c\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1810","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/1810"}],"collection":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/comments?post=1810"}],"version-history":[{"count":0,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/1810\/revisions"}],"wp:attachment":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/media?parent=1810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}